Image backups with netcat
This How-to applies to:
All
This How-to is intended for:
Desktop Power User
Transferring large blocks of information across the network between devices
Purpose
This is very useful when working with Virtual Machines or block devices in general. You often need to either transfer the contents of a large block device between machines, back up a block device, or make a copy of a block device.
Prerequisities
Two Linux systems with "netcat" installed.
Step by step
Let's assume you want to make a copy of an LVM partition on a different machine.
- Take a look at the source server and identify the partition you're looking to copy.
root@noded:~# lvs LV VG Attr LSize Origin Snap% Move Log Copy% hydra vols -wi-ao 10.00G mgm2 vols -wi-ao 4.00G relay vols -wi-ao 5.00G swap vols -wi-a- 512.00M zeo2 vols -wi-ao 40.00G
- Take "relay" in this instance, we will need a 5G partition on the target server. On the target server, create a new LVM partition with the following command;
lvcreate -L 5G -n relay_copy vols # in this instance; # 5G is the desired size (make sure it's large enough!) # relay_copy is the name of the target block device # vols is a volume group on the target system with at least 5G of free space ..
- Now set up a "listener" on the target system which will pipe it's output into your new block device.
netcat -l -p 8009 > /dev/vols/relay_copy # -l = listen # -p 8009 = on port 8009 # > /dev/vols/relay_copy = pipe anything that comes in to our new block device
- Now back on the source server, you can use netcat to pipe the contents of the block device out over the network
cat /dev/vols/relay | netcat <dest ip> 8009 # copy the contents of /dev/vols/relay to <dest ip> on port 8009
- Note that the netcat "may" not exit when it's finished, so you can use "vmstat" on the target machine to monitor disk traffic, when the traffic stops simply use "Ctrl-C" on the source system to terminate the process.
[Anyone know a sure-fire syntax that will make the "cat" command on the source system exit properly?]
Further information
See "man netcat" for more information.

