Tech Line

Virtual Server Backup Remix

Here’s a better way to back up Virtual Server 2005 virtual machines.

Chris: I just tried the VBScript you provided for backing up Virtual Server 2005 VMs (see my last column, "VM Backup Burden") and noticed a problem. The backup starts before the Virtual Server has completed its save function, which might be a bit dangerous for people who will depend on the backup. The script was an excellent start for me, who has many Virtual Servers to back up, and not a lot of scripting experience.
— Ev

Tech Help—Just An
E-Mail Away

Got a Windows, Exchange or virtualization question or need troubleshooting help? Or maybe you want a better explanation than provided in the manuals? Describe your dilemma in an e-mail to the MCPmag.com editors at mailto:[email protected]; the best questions get answered in this column and garner the questioner with a nifty MCPmag.com baseball-style cap.

When you send your questions, please include your full first and last name, location, certifications (if any) with your message. (If you prefer to remain anonymous, specify this in your message, but submit the requested information for verification purposes.)

Ev, thanks for pointing out this issue with the script. When it comes to backup, order is definitely important. The last time that I got bit by doing something out of order was when my wife turned on the power while I was in the middle of installing a ceiling fan. It’s much less shocking when power is turned back on after the installation is finished!

With larger VMs that take several seconds to several minutes to save their state, it’s critical that the save state process complete before the backup begins. This can actually be checked by querying the VM object’s State property. With backups, you don’t want to start a VM backup until the Save property equals 2. When queried, the State property will return a 1, 2, 5, or 7. Each value is associated with a particular state. Here is a breakdown between a state integer value and its corresponding meaning:

  • Stopped = 1
  • Saved = 2
  • Running = 5
  • Saving = 7

So before starting a backup, you must ensure that the State property equals 2. Here’s a modified version of my previous virtual server back-up script that uses a loop to ensure that the VM to be backed up is fully saved before proceeding with the backup.

Option Explicit
'Declare variables
Dim strVMName, strVMFolder, strToday
Dim strBackupFolder, strBackupFile, strVMState
Dim strJobName, strDescription, strNTBackupCommand
Dim objVirtualServer, objVirtualMachine, objShell
Dim errBackup
Const Saved = 2
Const Running = 5
Const Stopped = 1
Const Saving = 7

'Load current date (formatted as mm-dd-yyyy)
'into variable strToday
strToday = Month(Date) & "-" & Day(Date) & "-" & Year(Date)

'Define Script Variables
strVMName = "W2K3-SV"
strVMFolder = """H:\VMs\W2K3-SV"""
strBackupFolder = "H:\Backup\VMs"
strBackupFile = Chr(34) & strBackupFolder & "\" & _
   strVMName & "_" & strToday & ".bkf" & Chr(34)
'Chr(34) is used to include a quote in the string
strJobName = Chr(34) & strToday & " Full Backup" & Chr(34)
strDescription = Chr(34) & "Starting " & strToday &Chr(34)
strNTBackupCommand = "ntbackup backup " & strVMFolder & _
   " /J " & strJobName & " /D " & _
   strDescription & " /F " & strBackupFile

'Instantiate Virtual Server Object
Set objVirtualServer = _
   CreateObject("VirtualServer.Application")

'Instantiate Virtual Machine Object
Set objVirtualMachine = _
   objVirtualServer.FindVirtualMachine(strVMName)

'Save VM State
objVirtualMachine.Save()
strVMState = objVirtualMachine.state

'Wait for VM save state to finish
Do Until strVMState = Saved
   WScript.Sleep(5000) ' 5 second delay
   strVMState = objVirtualMachine.state

Loop

'Instantiate command shell object
Set objShell = WScript.CreateObject("Wscript.shell")

'Use ntbackup.exe to back up saved VM
errBackup = objShell.Run(strNTBackupCommand,1,True)

'Restart VM
objVirtualMachine.Startup()

Starting in line 8, I declared constants for each possible VM state. I use a Do Until loop to wait for the VM to finish saving before moving on to the backup portion of the script. Note that I also include a 5 second delay, to limit polling of the VM state to once every five seconds. You can customize the delay to whatever suits your interests.

Thanks, Ev, for keeping me on my toes. I hope that this script offers the solution you’re looking for.

About the Author

Chris Wolf is a Microsoft MVP for Windows --Virtual Machine and is a MCSE, MCT, and CCNA. He's a Senior Analyst for Burton Group who specializes in the areas of virtualization solutions, high availability, storage and enterprise management. Chris is the author of Virtualization: From the Desktop to the Enterprise (Apress), Troubleshooting Microsoft Technologies (Addison Wesley), and a contributor to the Windows Server 2003 Deployment Kit (Microsoft Press).learningstore-20/">Troubleshooting Microsoft Technologies (Addison Wesley) and a contributor to the Windows Server 2003 Deployment Kit (Microsoft Press).

comments powered by Disqus
Most   Popular