Suppose you have a ~250GB hard disk and a removable hard disk whose capacity is ~320GB, and you want to backup all the files from the hard disk to the removable disk. With “dd”, it is a very easy task. Again, suppose your hard disk’s Linux device name is /dev/sda and the removable disk is /dev/sdb. The following command can copy all the content from /dev/sda to /dev/sdb:
dd if=/dev/sda of=/dev/sdb
Here, if=… sets the source and of=… sets the destination. “dd” doesn’t care of the contents of the hard disk. It just reads bytes from /dev/sda and writes them into /dev/sdb. It doesn’t know what are files. So, the hard disk file system and how many partitions it has are not important. For example, if /dev/sda is splitted into three partitions, the /dev/sdb will have the same partitions.
Most of time you don’t want to make a complete duplication of your hard disk. You may prefer to creating an image file of the hard disk and save it in other storage devices. The following command will create an image file “disk1.img” in your user’s directory from /dev/sda:
dd if=/dev/sda of=~/disk1.img
Since you have created an image file, you can compress it with “gzip” or “bzip2”:
gzip disk1.img #generates disk1.img.gz
bzip2 disk1.img #generates disk1.img.bz2
Backing up a hard disk partition is much similar to backing up a whole hard disk. The reason is that Unix/Linux uses device name, such as /dev/sda1, /dev/sda5… to indicate the partitions. For example, if you want to create an image file from the first partition of /dev/sda, use “dd” like this:
dd if=/dev/sda1 of=~/disk2.img
By the way, you can copy a partition to another partition completely, just set “of” to the partition’s device name. For example:
dd if=/dev/sda1 of=/dev/sdb5
This command will copy all the contents from /dev/sda1 to /dev/sdb5. You must be sure that the capacity of /dev/sdb5 is larger than /dev/sda1.
To restore a partition or a hard disk from an image file, just exchange the arguments “if” and “of”. For example, restore the whole hard disk from the image file “disk1.img”:
dd if=disk1.img of=/dev/sda
Restore the first partition of /dev/sda from the image file “disk2.img”:
dd if=disk2.img of=/dev/sda1
How to do it by network ?
Netcat is also a really handy utility for cloning drives as it allows you to do the transfer over the network (make sure you have a fast connection) without putting copy-from and copy-to drives in the same machine.
On the machine you are copying from:
dd if=/dev/sda | nc 10.1.1.2 9000
On the machine you are copying to (assume IP is 10.1.1.2):
nc -l -p 9000 | dd of=/dev/sda