Favorites
Virtual Server Virtual Machine Backup Script using VBS/VBScript

Link to Text File: vmbackups.txt

'================================================================
'External Requirements:
'VSS Software Development Kit (SDK)
'Robocopy.exe from Windows Server Resource Kit
'credits: http://redmondmag.com/columns/article.asp?EditorialsID=2324
'copy vshadow.exe from C:\Program Files (x86)\Microsoft\VSSSDK72\TestApps\vshadow\bin\release-server
'to the same folder as where you run this script
'Be Sure the Resource Kit has been added to the Path enviroment, test by opening a Command Prompt and typing robocopy.
'================================================================

'================================================================
'Warnings:
'
'If your VM is a Database Server, such as MS SQL Server, or a
'Domain Controller, you want to Shut the VM down instead of Saving.
'The reason is that you cannot always restore the Saved State of the VM
'which puts you at risk of DB corruption.
'================================================================

'================================================================
'Check out my new HyperV Backup Script at http://www.czerno.com/html/windows/Backup_HyperV_Virtual_Machines.asp
'================================================================

On Error Resume Next

Set objShell = CreateObject ("WScript.Shell")
set objFSO=CreateObject("Scripting.FileSystemObject")
Set virtualServer = CreateObject("VirtualServer.Application")

'****************************************************************
'Begin User Definable Section
'****************************************************************

'================================================================
'Load current date (formatted as mm-dd-yyyy)
'into variable strDate
'================================================================
strDate = Month(Now) & "-" & Day(Now) & "-" & Year(Now)

'================================================================
' Path to backup VMs to, can be UNC or Local
' Local Path Exmaple: "E:\VM Backups"
' UNC Example: "\\<servername>\<Share Name>\<Path Name>"
'================================================================
strBackupDir = "\\<servername>\<Share Name>\<Path Name>"

'================================================================
'Drive Letter, plus colon, where VMs are located
'================================================================

'================================================================
'Be sure this drive is limited to only VMs as everything will
'be created in the Shadow Copy.
'It's ok if there is more, but it will take longer and use more
'disk space to create the Shadow Copy.
'There is a chance, if you are linited in space, not all the VMs
'will be in the Shadow Copy.
'DO NOT USE System Drive, usually C:
'strVMdrive = "D:"
'================================================================
strVMdrive = "D:"

'================================================================
'Subfolder of the Drive entered above, where VMs are located, example given equates to D:\Virtual Machines
'================================================================
strVMfolder = "Virtual Machines"

'================================================================
'Free Drive Letter, plus colon, to mount the Shadow Copy
'================================================================
strTempDrive = "V:"

'================================================================
'Log File Folder, don't forget the trailing backslash; D:\bin\vmbackuplogs\
'================================================================
strLogFolder = "D:\bin\vmbackuplogs\"

'================================================================
'RoboCopy Log File, only change the portion in quotes
'================================================================
strRoboLog = strLogFolder & strDate & "vmbackuprobocopy.log"

'================================================================
'VBScript Log File, only change the portion in quotes
'================================================================
strVBSLog = strLogFolder & strDate & "vmbackupvbscript.log"

'create Log File folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder(strLogFolder)

'================================================================
'Email From address
'================================================================
strEmailFrom = "youremail@domainname.com"

'================================================================
'Email to address
'================================================================
strEmailTo = "youremail@domainname.com"

'================================================================
'SMTP Server
'================================================================
strSMTP = "smtp.server"

'****************************************************************
'End User Defined Section
'****************************************************************

'#############################################################################
'Start the Backup Process
'#############################################################################

'=============================================================================
'Setup VBS Log File
'=============================================================================

Set objFSO = createobject("scripting.filesystemobject")
Set objStream = objFSO.OpenTextFile(strVBSLog,2,true)
objStream.Writeline((Now))
objStream.Writeline(" ")

'=============================================================================
'Save Running VMs
'=============================================================================
For each objVM in virtualServer.VirtualMachines

objStream.Writeline((Now) & " --- " & "If " & objVM.Name & " is running, Saving State")

If objVM.State = 5 then
objStream.Writeline((Now) & " --- " & objVM.Name & " is Saving")

objVM.Save
objStream.Writeline("(Now) & " --- " & Loop whilte waiting for " & objVM.Name & " to save")

while objVM.State <> 2
objStream.Writeline((Now) & " --- " & objVM.Name & " is still Saving")
WScript.Sleep 5000

wend
Else
objStream.Writeline((Now) & " --- " & objVM.Name & " is not running")

End If
Next


'=============================================================================
'Create Shadow Copy cmds
'=============================================================================
objStream.Writeline((Now) & " --- " & "Create Shadow Copy cmds")
sExCmd = "CreateVSS.cmd"
Set oFileSys = CreateObject("Scripting.FileSystemObject")
if oFileSys.FileExists(sExCmd) then oFileSys.DeleteFile(sExCmd)
set oExCmd = oFileSys.CreateTextFile(sExCmd, CopyOverwrite)

'=============================================================================
'Create Shadow Copy
'=============================================================================
objStream.Writeline((Now) & " --- " & "Create Shadow Copy")
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,1, TRUE)


'=============================================================================
'Start Saved VMs
'=============================================================================
For each objVM in virtualServer.VirtualMachines
objStream.Writeline((Now) & " --- " & "If " & objVM.Name & " is saved, Starting VM")

If objVM.State = 2 then

objVM.Startup
objStream.Writeline((Now) & " --- " & objVM.Name & " is Starting")

while objVM.State <> 5
objStream.Writeline((Now) & " --- " & objVM.Name & " is still Restoring")
WScript.Sleep 5000
wend
End If
Next

For each objVM in virtualServer.VirtualMachines

If objVM.State = 1 then
objStream.Writeline((Now) & " --- " & objVM.Name & " is Turned Off")

If objVM.Autostartatlaunch = 1 then
objStream.Writeline((Now) & " --- " & objVM.Name & " is Turned Off but set to AutoStart at Host OS Boot")

objVM.Startup

while objVM.State <> 5
objStream.Writeline((Now) & " --- " & objVM.Name & " is still Starting")
WScript.Sleep 500
wend
End if

End If
Next

Set objVM = Nothing


'=============================================================================
'RoboCopy the VMs from Shadow Copy and wait for completion
'=============================================================================
objStream.Writeline((Now) & " --- " & "RoboCopy the VMs from Shadow Copy and wait for completion")
Set objExecObject = objShell.Exec("cmd /c robocopy.exe """ & strTempDrive & "\" & strVMfolder & """ """ & strBackupDir & """ /e /r:10 /w:60 /nfl /ndl /log:" & strRoboLog & "")

Do While objExecObject.Status <> 1
WScript.Sleep 5000
Loop

'=============================================================================
'Delete the Shadow Copy
'=============================================================================
objStream.Writeline((Now) & " --- " & "RoboCopy is complete, deleting Shadow Copy")
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,1, TRUE)


'=============================================================================
'Backup should now be finished, Close VBS Log File and Email both Log Files
'=============================================================================
objStream.Writeline((Now) & " --- " & "Backup is finished, Email Log File")
objStream.Writeline((Now) & " --- " & "Backup is done, dude!")
objStream.close

Set objEmail = CreateObject("CDO.Message")
objEmail.From = strEmailFrom
objEmail.To = strEmailTo
objEmail.Subject = "VM Backups"
objEmail.AddAttachment strRoboLog
objEmail.AddAttachment strVBSLog
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
StrSMTP
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send