Virtualization Pro

Feb 4 2008   3:23PM GMT

Pre and post backup scripts for VMware Server 1.0.4

Posted by: Akutz
Andrew Kutz

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.

for F in $(grep "config" /etc/vmware/vm-list | sed "s/\(config \)\(.*\)/\2/g")
        VMCFG=$(echo $F | tr -d \")
        VMSTATE=$(/usr/bin/vmware-cmd $VMCFG getstate)

        echo -n "$VMCFG - $VMSTATE"
        if [ $(echo $VMSTATE | grep on) ]
                echo $VMCFG >> /tmp/vms_to_be_resumed
                echo " - suspending"
                /usr/bin/vmware-cmd $VMCFG suspend

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.

for F in $(cat /tmp/vms_to_be_resumed)
        VMCFG=$(echo $F | tr -d \")
        VMSTATE=$(/usr/bin/vmware-cmd $VMCFG getstate)
        if [ $(echo $VMSTATE | grep suspended) ]
                echo "$VMCFG - $VMSTATE - resuming"
                /usr/bin/vmware-cmd $VMCFG  start
rm /tmp/vms_to_be_resumed

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!

1  Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Jay Dog
    So has anyone actually written a script for Windows yet Andy?
    5 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: