VM-Box backup VBscript (with rotation)
Hi
I recently had to find a simple (cheap), but effective way to backup
non-critical vm-boxes (non-critical in the sense that they only have to
be available 99%). Here, I will make available a little script of mine, which
makes use of the VMCOM-Api (VM-Server, no ESX).
Motivation
Pure data-backup/restore usually is solved and unproblematic, however,
configurations and parametrisations of underlying software components often
are a pain, and, depending on their complexity, it takes hours up to days to
restore them in worst-case scenarios.
In an application service provider model, often customer-dedicated machines
are fully configured running on vm-boxes. So, by backing up the vm-boxes,
you actually are backing up all the necessary configurations and parametrisations.
Script
The script performs a rotational backup (4 weekdays, 5 weekends (fridays)).
Events as well as exceptions are written to the application event log.
If you are running the vm-server in a 64bit-windows environment, make
sure (VMCOM-Api does not support (yet) 64bit) to start the script in a 32bit-environment by using
Code:
%WINDIR%\SysWOW64\cmd.exe /c VMBackup.vbs
in your at or scheduler.
Further pro/co's are up to you. The script has its value in certain
environments, but none in others. Decide yourself :)
If you have any suggestions/ideas, please let me know.
Cheers
Code:
''''''''''''''''''''''''''''''''''''''''''''
' Settings general
''''''''''''''''''''''''''''''''''''''''''''
VMBoxDirectory = "Z:\VMBoxDirectory\" ' Backslash necessary
VMBoxDirectoryFile = VMBoxDirectory & "Windows Server 2003 Standard Edition.vmx"
BackupRootDirectory = "E:\VMBackups\" ' Backslash necessary
''''''''''''''''''''''''''''''''''''''''''''
' Settings VMServer
''''''''''''''''''''''''''''''''''''''''''''
VMServer_IP= "127.0.0.1" ' IP-Address VMWare Servers
VMServer_Port= "902" ' Port: 902 (default)
VMServer_Username= "username" ' User (needs privileges to start/stop VMBox)
VMServer_Password= "password" ' Password
''''''''''''''''''''''''''''''''''''''''''''
' Settings Script
''''''''''''''''''''''''''''''''''''''''''''
VMServerConnectMaxTries = 10 'Number of Tries to connect with the VMServer
VMConnectMaxTries = 10 'Number of Tries to connect with the VMSBox
''''''''''''''''''''''''''''''''''''''''''''
' Constants
''''''''''''''''''''''''''''''''''''''''''''
Const FridayName = "Friday" ' Adjust for your culture setting
Const vmExecutionState_On = 1
Const vmExecutionState_Off = 2
Const vmExecutionState_Suspended = 3
Const vmExecutionState_Stuck = 4
Const vmExecutionState_Unknown = 5
Const vmPowerOpMode_Hard = 1
Const vmPowerOpMode_Soft = 2
Const vmPowerOpMode_TrySoft = 3
Const OneSecond = 1000
Const EVENT_SUCCESS = 0
Const EVENT_ERROR = 1
''''''''''''''''''''''''''''''''''''''''''''
' Variables
''''''''''''''''''''''''''''''''''''''''''''
WeekDay = WeekDayName(WeekDay(Date))
NrFriday= Day(Date) \ 7 + 1
BackupDirectory = BackupRootDirectory & WeekDay
If WeekDay = FridayName Then
BackupDirectory = BackupDirectory & NrFriday
End If
Set WScript_Shell = CreateObject("WScript.Shell")
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set VmCOM_VmConnectParams = CreateObject("VmCOM.VmConnectParams")
If Err <> 0 Then
ErrorHandler "VMCom-API not available.",true
End If
Set VmCOM_VmServerCtl = CreateObject("VmCOM.VmServerCtl")
If Err <> 0 Then
ErrorHandler "VMCom-API not available.",true
End If
Set WmCOM_VmCtl = CreateObject("VmCOM.VmCtl")
If Err <> 0 Then
ErrorHandler "VMCom-API not available.",true
End If
''''''''''''''''''''''''''''''''''''''''''''
' VMServer connection
''''''''''''''''''''''''''''''''''''''''''
VmCOM_VmConnectParams.hostname = VMServer_IP
VmCOM_VmConnectParams.username = VMServer_Username
VmCOM_VmConnectParams.password = VMServer_Password
VmCOM_VmConnectParams.port = VMServer_Port
Connected = false
For tries_srv = 1 To VMServerConnectMaxTries
VmCOM_VmServerCtl.Connect VmCOM_VmConnectParams
If Err.number = 0 Then
Connected = true
Exit For
End If
WScript.Sleep OneSecond
Err.clear
Next
If Connected = false Then
ErrorHandler "Could not establish the connection to the VMServer.",true
End If
''''''''''''''''''''''''''''''''''''''''''''
' VMBox connection
''''''''''''''''''''''''''''''''''''''''''
Connected_m = false
For tries_vm = 1 To VMConnectMaxTries
WmCOM_VmCtl.Connect VmCOM_VmConnectParams, VMBoxDirectoryFile
If Err.number = 0 Then
Connected_m = true
Exit For
End If
WScript.Sleep OneSecond
Err.clear
Next
If Connected_m = false Then
ErrorHandler "Could not establish the connection to the VMBox " & VMBoxDirectoryFile & ".", true
End If
''''''''''''''''''''''''''''''''''''''''''''
' Suspend VMBox if vmExecutionState_On
''''''''''''''''''''''''''''''''''''''''''''
ps = WmCOM_VmCtl.ExecutionState
If ps = vmExecutionState_On Then
WmCOM_VmCtl.Suspend(vmPowerOpMode_TrySoft)
psnew = WmCOM_VmCtl.ExecutionState
Do while psnew = vmExecutionState_On
WScript.Sleep OneSecond
psnew = WmCOM_VmCtl.ExecutionState
Loop
was_on = true
ElseIf ps = vmExecutionState_Off Or ps = vmExecutionState_Suspended Then
was_on = false
Else
ErrorHandler "Could not determine the state of the VMBox " & VMBoxDirectoryFile & ".", true
End If
''''''''''''''''''''''''''''''''''''''''''''
' Copy VMBox
''''''''''''''''''''''''''''''''''''''''''''
XCOPY = "xcopy"
Parameters = VMBoxDirectory & "* " & BackupDirectory & " /Y /C"
If (FileSystemObject.FolderExists(BackupDirectory) = false) then
FileSystemObject.CreateFolder(BackupDirectory)
If Err <> 0 Then
ErrorHandler "Error while creating the backup directory " & BackupDirectory & ".",true
End If
End if
ps = WmCOM_VmCtl.ExecutionState
If ps = vmExecutionState_Off OR ps = vmExecutionState_Suspended Then
WScript_Shell.run(XCOPY & " " & Parameters)
If Err <> 0 Then
ErrorHandler "Error while copying.",false
End If
End If
''''''''''''''''''''''''''''''''''''''''''''
' Resume VMBox if suspended (ie if was_on = true)
''''''''''''''''''''''''''''''''''''''''''''
If ps = vmExecutionState_Off OR ps = vmExecutionState_Suspended Then
If was_on = true Then
WmCOM_VmCtl.Start(vmPowerOpMode_TrySoft)
If Err <> 0 Then
ErrorHandler "Error while starting the VMBox " & VMBoxDirectoryFile & ".", true
End If
End If
End If
WScript_Shell.LogEvent EVENT_SUCCESS, "VMBackup: Backup to " & BackupDirectory & " successfully created."
''''''''''''''''''''''''''''''''''''''''''''
' ErrorHandling
''''''''''''''''''''''''''''''''''''''''''''
Function ErrorHandler(Message, FullStop)
WScript_Shell.LogEvent EVENT_ERROR, "VMBackup: " & Message
If FullStop = true Then
WScript.Quit
End If
End Function
'See for a few hints: http://www.admins-tipps.de/Microsoft/Windows_Scripting_Host_(WSH)/VBScript_zum_Starten_einer_VMWare-VM_und_Connect_per_RDP.htm