Continued digging of my old scripts has turned up another which I think may have some use. It's a script to backup all Xen VMs running on a remote host. I should explain that this ran between a backup host with plenty of space, and the Xen host, which contained a lab environment of several VMs.
I had also configured SSH keypair authentication between the two systems, as it largely relies on a string of SSH commands.
The script works as follows:
- Pause all guests running on the Xen host. (This helps to keep things between the hosts in sync)
- Takes each guest, and saves it in it's entirety on the local file system.
- Un-pause all the guests and show their status.
- Working through each of the guest backup files, it zips, transfers, and deletes the not needed copy.
After it's complete you have a backup for all your guests at a given time.
As an aside, I would like to say the Xen project has really matured recently (last two/three years as I write this). I can remember a time when it was very buggy, VMs would randomly freeze and it was a complete pain to set-up. I still feel the pain of regularly compiling custom Kernels for it on RHEL machines. They even tried to push it on me during my RHCE study and it was not well behaved then! It's not always my virtualisation tool of choice, but it does have some advantages today like speed of install, opensource freedom and scriptability. If you didn't like it before, it might surprise you!
The Script:
#!/bin/bash # # backup_lab.sh will log into the Dom0 machine containing the lab and # instruct it to backup and zip every image it finds. XENIP="10.1.1.1" GUESTS=$(ssh root@$XENIP "xm list | grep -Ev '(Name|Domain-0)'" | awk '{ print $1 }') DATE=$(date +%Y-%m-%d) # First Get the list of machines, pause them all then save them, and restore, in order echo "Working on Guests: $GUESTS" echo # Pause all the guests before we start. (Not sure how needed this is, just helps not to let them get out of sync) echo for g in $GUESTS do echo Pausing $g ssh root@$XENIP "xm pause $g" done echo "All Guests should now be paused" echo echo "Current Xen State:" ssh root@$XENIP "xm list" echo echo # Now unpause, then backup the guest, restore from its backup, and pause! for g in $GUESTS do echo "Backing up guest $g" echo ssh root@$XENIP "xm unpause $g" echo "Guest Un-paused for backup" echo echo "Saving Guest $g to /root/$g.bak" echo ssh root@$XENIP "xm save $g /root/$g.bak" echo "Guest $g Saved" echo echo "Restarting Guest" echo ssh root@$XENIP "xm restore /root/$g.bak" echo "$g has been restored, pausing" ssh root@$XENIP "xm pause $g" echo "$g has been paused" echo echo "Guest $g has been backed up." echo done # Un-pause all the guests echo for g in $GUESTS do echo Un-pausing $g ssh root@$XENIP "xm unpause $g" done # Display the VM list in case anyone is watching ssh root@$XENIP "xm list" # Now all the guests have a backed up file, we can zip and move these for g in $GUESTS do echo # Tell the remote end to compress echo "Compressing /root/$g.bak" ssh root@$XENIP "gzip /root/$g.bak" # get the remote file back to this directory echo "Collecting /root/$g.bak.gz" scp root@$XENIP:/root/$g.bak.gz /backup/lab/$g.$DATE.gz # Delete the remote backup echo "Deleting remote backup /root/$g.bak.gz" ssh root@$XENIP "rm -fv /root/$g.bak.gz" done echo "All Done!"
No comments:
Post a Comment