Basti’s Buggy Blog

Poking at software.

Create, Mount and Restore partclone Images

The Goal

Motivation: Space efficient binary backups of Raspberry Pi SD-cards

  • Create a binary image of a ext4 partition
  • The image should be small (e.g. 2GB/32GB occupied -> image size ~2GB)
  • The backup process should not require more storage than the image itself (at no point in the backup process)
  • The files inside the backup shall be accessible in a simple manner (for copying individual files)
  • (optional) It should be possible to restore the image to the partition which is smaller than the original partition

Requirements

How-To

Plugin the device containing the partition to be backed up (e.g. /dev/sdx).

Backup

Optional: Backup the MBR + partition table

sudo dd if=/dev/sdx bs=512 count=1 > ./mbr.img

Optional: Save partition layout

sudo sfdisk -d /dev/sdx > ./partition-table.txt

Optional: Partclone image backup of boot partition

sudo partclone.fat32 --clone --source /dev/sdx1 --output - > ./boot.pcl

Partclone image backup of root ext4 partition

sudo partclone.ext4 --clone --source /dev/sdx2 --output ./rootfs.pcl

Restore

Optional: Restore the MBR + partition table

sudo dd if=./mbr.img bs=512 count=1 of=/dev/sdx
# if restoring to a smaller disk, fix partition size:
sudo cfdisk

Optional: Restore partclone image of boot partition

sudo partclone.fat32 --restore --source ./boot.pcl --output /dev/sdx1

Restore partclone image of root ext4 partition. Use the -C flag to disable the disk size check to enable restoring to smaller partitions (untested, but it should work).

sudo partclone.ext4 --restore --source ./rootfs.pcl --output /dev/sdx2 -C

Mounting a partclone Image

The imagemount binary in the partclone-utils uses the network block devices API to provide access to the backups through a standard filesystem interface. This is done transparently, no further nbd utilities are required.

First we load the network block devices driver to create the devices /dev/nbd0, /dev/nbd1, …, which imagemount needs in order to mount the backup.

sudo modprobe nbd

Next, mount the actual image.

sudo imagemount\
    -v 3\                 # verbosity level 3
    -d /dev/nbd0\         # use the first network block device
    -f ./rootfs.pcl\      # our backup image
    -t ext4\              # our image contains a ext4 filesystem
    -m /media/imagemount\ # mount it here
    -D\                   # do not daemonize, keep in foreground
    -r                    # mount read only (write does not work anyways)

Now the filesystem contained in the backup should be accessible at the specified mountpoint as long as the imagemount process is active. To unmount the filesystem, simply terminate the process.

See Also