Raspberry Pi Backup & Restore from macOS

Salvador Guerrero
3 min readSep 21, 2023

TLDR;

# Get the disk name
$ diskutil list

# Umount the disk
$ diskutil umountDisk /dev/disk5

# Backup
$ sudo dd if=/dev/disk5 status=progress bs=100M | gzip -c > /Volumes/drive_name/folder/backup.img.gz

# Restore
$ gunzip -c /Volumes/drive_name/folder/backup.img.gz | sudo dd of=/dev/disk5 status=progress bs=100M

The Story

I configured my Raspberry Pi to run a Bitcoin node plus a lightning node on the same machine, I spent quite a bit on installation and configuration to have it just as I wanted it, and the next logical step is to create backups of the microSD and my external drive.

I didn’t use a raid external drive because I wanted it to be the most basic setup possible, a 2T small external drive, plus a 512 GB microSD.

In addition to these full backups, I have it automatically backing up the important state information of my lightning node channels and LNbits state databases.

So in reality, the purpose of these full backups is to not have to install and configure everything from scratch if a catastrophe happens. I will still have to complement these backups with the additional recurring state backups.

For my backups, I decided to create the images and compress them at the same time, with the magic of the terminal this is possible. But before I start running any backup command I have to know the source disk.

After inserting the microSD or external drive, open a terminal and run the following command:

$ diskutil list

The above command will list all the disks in your drive, identify your external drive and copy it’s path. Don’t use the drive names that have suffixes with letters and numbers (i.e. /dev/disk5a1), use the topmost drive name (i.e. /dev/disk5)

Now that the drive is identified, umount it using diskutil, in my case /dev/disk5

$ diskutil umountDisk /dev/disk5

Next is to run the dd command with gzip,

  • dd — is to create the image from a drive
  • gzip — to compress the image

If we didn’t use gzip to compress the image, the image size would be as large as the drive and we don’t want] that.

$ sudo dd if=/dev/disk5 status=progress bs=100M | gzip -c > /Volumes/drive_name/folder/backup.img.gz

The above command will create a clone image from disk5, pipe it through gzip to compress the image and write it out to another external drive attached to my computer.

When I created an image with bs=10M it took a long time, but when I created an image with bs=100M it was much faster, play around with the numbers and see what fit best for your Mac hardware.

Next is the restore command, I did actually run a restore on another microSD card I purchased in advance, the same brand and size (it was on sale), the only error I made was that I didn’t specify the bs parameter and the restore took 16+HRS, next time I think I will increase the bs parameter to 100 and see if that also works for restoring images correctly.

Before running the restore command use the above commands to get the drive name and umount the drive. Now you’re good to proceed with the restore:

$ gunzip -c folder/backup.img.gz | sudo dd of=/dev/disk5 status=progress bs=100M

Yes, the restore did work, and that restored microSD card is where I’m running my Bitcoin and lightning nodes at this moment.

That’s it, I hope this is useful for you. 🍻

References

--

--

Salvador Guerrero

Computer Science Engineer, Cross-Platform App Developer, Open Source contributor. 🇲🇽🇺🇸