How to clone a drive over the network in Linux

In my embedded work I often need to clone disk drives, USB memory sticks, and miscellaneous other types of mass storage devices.  Here is a simple and easy way to clone/copy the primary hard disk of a source machine onto the primary hard disk of a destination without taking anything apart or using tertiary storage.  Of course, you may need to update drivers and system configuration on the destination machine after copying the source machine to it (if the hardware in each machine is not identical).

Assuming that your destination machine disk drive is as large as your source machine disk drive (or larger), you can copy the contents of the source machine drive directly to the target machine using the netcat (‘nc’) application. Since netcat uses the network, you may encounter problems if you’re running a firewall on either machine. If you are then either temporarily disable them or reconfigure them to allow traffic on whichever port you tell netcat to use. I typically use port 12345. For example, I needed to copy/clone the contents of the primary hard disk so I booted each machine with a live Linux CD and executed the following commands:

[Target Machine (assume IP address 192.168.0.10)]

# nc -l 12345 | gunzip - > /dev/sda

[Source Machine]

# cat /dev/sda | gzip - | nc 192.168.0.10 12345

And then I waited. This resulted in about 8MB/sec throughput (on average) over my 10MB/sec LAN, which is not bad. Saturation would be 10MB/sec so gzip was definitely improving performance since it was compressing at least 2MB/sec. It took a while to transfer, but I didn't have to use a tertiary mass storage device and I didn't have to worry about copying non-regular "files" such as device nodes and fifo's/pipes, which would normally require the use of tar or rar. I just started the process and let it run while I did other work.  I suspect it ran for a couple of hours, but I wasn't keeping track so I don't know for sure.

This method of cloning a drive completely ignores drive geometry and file systems so it will not work on all machine configurations, but it's likely to work for most and it's an easy set and walk away type of solution to an otherwise complex problem.

After using this method to clone the source machine, I used the Linux LVM utilities to resize the partitions and file system on the destination machine so no space was wasted.  This can also be done to NTFS partitions using a Linux live CD.

About these ads

, , , , ,

  1. #1 by David on February 3, 2012 - 8:48 am

    On the first command:

    nc -l 12345 | gunzip – > /dev/sda

    I needed to change it to:

    nc -l -p 12345 | gunzip – > /dev/sda

    so that on the source machine I didn’t get a CONNECTION REFUSED error. I don’t know if that reflects different versions of netcat or what, but that was my experience.

    Thanks for the article…it’s running right now and I’ll see if it works (soon I hope).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: