Scripting Backup of Virtual Machines on Virtual Server 2005

Many of you have asked how you can back up your Virtual Machines running on Virtual Server 2005.

One of the suggestions is as Virtual Server has an open scriptable interface that you use a script to put all of your Virtual Machines in to a Saved State, backup the .vhd, .vmc, undo and save state files and then restore your machines.

This script takes two command line parameters - the first one being the name of the virtual machine to backup, while the second one is the name of the path to backup to:

'Script Begins
On Error Resume Next

'Create Shell Object
Set objShell = CreateObject ("WScript.Shell")

'Connect to Virtual Server
Set virtualServer = CreateObject("VirtualServer.Application")

'Get virtual machine from command-line parameter
set vm = virtualServer.FindVirtualMachine(WScript.Arguments(0))

'Save state the virtual machine
set saveTask = vm.Save

'Loop waiting for task completion - and display status
while not saveTask.isComplete
WScript.Sleep 1000

'Copy virtual hard disks and undo disks
for each vhd in vm.HardDiskConnections
objShell.Run "%comspec% /c copy " & chr(34) & vhd.undoHardDisk.file & chr(34) & " " & chr(34) & WScript.Arguments(1) & chr(34),1 ,
objShell.Run "%comspec% /c copy " & chr(34) & vhd.HardDisk.file & chr(34) & " " & chr(34) & WScript.Arguments(1) & chr(34),1 , True

'Copy .VMC and .VSV files
objShell.Run "%comspec% /c copy " & chr(34) & vm.File & chr(34) & " " & chr(34) & WScript.Arguments(1) & chr(34),1 , True
objShell.Run "%comspec% /c copy " & chr(34) & vm.SavedStateFilePath & chr(34) & " " & chr(34) & WScript.Arguments(1) & chr(34),1 , True

'Once everything is done - startup the virtual machine

'Script ends

Let me know how you get on.


Comments (2)

  1. Don Newman says:

    This would be a lot better if it incrporated shadow copies to minimize the downtime.

  2. Damon says:

    Thanks for the inspiration! I used some of this code to create an environment similar to MS Virtual Labs in my own organization. There’s not much code at all out there for VS yet…

Skip to main content