How to backup disk partitions

Hard disks come in various sizes, 20GB (Giga Bytes), 40GB, 80GB, 160GB and so on. For our convenience and for the ease of managing data that we store on our hard disks, we generally divide the hard disk into logical groups or known more appropriately and commonly as partitions. These partitions are just logical, for our use and convenience and do not imply or do anything to your hard disk physically.

Taking backup

On Linux

You can use the following command to take backup of an entire partition. 

$dd if=/dev/hda1 bs=1k conv=sync,noerror | gzip -c | ssh -c blowfish user@hostname "dd of=backup.gz bs=1k" 

The dd command is used to read a disk partition, if specifies the input file system the partition hda1, bs specifies the block size, conv=synch, noerror tells the dd command to write something even when it is not able to read any block on the hard disk partition, and we are piping this command to gzip in order to compress it and further piping it with the command ssh to save the image as filename.gz on a remote machine. 

If you wish to backup an entire hard disk and not just the partitions you can specify /dev/hda as the input file system. 

Backing up the MBR

$dd if=/dev/hda of=mbr_backup count=1 bs=512

This command will copy the first 512 bytes of your hard disk, which is where the MBR is stored along with the partition table. 

On Windows

There are several handy tools available for backing up partitions of entire hard disks across a network in neat ways. You will find more information about them on their sites.

Windows also provides a flexible way of creating backup images of your partitions. Open your My Computer and then right click on any of your partitions, select properties. On the tools section you will see the third option of backup.  It is very simple and basic from then onwards to select the destination path of the backup file and save it.

Windows Backup

Although this is very easy and comes within windows, the above mentioned utilities are quite elegant themselves, especially for the more advanced users and administrators who would like backing up the partitions of important servers remotely.

Restoring Data

On Linux

$dd if=backup.gz | ssh -c blowfish root@deadhost "gunzip -c | dd of=/dev/hda1 bs=1k"

The above command sets the input file system as the backup.gz file which we had created during backup, then ssh’s to the target remote machine, and then specifies the output files system as /dev/hda1 - the first partition which we had taken backup of, with bs - block size as 1k.

Restoring the MBR

$dd if=mbr_backup of=/dev/hda

The above command will reload the data in our backup file mbr_backup to the hard disk. Remember though this is a very risky thing to do so ensure that you have the correct backup file your restoring the MBR from. If alternatively you wish to restore only the MBR without the partition table, you may use the following command: 

$dd of=/dev/hda if=backup-of-hda-mbr bs=446 count=1 

Since the MBR without the partition table is of 446 bytes, this will restore just the MBR and not the partition table which follows.