Backing Up Live Virtual Server VMs

Use this script to back up running Virtual Server 2005 R2 SP1 virtual machines.

One of my favorite Virtual Server 2005 R2 SP1 features is the included VS Writer, which leverages the Volume Shadow Copy Service (VSS) to back up running virtual machines.

In order to take advantage of VS Writer, you have two options:

  • Use backup software that supports VS Writer (note that support for shadow copies does not guarantee support for VS Writer).
  • Write a script that creates the shadow copy of the virtual machine files and then backs them up.

VS Writer does some great things when it is called. For instance, if the guest OS, such as Windows Server 2003, supports VSS, then VS Writer can talk to VSS inside a VM guest OS to properly prepare its applications for a shadow copy. Guest operating systems that do not support shadow copies, such as Linux guests, will be automatically suspended prior to the shadow copy, and then resumed once the shadow copy completes.

In the past, administrators had to manually suspend and resume all VMs to successfully create a reliable shadow copy. This approach is used in Jeff Trumbull's excellent script. Note that for shadow copy backups of virtual machines to work successfully, they all must have the latest edition of the Virtual Server VM additions installed inside their guest OS. For Linux VMs, please see Ben Armstrong's blog on the subject.

Backup software that supports VS Writer can back up running virtual server VMs by creating a volume shadow copy that contains each of the VMs' open files and then backing up the VM files associated with the shadow copy.

If your backup software does not support VSS and VS Writer, then you can still reliably back up Virtual Server VMs from the physical host using a vbscript. To back up running virtual machines, the script needs to perform the following tasks:

  1. Create a snapshot of server's volume that stores the virtual machines.
  2. Mount the snapshot to a temporary drive letter.
  3. Copy the virtual machine files to your preferred backup location -- either a locally mounted drive or a UNC path.

With that in mind, let's look at implementing a scripted solution. Steps 1 and 2 are the toughest, and rely on vshadow.exe to create and manage the shadow copy. Vshadow.exe is included in the VSS Software Development Kit (SDK). So, to script a live backup solution, you will first need to download the VSS SDK.

Once the download completes, the VSS SDK can be installed by following these steps:

  1. Run Setup.exe to install the VSS SDK.
  2. When the InstallShield Wizard opens, click Next.
  3. Click the "I accept the terms in the license agreement" radio button and click Next.
  4. Modify the software installation location, if desired, and click Next to continue.
  5. Click Install to start the installation.
  6. When the installation completes, click Finish.

You now need to create a folder on the virtual server system to store the script and its dependent vshadow.exe file. For example, in my lab I created a folder on the C drive named "scripts." Once the C:\scripts folder is created, copy the vshadow.exe and vshadow.pdb files from the "C:\Program Files (x86)\Microsoft\VSSSDK72\TestApps \vshadow\bin\release-server" folder to the "C:\scripts" folder.  Note that I deployed the VSS SDK on an x64 server. If your Virtual Server system is 32-bit, then the required vshadow files will be in the "C:\Program Files\Microsoft\VSSSDK72\TestApps \vshadow\bin\release-server" folder by default.

Next, you need to download and copy the vsbackup.vbs script to the C:\scripts folder. The vsbackup.vbs script is shown in the code here:


Set objShell = CreateObject ("WScript.Shell")
'Load current date (formatted as mm-dd-yyyy) 
'into variable strToday
strTime = Month(Now) & "-" & Day(Now) & "-" & Year(Now) &_
  "_" & hour(now) & "-" &  minute(now)
' Backup target folder or UNC path
strBackupDir = "E:\Backup\VS\" & strTime & "\"
'Drive containing Virtual Machines
strVMdrive = "D:" 
'VM folder path
strVMfolder = "VS"
'available drive letter used to mount shadow copy
strTempDrive = "X:"

'Prepare shadow copy commands
sExCmd = "CreateVSS.cmd"
Set oFileSys = CreateObject("Scripting.FileSystemObject")
if oFileSys.FileExists(sExCmd) then oFileSys.DeleteFile(sExCmd)
set oExCmd = oFileSys.CreateTextFile(sExCmd, CopyOverwrite)

'create backup folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder(strBackupDir)

' Create Shadow copy of VM drive
oExCmd.WriteLine "vshadow.exe -script=setvar1.cmd -p " &_
  strVMdrive
oExCmd.WriteLine "call setvar1.cmd"
oExCmd.WriteLine "vshadow.exe -el=%SHADOW_ID_1%," &_
  strTempDrive
oExCmd.Close
Result = objShell.run(sExCmd,vbMinimized, TRUE)

'Copy the virtual machine files from the shadow copy
strSource = strTempDrive & "\" & strVMfolder
objFSO.CopyFolder strSource , strBackupDir, TRUE

' Delete created shadow copy instance
if oFileSys.FileExists(sExCmd) then oFileSys.DeleteFile(sExCmd)
set oExCmd = oFileSys.CreateTextFile(sExCmd, CopyOverwrite)
oExCmd.WriteLine "Echo y | vshadow.exe -da"
oExCmd.Close
Result = objShell.run(sExCmd,vbMinimized, TRUE)

'Backup complete!
wscript.echo("Backup complete!")

Note that the script uses the shadow copy logic from Jeff Trumbull's backup script. Jeff's vshadow.exe scripting logic was spot-on, so I decided not to reinvent the wheel.

To use the script in your environment, you will need to modify the following variables:

  • strBackupDir
  • strVMdrive
  • strVMfolder
  • strTempDrive

strBackupDir should be set to the target location for the backup files. This could be a local mount such as "E:\Backup\VS" or a UNC path such as "\\fs1.redmondmag.com\vs1\backup." Note that you should only replace the first path defined by the variable, as the remaining portion of the variable definition is used to append a backup folder to the folder path that is named after the current date and time.

strVMdrive is the drive letter of the drive that stores the VM files. Since shadow copies occur at the volume level, it's most economical to back up all Virtual Server VMs on a given drive at the same time. If your virtual server system stores VMs on several drives, then you will need to maintain unique versions of the script for each volume that you need to back up.

strVMfolder should represent the complete folder path to the VM files. For example, on my lab system, the following folder structure is used:

D:\VS
|-----FC4
|       FC4.vmc
|       FC4.vsv
|       FC6.vhd
|
|-----W2K3
|       W2K3.vhd
|       W2K3.vmc
|       W2K3.vsv
|       W2K3.zip
|
|-----XP-Pro
        XP-Pro.vhd
        XP-Pro.vmc

So in my example, D is used for the strVMdrive variable and strVMfolder is set to "VS." If I had wanted to just back up the W2K3 VM, I could have set the strVMfolder variable to “VS\W2K3.”

strTempDrive is used to designate an available drive letter that will be used to mount the shadow copy during the backup. By default, the drive letter X is used.

The script creates a backup folder named after the date and time of the backup. The folder's contents will be identical to the source virtual machine file.

For example, a recent backup generated the following folder tree:

E:\Backup\VS\9-27-2007_11-20
|-----FC4
|       FC4.vmc
|       FC4.vsv
|       FC6.vhd
|
|-----W2K3
|       W2K3.vhd
|       W2K3.vmc
|       W2K3.vsv
|       W2K3.zip
|
|-----XP-Pro
        XP-Pro.vhd
        XP-Pro.vmc

The date/time naming of the folder allows the backup files to be automatically organized by the backup date and time.

The script will back up all files in the VM folders to the designated backup target folder. You could restore a VM to the time of the previous backup by copying the files in the VM's associated backup folder to their original source folder. Alternatively, you could mount a VM's virtual hard disk (VHD) file using VHD Mount in order to recover a single file.

Data Protection Manager is a great tool for backing up Virtual Server VMs and provides an excellent alternative to my scripted solution. However, if you're looking to integrate a scripted backup with your existing backup software or do not require the complete functionality of Data Protection Manager, then this script should provide just what you need.

While I wrote this script to solve a specific virtual server backup challenge, it can be used to back up any application that supports VSS. Hopefully it will help you to meet your virtual backup challenges.

comments powered by Disqus
Upcoming Events

Redmond Tech Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.