In the early days of the Raspberry Pi I mostly used 4 or 8 GB SD-cards to run a system. These days however, 16 GB cards seem to be the smallest and for an Owncloud emergency backup server I’m even using a 64 GB SD-card now. While creating backup image files of 4 or 8 GB SD-cards was quick and didn’t take too much space, creating an image file of the 64 GB SD-card is somewhat of a stretch. Fortunately, there is a way to create and restore images of individual partitions which makes the process a lot quicker and much less storage intensive.
A Separate Data Partition
By default, a Raspian image consists of two partitions on the SD-card. One for the boot files with a size of around 50 MB and a second partition for the system which usually takes up all the rest of an SD-card’s flash memory.So far, so good.
In the case of my Owncloud backup server that uses a 64 GB SD-card I deviated from this scheme by assigning 5 GB to the system partition and the remaining 49 GB for a standalone data partition. As the data on the data partition is backed up separately a backup system image of the server only requires to create an image of the 50 MB boot partition and the 5 GB system partition. No image of the 49 GB partition is required as I would restore the data separately if I ever had to rebuild the server from the two backup partition images.
Creating A Backup
In a first step it’s a good idea to read and save the partition information of the SD-card on a PC in a text file as this information helps a lot during the restore procedure described below. On Ubuntu, sfdisk is the command of choice:
sudo sfdisk -d /dev/mmcblk0 # partition table of /dev/mmcblk0 unit: sectors /dev/mmcblk0p1 : start= 8192, size= 122880, Id= c /dev/mmcblk0p2 : start= 131072, size= 10240000, Id=83 /dev/mmcblk0p3 : start= 10373120, size=114362368, Id=83 /dev/mmcblk0p4 : start= 0, size= 0, Id= 0
mmcblk0p1 is the 50 MB boot partition, mmcblk0p2 is the 5 GB system partition and mmcblk0p3 is the 49 GB data partition.
To backup the boot and system partition into two files, two commands are required on Ubuntu:
sudo dd bs=4M if=/dev/mmcblk0p1 of=backup-pi2-mmcblk0p1.dd sudo dd bs=4M if=/dev/mmcblk0p2 of=backup-pi2-mmcblk0p2.dd
And that’s it, you are done!
Restore The Server On A Different SD-card
Restoring the server on another SD-card is much easier than I first imagined. If the new SD-card is of the same size as the old one, one can use the partition information taken above and write it directly to the new SD-card with the following sfdisk command:
sfdisk /dev/mmcblk0 < saved-parition-information.txt
If the new SD-card is smaller (e.g. only 16 GB to test the restoration procedure), remove the mmcblk0p3 and p4 lines from the text file before running the sfdisk command. An extra data partition can then be created manually later and is not even needed for a restore test as the server on the new SD-card will boot just fine without the data partition.
Once the sfdisk command is done, eject and re-insert the SD-card from the PC just to make sure the PC re-reads the partition table and then restore the boot and system partition as follows:
sudo dd bs=4M of=/dev/mmcblk0p1 if=backup-pi2-mmcblk0p1.dd sudo dd bs=4M of=/dev/mmcblk0p2 if=backup-pi2-mmcblk0p2.dd
The commands are the same as above with input and output inverted. Once the second dd command has finished it’s done, the SD-card can now be inserted into a Raspberry Pi and boots just fine.
But hold on, what about a Master Boot Record!?
Coming from the PC world, I was a bit surprised that this worked as the procedure doesn’t write an MBR on the SD-card which would be required if a PC with a BIOS wanted to boot from it. But a Raspberry Pi is not a PC and doesn’t require an MBR in the first sector of the SD-card. Instead the Raspi boot code searches for the first partition on the SD-card an then looks for a specific file there it uses for the boot process. For the details, have a look here.
So as you can see, backing up and restoring the boot and system partition on a large SD-card with a separate data partition is a piece of cake, much easier than I thought!