How to Backup and Restore Your Hard Disk by Creating a Disk Image

|

It’s been almost a year since I setup my personal server which I named Jarvis. I haven’t backed-up Jarvis at all and recently grew paranoid that something might happen to him. Jarvis represents hundreds of hours of work, and there are documents and code that are stored only in him.

I spent some time figuring out how to backup everything. The easiest solution is to initialize Git repositories for all my projects and then create bare Git repos for all of them. Then I simply tarballed them up and stored copies of them elsewhere.

But I wanted to also preserve all my server configurations and packages. I wanted a hard drive that if Jarvis’ hard disk suffered some catastrophic failure like fire, I could simply plug into another computer and have a perfect clone up and running in no time. Spending hours installing packages isn’t fun. What I needed was to take a snapshot of the disk and create a disk image. It turns out dd is the command for the job.

Jarvis is made of:

I needed:

Steps I took:

  • boot Jarvis from the bootable USB stick by configuring the BIOS
    • boot into BIOS mode by repeatedly pressing the F2 key right after powering on
  • find out which disk is the original and which one is the target of the backup by running sudo fdisk -l
    • sda is my original drive and sdc is my flash drive
  • make sure no partitions are mounted from the source hard drive
  • mount the external 64GB flash drive
    • mount -t hfsplus /dev/sdX /mnt/sdX where sdX is the drive and partition; sdc1 in my case
  • back up the drive
    • dd if=/dev/sda conv=sync,noerror bs=64K | gzip -c > /mnt/sdc1/sda.img.gz
  • Monitor the progress of dd by opening another terminal and running watch -n 5 'sudo kill -USR1 $(pgrep ^dd)
  • Store extra information about the drive geometry necessary in order to interpret the partition table stored within the image. The most important of which is the cylinder size.
    • fdisk -l /dev/sda > /mnt/sda1/sda_fdisk.info

Notes:

  • I tried using the FAT file system only to find out that the maximum file size is 4GB.

References:

Comments