How to backup and restore a Raspberry Pi’s SD card

A quick solution to SD card corruption is to create a backup image of your SD card, so that if you face a SD card corruption again, you can simply burn that image to the card, put it in your Raspberry, and be back exactly where you were before the corruption.

In this article we’ll learn how to create an image of the Raspberry Pi’s SD card (or any SD card really) on Mac OS X.

I’ve experienced several occurrences of SD card corruption using a Raspberry Pi, mostly due to power losses. It can be frustrating to work on a project and suddenly your Raspberry is not booting anymore, and you have to re-image it an set it up again.

Hopefully your project is under source control; if not, well here is the lesson, keep all your projects on source control, and even better, keep multiple backups on different machines. Still, a lot of configuration steps and setting up of the Raspberry can’t be backed up, and will have to be done again. Making sure you have a backup image of your SD card is essential in ensuring you’re prepared for an issue.

Note: This article is for Mac OS X, but the process would be very similar for Linux users.

Create an image

  • Let’s not risk corrupting the SD card before we make an image of it. Instead of unplugging the power cord, shutdown the Raspberry cleanly by typing the following on the command line:

Once the Raspberry is powered off, remove the SD card and put it in your computer’s SD card reader.

  • Open the terminal on your computer, and list the devices: diskutil list. The device we’re looking for is the SD card. It should be FAT_32, and the size should be whatever size your SD card is (in my case, 7.9 GB since it’s a 8 GB card). Here, the SD card is device /dev/disk2:
SD card under diskutil list
SD card under diskutil list
  • Create an image of the SD card by typing:

Note that there will be no progress bar or any indication of the time left when you use dd. You can, however, get the current status by sending the signal SIGINFO to the process (Ctrl+T). This will display the current number of bytes transferred, which you can use to estimate the progress knowing the size of your SD card.

Restore a SD card

The procedure to copy an image to the SD card is the same as the one we saw in our article about setting up a headless Raspberry Pi.

  • ​List the devices with:

Let’s assume the device is /dev/disk2.

  • Unmount the SD card:

  • Copy the image to the SD card:

Here also, you can check on the progress using Ctrt+T.

  • Once it is finished, you can eject the SD card from your computer, put it in the Raspberry Pi and power it up. The system will be in the same state as it was when you created the image!

 

Make sure to check our other Raspberry Pi tutorials!

Be the first to comment

%d bloggers like this: