Wednesday, April 30, 2008

Copy hard disk or partition image to another system using a network and netcat

netcat is a swiss army knife in networking. It is designed to be a reliable "back-end" tool that can be used directly or easily driven by other programs/scripts, as well as a feature-rich network debugging and exploration tool, since it can create almost any kind of connection you would need and has several interesting built-in capabilities.

One handy and trusted use is to migrating data between two server hard drives. You can also use ssh for the same purpose, but encryption adds its own overheads.

A sample setup:

-----------------------
HostA // 192.168.1.1
------------------------
sda
NETWORK
sdb
------------------------
HostB // 192.168.1.2
-------------------------

To copy /dev/sda on HostA to /dev/sdb on HostB, first login as root.

On hostB (receiving end ~ write image mode), open port:
# netcat -p 2222 -l |bzip2 -d | dd of=/dev/sdb

Where,

  • -p 2222 : the source port, subject to privilege restrictions and availability. Make sure port 2222 is not used by another process.
  • -l : listen for an incoming connection rather than initiate a connection to a remote host.
  • bzip2 -d : Compresses image using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding. This will speed up network transfer ( -d : force decompression mode)
  • dd of=/dev/sda : The hard disk. You can also specify partition such as /dev/sda1

On hostA (send data over a network ~ read image mode), login as root:

# bzip2 -c /dev/sda | netcat hostA 2222 # OR use IP:
# bzip2 -c /dev/sda | netcat 192.168.1.1 2222

Apparently, this process takes its own time.

A note about latest netcat 1.84-10 and above: Above syntax will generate an error. It is an error to use -l in conjunction with the -p, -s, or -z. Additionally, any timeouts specified with -w are ignored. So use nc:

On hostA:
# nc -l 2222 > /dev/sdb
On hostB:
# nc hostA 2222< /dev/sda # Or
# nc 192.168.1.1 2222< /dev/sda

Using hostB, connect to the listening nc process at 2222 (hostA), feeding it the file (/dev/sda) which is to be transferred.
On hostA:
# nc -l 2222 | bzip2 -d > /dev/sdb
On hostB:
# bzip2 -c /dev/sda | nc 192.168.1.1 2222

No comments: