树莓派备份系统镜像并压缩

前言

手上的pi zero w用了一段时间,上面也部署了相当多的服务应用,都是心血啊,可不想啥时候系统损坏了重装一遍,于是想到备份一波系统镜像

备份

  1. 本地新建一个文件,将格式改为.img
  2. pizero.img
  3. 树莓派系统内存卡插上电脑
  4. 打开win32 disk imager,镜像文件选择好刚才新建的镜像,驱动选择插上的内存卡
  5. 点击 read (读取)!!!!,千万不要选错.
  6. 这样算是备份好了,但是有个问题,这样相当于将内存卡复制了一份,大小会和原先内存卡一样大.因此我们需要进行压缩

压缩

PiShrink压缩脚本是GitHub的一位大佬写的

(需要Linux环境,将以上的镜像文件将其挂载至Linux)

https://github.com/Drewsif/PiShrink

下载脚本,移动到bin目录是为了全局都可以直接使用脚本

wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh
chmod +x pishrink.sh
sudo mv pishrink.sh /usr/local/bin

用法也相当简单

sudo pishrink.sh pizero.img

pi@raspberrypi:/home/pi/ $ sudo pishrink.sh pizero.img
pishrink.sh v0.1.2
pishrink.sh: Gathering data ...
Creating new /etc/rc.local
pishrink.sh: Checking filesystem ...
rootfs: 80093/1904000 files (0.3% non-contiguous), 1105429/7797760 blocks
resize2fs 1.44.5 (15-Dec-2018)
pishrink.sh: Shrinking filesystem ...
resize2fs 1.44.5 (15-Dec-2018)
Resizing the filesystem on /dev/loop0 to 1181331 (4k) blocks.
Begin pass 2 (max = 101518)
Relocating blocks XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 238)
Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 4 (max = 8379)
Updating inode references XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/loop0 is now 1181331 (4k) blocks long.

pishrink.sh: Shrinking image ...
pishrink.sh: Shrunk pizero.img from 30G to 4.8G ...

效果相当明显,直接将30G的镜像压缩成4.8G,同时在恢复镜像时,还会自动扩容.

如果想压缩成一个新文件呢?

加上-s

sudo pishrink.sh -s pizero.img newpi.img

参考

https://shumeipai.nxez.com/2020/09/11/pishrink-make-raspberry-pi-images-smaller.html

tag(s): 树莓派 
show comments · back · home