Thursday, September 27, 2007

Backing Up Drive Images Over The Network

I tried out some useful network kung fu today with dd, buffer and netcat (thanks for the tip, Jeff!). You can backup an entire drive by creating an image of it on another machine. It's really easy to do, and it comes in real handy.

On the remote computer, you should have enough hard disk space to fit the image of the source computer's hard disk. Run the following on the remote computer:

$ nc -l -p 1234 > file.img

You can replace 1234 with a port number of your choosing (1024 and above if you're not root), and you can also replace file.img with any filename.

On the source computer, let's say we want to back up hard disk sda to the remote host with the IP address of We would run:

$ dd if=/dev/sda bs=64k | buffer -S 10M | nc 1234

The first command, dd will read /dev/sda 64k blocks at a time and pipe it into the program buffer, which in turn feeds it into netcat (nc). The destination is and the port number is 1234, since that is the number we used on the remote machine. The use of the buffer program is optional, it's meant to improve network performance.

Once it's done a neat little file called file.img will sit on the remote computer containing the disk image for the source computer's sda drive.

You can actually use the image file in programs such as Qemu, and boot the operating system on it (if it has one) in a virtual machine. Hard disk images are also useful for cloning the setup of an OS on multiple computers.

Note that if your hard disk contains an OS or software that have unfriendly proprietary licenses such as Microsoft Windows, it is probably a violation of some stick-in-the-mud license agreement to do this procedure. However if you're using a free OS such as Linux, you're in the clear.


sarnold said...

Which package contains buffer? why is it useful here?

Note that tar can be used to do subdirectories rather than entire drives, if you wish:

tar cf - /home/foo | ssh remotehost "cd blah; tar xf -"

Anonymous said...

Hi, I like the idea, but one question: Once you have the image file created by DD, how do you restore the image back to source machine?

Anonymous said...

Only problem with this is in the 'nc' man page:

for '-p': It is an error to use this option in conjunction with the -l option.

for '-l': It is an error to use this option in conjunction with the -p, -s, or -z options.