Categories
Server admin

Backing up Raspberry Pi

Why!?!?

“Why!?!?” That’s what I was asking when my pi stopped working. After days of configuration and tinkering it just wouldn’t connect to the network anymore. I said to myself “Welp, I guess I’ll reinstall from scratch” and spent the next few hours installing and setting everything back up again.

Solution? Make a Backup using a Mac

Obviously this is what I should have done from the beginning. Now what’s the “right” backup solution?

The simplest one is to insert an identical sd card into a card reader and insert that into the USB port. And then running this command from the terminal while the pi is off. Replace /dev/mmcblk0 with the location of your pi installation drive, and /dev/sda with where you want to store it to.

On mac, you can see all the drives and their paths by using this command. Source: https://pbxbook.com/other/dd_clone.html

diskutil list

The output should inform you on what disk you should be copying from. If it says /dev/disk5, then use /dev/rdisk5 that is just prepend an ‘r’ in front of the drive name and it will work faster. For details see https://superuser.com/questions/631592/why-is-dev-rdisk-about-20-times-faster-than-dev-disk-in-mac-os-x

# Warning: Do not do this on a raspberry pi system that is powered on. It might work, but might not since your system may write stuff at the same time.
sudo dd if=/dev/rdisk5 of=/dev/sda bs=4M status=progress

If you’d like to save the data to a file on your computer, use the following command. When running this on my own system, it took a long time (about 25 minutes or so), and doesn’t provide any output while running, so be patient with it when it is running. Source: https://forums.raspberrypi.com/viewtopic.php?t=46911

sudo dd bs=4M if=/dev/rdisk5 | gzip > ~/image`date +%d%m%y`.gz

To restore to a disk, use the following code. Source: https://forums.raspberrypi.com/viewtopic.php?t=46911

sudo gzip -dc /home/your_username/image.gz | dd bs=4M of=/dev/sdb

Then at the end, I swapped the SD cards to verify that the backup worked and it did!

For the people willing to install more stuff

Additionally if you installed zstd and pv, you can use combine them for a small and fast file with estimation replace 60906M with your estimate of the transfer size or use it without the -s and 60906M to just have a progress bar without a time estimate:

sudo dd bs=4M if=/dev/rdisk5 | pv -s 60906M | zstd -3 > ~/ddpvomv64_image`date +%y%m%d`.img.zst

And to restore use:

sudo zstd -dc /Users/shawn/Downloads/omv16_duckdns_image240116.img.zst | pv -s 15700M | sudo dd bs=4M of=/dev/rdisk5

Thanks, Sources:

Others using dd: https://forums.raspberrypi.com/viewtopic.php?t=46911
To get progress working: https://askubuntu.com/questions/215505/how-do-you-monitor-the-progress-of-dd
For a more complete solution: https://github.com/RichardMidnight/pi-safe

Leave a comment