Posted by: Akutz
Andrew Kutz, Virtualization
So I use BackupPC for a lot of my SMB backup needs. For my money it is the best open-source backup program hands-down. However, one of the problems that I have run into is when I backup my VM directories on VMware Server systems, the VMs sometimes go wonky because they do not like being backed up when they are running. So I need a way to suspend the VMs prior to a backup and resume them after a backup.
BackupPC implements what are called Pre and Post commands that allow an IT administrator to supply commands to be before and after a backup operation is to take place. The two Pre and Post commands we are concerned with are DumpPreUserCmd and DumpPostUserCmd. The BackupPC documentation explains how to use the Pre and Post commands to send a command to the server that is going to be, or just was, backed up via SSH. Now obviously this works great for Linux, UNIX, and OS X servers, but for an OS that lacks certain fundamental necessities like an SSH server, and I am of course talking about Windows, the burden is on the IT administrator to install one (it is possible to obtain a free SSH server for Windows).
So now that we know how to issue commands to a server before and after it is to be backed up, we need to be able to tell that server to suspend and then resume its VMs. However, it is not enough to simply issue a suspend and resume command to all VMs, there needs to be some logic involved. For example:
- The VM should only be suspended if it is running.
- The VM should only be resumed if it is suspended and was suspended as part of the backup operation.
Lucky for you I have written two bash scripts (Linux/Unix/OS X compatible) that do just that.
Note: The IFS variables have a letter O in them instead of the numeral 0 because WordPress keeps escaping the 0. So if you copy and paste the scripts, please replace the letter O with a number 0.
suspend_vms – This script will suspend all of the VMs currently running that are listed in the server’s VM list file. A note will be placed into a temporary file /tmp/vms_to_be_resumed that this VM was suspended as part of a backup operation. This temporary file will be read by the start_vms script in order to determine which VMs to resume.
#!/bin/bash IFS=$'\\O12' for F in $(grep "config" /etc/vmware/vm-list | sed "s/\(config \)\(.*\)/\2/g") do VMCFG=$(echo $F | tr -d \") VMSTATE=$(/usr/bin/vmware-cmd $VMCFG getstate) echo -n "$VMCFG - $VMSTATE" if [ $(echo $VMSTATE | grep on) ] then echo $VMCFG >> /tmp/vms_to_be_resumed echo " - suspending" /usr/bin/vmware-cmd $VMCFG suspend fi done echo
start_vms – This script will parse the contents of /tmp/vms_to_be_resumed and resume the VMs that are listed in the file and are currently suspended. The temporary file will be deleted at the end of this script’s execution.
#!/bin/bash IFS=$'\\O12' for F in $(cat /tmp/vms_to_be_resumed) do VMCFG=$(echo $F | tr -d \") VMSTATE=$(/usr/bin/vmware-cmd $VMCFG getstate) if [ $(echo $VMSTATE | grep suspended) ] then echo "$VMCFG - $VMSTATE - resuming" /usr/bin/vmware-cmd $VMCFG start fi done rm /tmp/vms_to_be_resumed echo
These scripts should allow you to successfully back up VMs running on VMware Server on a Linux server. Simply place them in an appropriate location on the server to be backed up that has VMs, such as /usr/local/bin, and use BackupPC’s pre and post commands to call them via SSH. If someone would like to write the equivalent scripts for Windows I would be happy to post those here as well (if I do not write them soon myself).
Hope this helps!