January 28, 2010

HOWTO: Backup an Entire Hard Disk to a Smaller One

In my book, one of the best ways to back up an entire disk is to simply dump an image of it to external media. For instance, let's say you have an OS installed on your workstation and you want to try a new Linux distribution. You could repartition the drive, mess with the bootloader, and install the new OS next to the old one. Then, if you don't like it, nuke the new partition, resize the old one, and fix up the MBR to point back to the old OS. This is certainly a common scenario, but it carries with it the substantial possibility that something will go wrong. You could lose your main OS and have to start over from scratch. Or you might end up spending hours trying to recover your old OS that you didn't properly back up before starting the procedure. Because hey, you're a Linux superstar by now, who needs backups anyway?

It's much simpler to just boot from a live CD and copy the entire disk image (partition table and all) to a file on external media with dd(1). When done with your experimentations, simply copy it back. This way you don't have to muck about with partitions, permissions, metadata, bootloaders, or anything. It takes a bit of time, but it is exceedingly simple and much less risky than other backup and restore methods. Here's an example. Assume that /dev/sda is the main disk in the machine that you're backing up and /dev/sdb1 is a partition on external storage.

mkdir /mnt/sdb1
mount /dev/sdb1 /mnt/sdb1
dd if=/dev/sda of=/mnt/sdb1/sda.img

This works great, but one obvious drawback is that the free space on sdb1 must be greater than (or by freakish coincidence, exactly equal to) the total size of /dev/sda. Unless, perhaps, we compress it. Then the minimum free space requirement on /dev/sdb1 becomes much lower. In effect, required space is equal to the amount of actual data on /dev/sda after it's been compressed. For example, if /dev/sda is 80GB in size, but the df(1) command reports that only about 10GB of it is in use, it's fair to guess that a compressed image of the disk should end up somewhere under 10GB in size.

So, there's no theoretical reason this couldn't work. But we have to recognize that the free space must compress well in order to get this kind of efficiency. Except in very specific circumstances, the "free" space on a hard disk is not actually empty. When you delete a file, the data is still there; the OS simply removes the references to the file. This data will be included in a dumped image of the disk and it may or may not compress well.

The solution is to fill up the free space with zeros. When it comes down to it, there's nothing that compresses better than a whole lot of nothing. For best results, this should be done on all file systems in addition to the swap partition.

The Backup Procedure

Mount the main disk and fill up the remaining free space on all partitions with a file containing all zeros. (/dev/sda1 is the only data partition in this example.)

mkdir /mnt/sda1
mount /dev/sda1 /mnt/sda1
dd if=/dev/zero of=/mnt/sda1/zero

Wait until you get the error "No space left on device", then remove the zero file on each partition.

rm /mnt/sda1/zero

Umount the partition.

umount /mnt/sda1

Zero the swap partition. (Shown as /dev/sda5 here.)

dd if=/dev/zero of=/dev/sda5

Reinitialize the swap partition. (Otherwise it won't be detected and enabled on boot after restore.)

mkswap /dev/sda5

Mount the backup partition.

mkdir /mnt/sdb1
mount /dev/sdb1 /mnt/sdb1

Copy the entire disk to an image file on the backup drive (/dev/sdb1), piping it through gzip for compression.

dd if=/dev/sda | gzip - > /mnt/sdb1/backup-sda.gz

Umount the backup device, reboot, and you're free to do whatever you like to your main disk, knowing that you have a good backup on hand.

The Restore Procedure

Restoring is even easier than backing up. Once you're up and running on the live CD:

mkdir /mnt/sdb1
mount /dev/sdb1 /mnt/sdb1
gunzip -c /mnt/sdb1/backup-sda.gz | dd of=/dev/sda

Unmount the backup drive, reboot, and you should have your original OS back just as it was before the backup.


Omer Ahmed said...

Hello sir,
I just didn't understand, the swapping part, and what is sda5 ?

charles said...


If you're not familiar with swap partitions in Linux, best to read up on it: https://www.linux.com/news/software/applications/8208-all-about-linux-swap-space/