Backing up and restoring Raspberry Pi SD card

This shows how to do it on MacOS. It’s quite straightforward using dd.

Backup
Remote way

If you prefer to do it remotely via SSH, use this command:

ssh root@your_rpi_ip "dd if=/dev/your_rpi_sd_card bs=1m | gzip -1 -" | pv | dd of=rpi.gz

your_rpi_ip can be obtained by hostname -I . if is followed by the your sd card path when mounted on RPi, which can be obtained by lsblk -p (make sure the disk you believe to be the SD card matches the size of your SD card!). pv is used here so that the progress of dd can be constantly checked. If you don’t have it, install it using brew install pv if you have homebrew installed. 

Depending on the size of your SD card and your connection speed, it could take some time. So go and get a coffee!

Local way (RPi end)

Similarly, you should be able to dd locally on RPi then scp the image to your PC/Mac. As a result, the SSH part will be stripped away:

dd if=/dev/your_rpi_sd_card bs=1m | gzip -1 - | pv | dd of=rpi.gz

After it’s done, you can use scp to transfer the image file to your PC/Mac.

Local way (PC/Mac end)

This is recommended since it’s faster due to the fact that the SD card is physically connected to your PC/Mac.

  1. Remove the SD card from your RPi and connect it to your computer.
  2. Check the disk name on your Mac using: diskutil list. Similarly, double check the disk size so that you don’t make a mistake.
  3. Same as the local way on RPi end: dd if=/dev/your_rpi_sd_card bs=1m | gzip -1 - | pv | dd of=rpi.gz.

Restoring

It’s also pretty straight-forward for the restoring. The steps are:

  1. Connect the SD card to your computer.
  2. Check the name of the disk using the method mentioned above
  3. Unmount (not eject) the SD card by diskutil unmountDisk /dev/disk_nr
  4. Burn the image file into your SD card using: gzip -dc yourRPiImage | pv | sudo dd of=/dev/disk_nr bs=1m

After another coffee, you can plug your SD card back into your RPi. Enjoy your back-to-life RPi.

P.S.

Instead of using pv, progress and watch can be used together to check the status. So run the above commands without pv, and open another terminal and run watch progress, you now even have a progress bar!