adventures with microcomputers prototyping with raspberry pi

8. Backup Procedure

One can backup the entirety of their SD card (archiving applications, users, etc.) by simply reversing the procedure described here. Nonetheless, dd, the application that does the copying, is incapable of distinguishing between used and empty space on the SD card. More specifically, using dd alone for backups will result in a img file the size of one's SD card, which is ridiculous if your image is less than 2GB.

Below are steps to write the contents of a microSD card to a computer as a compressed .img file (often referred to as "making an image"):

  1. if you have not already done so, shut down your raspberry pi: sudo shutdown now
  2. remove the microSD card from the pi, insert it into an SD card adapter
  3. on your mac issue the following command in the terminal: diskutil list
  4. plug the SD card with the microSD into the SD card port on your mac
  5. repeat step 3, noting the disk number of the newly inserted SD card: /dev/disk<disk# from diskutil>
  6. unmount the disk from step 5: diskutil unmountDisk /dev/<disk# from diskutil>
  7. pipe the output of dd to gzip, archiving and compressing the image in one line, with the following: sudo dd bs=1m if=/dev/disk[DISK# FROM DISKUTIL] | gzip > rasbian.img.gz (this takes a VERY LONG TIME, so go get a cup of coffee or take a walk or something)

unarchiving with gunzip pi

If you archive with gzip you will need to run the following command to extract and restore the img to another SD card: gunzip --stdout rasbian.img.gz | sudo dd bs=1m of=/dev/<disk# from diskutil>

It is wise to make a habit of backing up your image regularly.