Wednesday, September 2, 2009

Using the pipe + ssh to mirror a disk

I have a few odd jobs here and there.  One of which is being a remote freelance admin for a business' 2 co-located servers.  They run a few virtual machines using Xen.  They are in different places and have no shared storage.  I was asked to move one of the virtual machines from box A to box B.  The disks for the VM's are LVM partitions.

I created the new lvm partitions on Box-B with the same size as on the old server.  I shut down the virtual machine - and ran a variation of the following command.

box-a #> dd if=/dev/vg1/disk1 | gzip | ssh root@box-b "| gunzip | dd of=/dev/vg1/disk1"
Did you know that you can pipe standard i/o through ssh like this?  Lets break this down.

'dd' is a program for the direct copy and writing of data.  'if' is the 'input file' and 'of' is the 'output file'.  If no output file is specified it defaults to Standard Out, same for no input file, it defaults to reading from Standard In.  The block device is read, piped through gzip to compress it, then piped to ssh.  The quoted section after the ssh is what command to run on the remote host.  If a command is preceded by a pipe then ssh will take anything it gets from standard in, and direct it out to there.  In this case - 'gunzip' - to decompress the stream, then to 'dd' which then writes to the block device on the remote system.  The gzip/gunzip wasn't necessary but it sped things up since my bottleneck was the wan between the 2 hosts.  20 or 30 minutes later it was done.  I periodically did a 'killall -USR1 dd' in a different shell so dd would print it's progress.  Once this was completed I transferred the Xen configuration file to the new machine, made changes to it's IP and booted the VM.

0 comments: