netcat utility (nc command) considered as TCP/IP swiss army knife. It reads and writes data across network connections, using TCP or UDP protocol. It is designed to be a reliable "back-end" tool that can be used directly or easily driven by other programs and scripts. At the same time, it is 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.
I also install the netcat package for administering a network and you'd like to use its debugging and network exploration capabilities.
One my favorite usage is to migrating data between two server hard drives using netcat over a network. It is very easy to copy complete drive image from one server to another.
You can also use ssh for the same purpose, but encryption adds its own overheads. This is tried and trusted method (hat tip to karl) .
Your task is copy HostA /dev/sda to HostB's /dev/sdb using netcat command. First login as root user
Command to type on hostB (receiving end ~ write image mode)
You need to open port on hostB using netcat, enter : # netcat -p 2222 -l |bzip2 -d | dd of=/dev/sdb Where,
-p 2222 : Specifies the source port nc should use, subject to privilege restrictions and availability. Make sure port 2222 is not used by another process.
-l : Used to specify that nc should 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 : /dev/sda is your hard disk. You can also specify partition such as /dev/sda1
Command to type on hostA (send data over a network ~ read image mode)
Now all you have to do is start copying image. Again login as root and enter: # bzip2 -c /dev/sda | netcat hostA 2222 OR use IP address: # bzip2 -c /dev/sda | netcat 192.168.1.1 2222
This process takes its own time.
A note about latest netcat version 1.84-10 and above
If you are using latest nc / netcat version above syntax will generate an error. It is an error to use -l option in conjunction with the -p, -s, or -z options. Additionally, any timeouts specified with the -w option are ignored. So use nc command as follows.
On hostA, enter: # nc -l 2222 > /dev/sdb On hostB, enter: # nc hostA 2222< /dev/sda OR # nc 192.168.1.1 2222< /dev/sda
Using a second machine (hostB), connect to the listening nc process at 2222 (hostA), feeding it the file (/dev/sda)which is to be transferred. You can use bzip2 as follows. On hostA, enter: # nc -l 2222 | bzip2 -d > /dev/sdb On hostB, enter: # bzip2 -c /dev/sda | nc 192.168.1.1 2222
You should definitely use bs=16M or something like that. Otherwise, the copy will take forever. Copying a 300 GB hard drive over a 1 Gbps cross-over cable took about 1 1/2 hours or so using bs=16M Without this option, the same thing would have taken about 7 hours.
In short use command as follows: # netcat -p 2222 -l |bzip2 -d | dd of=/dev/sdb bs=16M
첫댓글 netcat 으로 disk image 을 어떻게 복제하나?
http://www.novell.com/coolsolutions/feature/19486.html