Backing up your VMs
Here’s the problem; you have several virtual machines running on your VMWare server. Each one is configured to back up some of its important state, but what about the machine itself?
Here’s a shell script I wrote to handle the situation.
-
-
#!/bin/sh
-
-
BACKUPDIR=/mnt/bignasdrive/vms
-
-
function backupvm
-
{
-
vm_config=$1
-
vm_path=`dirname "$vm_config"`
-
targetdir=$BACKUPDIR$vm_path
-
-
echo "Copying $vm_path to $targetdir"
-
mkdir –parents $targetdir
-
cp –update –verbose $vm_path/* $targetdir/
-
}
-
-
function suspendvm
-
{
-
vm_config=$1
-
-
echo Suspending $vm_config…
-
vmware-cmd "$vm_config" suspend trysoft
-
}
-
-
function resumevm
-
{
-
vm_config=$1
-
-
echo Resuming $vm_config…
-
vmware-cmd "$vm_config" start trysoft
-
}
-
-
function checkbackupvm
-
{
-
vmpath=$1
-
vmstate=$2
-
state=`echo $vmstate|awk ‘{print $NF}’`
-
if [ "$state" == "on" ]; then
-
suspendvm "$vmpath"
-
backupvm "$vmpath"
-
resumevm "$vmpath"
-
else
-
backupvm "$vmpath"
-
fi
-
}
-
-
function getvmlist
-
{
-
for b in ${VMs// /___} ;
-
do
-
vmstate=`vmware-cmd "${b//___/ }" getstate`
-
checkbackupvm "${b//___/ }" "$vmstate"
-
done
-
}
-
-
VMs=`vmware-cmd -l`
-
-
getvmlist
-
What the script does is
* Get the list of registered machines
* For each machine,
* if it’s running – suspend, backup, restore
* else backup
Much ofthe time was spent handling the completely silly idea of putting spaces in file names.
Note that you must probably change the backup target directory high up in the script. I mounted my ReadNAS+ with 1.5TB space for backups…
Put the script somewhere, make it runnable and symlink it into /etc/cron.daily or cron.weekly
chmod +x backupvms ln -s backupvms /etc/cron.daily
It is worth noting that I ran into problems as not all services on all machines handled the suspend/restore cycle well. The backing up worked just fine, though
–Jesper Högström