Welcome to MSDN Blogs Sign in | Join | Help

A simple script to backup a Virtual Server

A common question is how to quickly and easily backup a virtual machine under Virtual Server.  There are a number of ways to do this - each of which have their own advantages and disadvantages. 

Today I am going to show you how to have a simple script that 'save states' a virtual machine, copies the virtual machines files, and restores the virtual machine.  The advantage of this approach is that it will work no matter what guest operating system you are running - however the disadvantages are that it will take the virtual machine offline for a period of time, and depending on the size of the virtual machine it may take a while to make the backup copy. 

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
wend

'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 ,
True
  
objShell.Run "%comspec% /c copy " & chr(34) & vhd.HardDisk.file & chr(34) & " " & chr(34) & WScript.Arguments(1) & chr(34),1 , True
next

'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
vm.Startup

'Script ends

See - easy.  One interesting scripting note (which I could not find anywhere else) is that the ",1,True" option at the end of the objShell.Run command tells the vbScript to wait until after the command is completed - which is critical for this script.

Cheers,
Ben

Published Friday, February 25, 2005 12:01 AM by Virtual PC Guy

Comments

Friday, February 25, 2005 1:42 AM by thomas woelfer

# re: A simple script to backup a Virtual Server

Ben,

will this also work with virtual pc somehow? i just checked and there doesn't seem to be a 'VirtialPC.Application'.... or doesn't vpc expose anything to automation ?

WM_QUERY
thomas woelfer
Friday, February 25, 2005 10:54 PM by Virtual PC Guy

# re: A simple script to backup a Virtual Server

Hi Thomas,

Unfortunately the COM API is only available with Virtual Server. There is no way to do this with Virtual PC today.

Cheers,
Ben
Friday, February 25, 2005 10:54 PM by Virtual PC Guy

# re: A simple script to backup a Virtual Server

Hi Thomas,

Unfortunately the COM API is only available with Virtual Server. There is no way to do this with Virtual PC today.

Cheers,
Ben
Saturday, February 26, 2005 4:16 AM by thomas woelfer

# re: A simple script to backup a Virtual Server

Ben,

ok, thanks anyway :-)

WM_CHEERS
thomas woelfer
Saturday, February 26, 2005 5:53 PM by Mark

# re: A simple script to backup a Virtual Server

If you could integrate this to use the Volume Shadow Copy service, then the downtime for each VM will be less than 1 minute.
Tuesday, March 01, 2005 12:15 AM by Per Østergaard

# re: A simple script to backup a Virtual Server

Why not simply use
set objFSO=CreateObject("Scripting.FileSystemObject")
objFSO.FileCopy "from", "to"
instead of shelling the copy?

Tuesday, March 01, 2005 12:19 AM by Virtual PC Guy

# re: A simple script to backup a Virtual Server

Hi Per,

Because I was too busy to figure out how to do this properly - thanks for the info.

Cheers,
Ben
Friday, March 18, 2005 2:03 AM by praful tarkar

# re: A simple script to backup a Virtual Server

hie,
i read ur script found it interesting and useful. I just wan more information on this since we r a group of five students who r planning to make a backup server.
so wd pleaze send us any inforation which u have.
waiting for ur reply...
paps........
bie.............
New Comments to this post are disabled
 
Page view tracker