Backing up a running Raspberry Pi remotely

It’s a good practice to back your Raspberries up regularly, especially if you’re using cheapo cards. However, I also don’t want to stop the box (it’s running my home DNS).

I could solve that on Raspbian with something like this (doom is the hostname):

ssh doom "sudo dd if=/dev/mmcblk0 bs=16M | pigz -" | dd of=doom-$(date +%Y-%m-%d-%H%M).iso.gz

This produces a compressed image of the card, e.g. doom-2022-01-08-1845.iso.gz. I opted for pigz for compression, to parallelize things a little bit (packing the image requires a lot of CPU and is usually a bottleneck).

To verify the backup is correct, we can mount the image locally (e.g. extract & run hdiutil mount doom-2022-01-08-1845.iso on macOS). Restoring it to a new SD card is possible with dd again, just as described in the usual Raspberry Pi preparation guides.

Make sure to set the correct block device. In my case, I dump the whole card (i.e. the mmcblk0 device), but copying a partition (e.g. /dev/mmcblk0p2) is also an option. However, the latter could complicate things for you if you want to restore the image with a plain dd, so I recommend dumping the whole device.

Also, keep in mind this image is unencrypted. Don’t forget to encrypt your backups!