Complete Disk Backup

Published: by Creative Commons Licence

Time to read: 2 minutes

The Unix tool dd copies data at device level (not at file system level). With that you can create images of complete drives or restore them to a drive. The following examples were created under macOS, but under Linux it is almost the same.

Creating an image using an SD card as an example

  1. Insert SD card formatted with VFAT.

  2. show list of drives

     $ df -h
    
  3. Select the correct drive from the list. On the left are the Devices, Mountpoints are on the right. In the case of an SD card, e.g. /dev/disk5s2.

  4. Unmount device (Under Linux umount instead).

     $ sudo diskutil unmount /dev/disk5s2
    
  5. Copy image. It is important that here as the source (if) not only the partition (here /dev/disk5s2) is set, but the entire device (here /dev/disk5). The parameter of=\.... is the target image to be generated. The parameter bs=1m specifies to create a boot sector with 1 MB on the target image.

     $ sudo dd if=/dev/disk5 of=./any_name.img bs=1m
    
  6. To have a backup of any_name.img it would make sense to compress and encrypt the file, e.g. with gzip and gpg:

     $ gzip any_name.img
     $ gpg -c any_name.img.gz
     Passphrase: *************
     $ ls 
     any_name.img.gz.gpg
    

Restore the backup

  1. decrypt accordingly with gunzip and gpg without the -c Parameter.

     $ gpg any_name.img.gz.gpg
     Passphrase: ***********
     gunzip any_name.img.gz
     $ ls 
     any_name.img
    
  2. Insert a SD card formatted with VFAT.

  3. show list of drives.

     $ df -h
    
  4. Select the correct drive from the list. On the left are the Devices, Mountpoints are on the right. For an SD card for example you have on the left side /dev/disk5s2.

  5. Unmount device (Under Linux umount instead).

     $ sudo diskutil unmount /dev/disk5s2
    
  6. Restores the image. It is important to set the entire device (here /dev/disk5) as the target (of) but not only a partition (here /dev/disk5s2). The parameter if=... is the source image to read. The parameter bs=1m specifies that a boot sector with 1 MB is to be created in the target device.

     $ sudo dd if=./any_name.img of=/dev/disk5 bs=1m