Since we have a standalone VMWare ESX Server, we’re not using the consolidated backup (which doesn’t really sound like it would achieve what I want in a backup system anyway). Instead, every night I export all of the Virtual Machine disk and configuration files off to another server.
This way, if the ESX Server and it’s storage cluster completely fails, it is a simple task to rebuild the server and recover the disk images. It is an unlikely occurrence, especially if you’re using external storage such as a NAS or SAN device, but it might just save my bacon.
The script that I use to backup the virtual machines is good. It creates a snapshot of any virtual machine and then exports that snapshot to disk. The problem with the script is that it doesn’t always delete the snapshot even though it’s supposed to. Then, when it next attempts to backup a virtual machine it fails as the existing snapshot conflicts.
So, I came up with a small script to remove and consolidate all snapshots for each running virtual machine prior to the backup. Since I’ve implemented this script to run before the backup script, the problem has gone away. Note that this script is intended to run on VMWare ESX Server 3.5 and is unlikely to work on 3.5i as it does not have the Linux interface. It may work on older versions of ESX Server but it may require some minor changes to do so.
I’ll explain the script bit by bit:
for virtualMachine in $(/usr/bin/vmware-cmd -l) do ... done
This do loop runs through all of the virtual machines that are found by the list command. Next, only limit ourselves to virtual machines that are running (you could skip this step if you wanted to remove snapshots from all virtual machines):
if /usr/bin/vmware-cmd $virtualMachine getstate | grep -q "on" then ... fi
The if statement queries the ESX server to see if the virtual machine is running. If it is running, we’ll next check if it has any snapshots we want to consolidate and remove:
if /usr/bin/vmware-cmd $virtualMachine hassnapshot | grep -q "1" then ... fi
The final thing to do is to actually remove the snapshots:
/usr/bin/vmware-cmd $virtualMachine removesnapshots
Putting it all together, our script looks like this:
for virtualMachine in $(/usr/bin/vmware-cmd -l) do if /usr/bin/vmware-cmd $virtualMachine getstate | grep -q "on" then echo $virtualMachine is running... if /usr/bin/vmware-cmd $virtualMachine hassnapshot | grep -q "1" then echo $virtualMachine has a snapshot, removing... /usr/bin/vmware-cmd $virtualMachine removesnapshots else echo $virtualMachine has no snapshots. fi fi echo done
It’s a pretty simple script which you could possibly tart up by logging to /var/log/ or somewhere else. You could even have it email you with the results. For our purposes, though, it’s worked a treat and was a fun re-introduction into shell scripting since I haven’t done it for years.
No comments, make a comment »


Friday October 10, 2008 - 1:59 pm (1 month, 1 week ago)