nixCraft: Linux Tips, Hacks, Tutorials, And Ideas In Blog Format
http://www.cyberciti.biz/
Copy hard disk or partition image to another system using a network and netcat (nc)
Posted by Vivek Gite <vivek@nixcraft.com> [24 Comments]
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 [2]) .
Make sure you have backup of all important data [3].
It is possible that nc may not be installed by default under Redhat / CentOS / Debian Linux.
Use yum command as follows:
# yum install nc
Output:
Loading "installonlyn" plugin Loading "rhnplugin" plugin Setting up Install Process Setting up repositories rhel-x86_64-server-vt-5 100% |=========================| 1.2 kB 00:00 rhel-x86_64-server-5 100% |=========================| 1.2 kB 00:00 Reading repository metadata in from local files Parsing package install arguments Resolving Dependencies --> Populating transaction set with selected packages. Please wait. ---> Downloading header for nc to pack into transaction set. nc-1.84-10.fc6.x86_64.rpm 100% |=========================| 6.9 kB 00:00 ---> Package nc.x86_64 0:1.84-10.fc6 set to be updated --> Running transaction check Dependencies Resolved ============================================================================= Package Arch Version Repository Size ============================================================================= Installing: nc x86_64 1.84-10.fc6 rhel-x86_64-server-5 56 k Transaction Summary ============================================================================= Install 1 Package(s) Update 0 Package(s) Remove 0 Package(s) Total download size: 56 k Is this ok [y/N]: y Downloading Packages: (1/1): nc-1.84-10.fc6.x86 100% |=========================| 56 kB 00:00 Running Transaction Test Finished Transaction Test Transaction Test Succeeded Running Transaction Installing: nc ######################### [1/1] Installed: nc.x86_64 0:1.84-10.fc6 Complete!
Simply use apt-get command [4]:
$ sudo apt-get install netcat
WARNING! These examples may result into data loss, ensure there are good backups before doing this, as using command wrong way can be dangerous.Our sample setup
-----------------------
HostA // 192.168.1.1
------------------------
sda
NETWORK
sdb
------------------------
HostB // 192.168.1.2
-------------------------
Your task is copy HostA /dev/sda to HostB's /dev/sdb using netcat command. First login as root user
You need to open port on hostB using netcat, enter :
# netcat -p 2222 -l |bzip2 -d | dd of=/dev/sdb
Where,
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.
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
As suggested by anonymous user:
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
Updated for accuracy.
4000+ howtos and counting! Want to read more Linux / UNIX howtos, tips and tricks? Subscribe to our free daily email newsletter or weekly newsletter to make sure you don't miss a single tip/tricks. Alternatively, subscribe via RSS/XML feed.
Article printed from nixCraft: http://www.cyberciti.biz/
URL to article: http://www.cyberciti.biz/tips/howto-copy-compressed-drive-image-over-network.html
URLs in this post:
[1] Image: http://www.cyberciti.biz/tips/category/networking/
[2] karl: http://www.meme.com/
[3] backup of all important data: http://www.cyberciti.biz/tips/perform-backups-for-the-linux-operating-system.html
[4] apt-get command: http://www.cyberciti.biz/tips/linux-debian-package-management-cheat-sheet.html
[5] Linux port scanning with netcat (nc) command: http://www.cyberciti.biz/faq/linux-port-scanning/
[6] nmap command for port scanning: http://www.cyberciti.biz/tips/linux-scanning-network-for-open-ports.html
[7] A good collection of netcat command usage: http://www.terminally-incoherent.com/blog/2007/08/07/few-useful-netcat-tricks/
[8] Another good netcat disk image howto: http://www.novell.com/coolsolutions/feature/19486.html
Click here to print.
Copyright © 2004-2010 nixCraft. All rights reserved. This print / pdf version is for personal non-commercial use only. More details - http://www.cyberciti.biz/tips/copyright