2014-03-22

How to backup your Raspberry Pi SD-Card

In the following I will explain on how you can backup your Pi's SD-card, so that whenever it breaks down, you can simply restore the backup image to a fresh SD-Card. This may also help with tinkering, when you totally screwed up your Pi's Linux-installation. I will explain the first step specifically for Mac OS X, but you can do this similarly on other UNIXes and Linux, by using the mount and umount commands.

So, shut down your Pi using the command sudo shutdown -h now and remove the SD-card after the Pi has done so. Take the card and insert it into your Mac's SD-card slot.

First of all, we need to find out which disk device has been assigned to the card. We can do this with diskutil list:

user@mymacintosh:~ $ diskutil list                                                                
/dev/disk0                                                                                            
   #:                       TYPE NAME                    SIZE       IDENTIFIER                        
   0:      GUID_partition_scheme                        *512.1 GB   disk0                             
   1:                        EFI EFI                     209.7 MB   disk0s1                           
   2:                  Apple_HFS M4                      511.1 GB   disk0s2                           
   3:                 Apple_Boot Recovery HD             784.2 MB   disk0s3                           
/dev/disk1                                                                                            
   #:                       TYPE NAME                    SIZE       IDENTIFIER                        
   0:     FDisk_partition_scheme                        *15.9 GB    disk1                             
   1:             Windows_FAT_32                         58.7 MB    disk1s1                         
   2:                      Linux                         15.9 GB    disk1s2                           

Then unmount the card using diskutil unmountDisk:

user@mymacintosh:~ $ diskutil umountDisk /dev/disk1                                              
Unmount of all volumes on disk1 was successful                                                     

Next we will use dd to produce an image, which we can compress using bzip2, or pbzip2 for more performance. You can use MacPorts to install pbzip2.

user@mymacintosh:~ $ sudo dd if=/dev/disk1 | pbzip2 > raspberry_pi_$(date "+%Y-%m-%d").img.bz2 
31116288+0 records in                                                                                                           
31116288+0 records out                                                                                                        
15931539456 bytes transferred in 2347.692010 secs (6786043 bytes/sec)                                                           

Restoring the image is also easy:

user@mymacintosh:~ $ bunzip2 raspberry_pi_2014-03-22.img.bz2 | sudo dd of=/dev/disk1      

2 comments:

  1. Depending on the SD Card you use, you can improve the performance of the dd command by setting the blocksize parameter to something more than the 512Byte default. On my Macbook I had the best results with a blocksize of about 8M: dd if=/dev/disk1 bs=8M

    ReplyDelete
  2. Thanks. I would have assumed that today's OSes have so many abstraction layers that block size doesn't matter anymore!

    ReplyDelete