Virtual Server - Backup

script to backup virtual server guests using volume shadow copy

Source: http://cwashington.netreach.net/depo/view.asp?Index=1049&ScriptType=vbscript
Language:
VBScript
Keywords:
Virtual Server
Code Snippet

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"


Created 2012-02-06
comments powered by Disqus
Login