备份 raspberrypi

使用 dd 备份 sd 卡数据

sudo dd bs=4M if=/dev/mmcblk0 | gzip > PiOS.img.gz

mount 文件

使用 parted 命令查看镜像文件分区信息,如下所示:

root@raspberrypi:/media/data/bak/img # parted ./PiOS.img
GNU Parted 3.2
Using /media/data/bak/img/PiOS.img
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) B
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resizepart NUMBER END                    resize partition NUMBER
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted
(parted) unit
Unit?  [compact]? y
(parted) print
Model:  (file)
Disk /media/data/bak/img/PiOS.img: 31914983424B
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start       End           Size          Type     File system  Flags
 1      4194304B    272629759B    268435456B    primary  fat32        lba
 2      272629760B  31914983423B  31642353664B  primary  ext4

(parted) q

执行如下命令挂载分区:

mount -o loop,ro,offset=4194304 ./PiOS.img /media/data/bak/img/boot
mount -o loop,offset=272629760 /media/data/bak/img/PiOS.img /media/data/bak/img/rootfs