Back Up Microsoft Virtual Server Images…

Management, Productivity, Tips

This article first appeared at the now-closed Win32Scripting site. Since I find it useful, and my bookmark no longer works, I have been unable to reach its author, Jeff Trumbull, so I hope he doesn’t mind that I’ve archived it.

Author: Jeff Trumbull

Description:
Backup files that make a Microsoft virtual server with only about 1 minute of down time. Suspends the virtual server, takes a shadow copy , starts the virtual server then copies virtual server files. This could be used to copy any open files. Requires vshadow.exe from vss sdk.

Script:

On Error Resume Next

Set objShell = CreateObject ("WScript.Shell")
set objFSO=CreateObject("Scripting.FileSystemObject")
Set virtualServer = CreateObject("VirtualServer.Application")
DestBackupDir = "your backup path"
sExCmd = "CreateVSS.cmd"
Set oFileSys = CreateObject("Scripting.FileSystemObject")
if oFileSys.FileExists(sExCmd) then oFileSys.DeleteFile(sExCmd)
set oExCmd = oFileSys.CreateTextFile(sExCmd, CopyOverwrite)

For each objVM in virtualServer.VirtualMachines
     'See if vm machine is running. If so then do backup
     If objVM.State = 5 then
          'Save state the virtual machine
          set saveTask = objVM.Save
          'Loop waiting for task completion - and display status
          while not saveTask.isComplete
              WScript.Sleep 1000
          wend
               'Copy .VMC and .VSV files
               MyArray = Split(objVM.File,"\")
               Filename = MyArray(Ubound(MyArray))
               objFSO.CopyFile objVM.File,DestBackupDir & Filename
               MyArray = Split(objVM.SavedStateFilePath,"\")
               Filename = MyArray(Ubound(MyArray))
               objFSO.CopyFile objVM.SavedStateFilePath,DestBackupDir & Filename
          End If
Next
Set objVM = Nothing

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

' Start VM machine up from saved state
For each objVM in virtualServer.VirtualMachines
          'See if vm machine is Saved. If so then resume
           If objVM.State = 2 then
               'Start virtual machine
               objVM.Startup
           End If
Next

Set objVM = Nothing
WScript.Sleep 10000

If Result = 0 then
         'Loop through all vm machines
         For each objVM in virtualServer.VirtualMachines
            'See if vm machine is running. If so copy shadow backup of vm disk drives
             If objVM.State = 5 then
                  'Copy virtual hard disks and undo disks
                  For each vhd in objVM.HardDiskConnections
                       MyArray = Split(vhd.undoHardDisk.file,"\")
                       Filename = MyArray(Ubound(MyArray))
                       SourceName = "x" & Right(vhd.undoHardDisk.file,Len(vhd.undoHardDisk.file)-1)
                       wscript.echo vhd.undoHardDisk.file
                       wscript.echo SourceName
                       objFSO.CopyFile SourceName,DestBackupDir & Filename
                       MyArray = Split(vhd.HardDisk.file,"\")
                       Filename = MyArray(Ubound(MyArray))
                       SourceName = "x" & Right(vhd.HardDisk.file,Len(vhd.HardDisk.file)-1)
                       objFSO.CopyFile SourceName,DestBackupDir & Filename
                  Next
             End If
      Next
End If

' Shutdown all shadow copy instances
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)

'Script ends
wscript.echo "done"

Leave a Comment