Here is a script that I put together to backup VM on a remote ESXi server of mine. It is setup to run with in a VM using VCB Proxy. This can be changed to use backup over the network using nbdssl if needed.
Any feed back would be great.
-
'Version 1.0.0
'This script will make a full or differential of VMWare Virtual Machines.
'Dependencies:
' VCB 1.5 or greater http://downloads.vmware.com/d/details/vcb40/ZHcqYmRkJWpiZGUlaA==
' 7-zip 4.65 or greater http://www.7-zip.org/download.html
'To use strVCBTT = hotadd opton you will need to be in a VM and have a VCB-Helper http://www.vmware.com/pdf/vsphere4/r40/vsp_vcb_15_u1_admin_guide.pdf
'Created by: Paul Fuller
'Copyright 2010
'License: GNUv3 http://www.gnu.org/licenses/gpl.html
Option Explicit
Const PROGRAM_FILES = &H26&
Const strExe7z = "\7-Zip\7z.exe"
Const strExeArgsBase = "a -mx9 -sfx7z.sfx -ssw -bd -t7z -mmt -ms=off"
Const strhost="ucpg-vcenter.uchospitals.edu"
Const strusername="__sspi__"
Const strpassword="aaa"
Const strVMSName="name"
Const strVCBTT = "hotadd"
'Const strVCBTT = "nbdssl"
Const strOutputFolder = "D:\Backup"
Const strBackupfol = "D:\VM_Backups"
Const intForceFull = 7
'**Change options below only of you know what you are doing**
Dim objFileSys
Dim objWshNetwork
Dim objWshShell
Dim objShell
Dim objProgramFiles
Dim objFolder
Dim objFile
Dim strVM
Dim strCmd
Dim strvcbMounter
Dim intDateDiff
Dim log
Dim strArguments
Dim strExe
Call ScriptMode("cscript") 'Call the sub with the script host you want to use(cscript or wscript)
Set objFileSys = CreateObject( "Scripting.FileSystemObject" )
Set objWshNetwork = CreateObject( "WScript.Network" )
Set objShell = CreateObject("Shell.Application")
Set objWshShell = WScript.CreateObject("WScript.Shell")
Set objFolder = objShell.NameSpace(PROGRAM_FILES)
Set objProgramFiles = objFolder.Self
strvcbMounter = Chr(34) & objProgramFiles.path & "\VMware\VMware Consolidated Backup Framework\vcbMounter.exe" & Chr(34)
strExe = objProgramFiles.Path & strExe7z
intDateDiff = -1
'Take VMs as an arguements
If wscript.arguments.count = 0 Then
Wscript.Echo "No VM Specified! Argument count=" & wscript.arguments.count
wscript.quit
End If
If Not objFileSys.FileExists(objProgramFiles.path & strExe7z) Then
Wscript.Echo "Please install 7-zip on this computer!"
wscript.quit
End If
If Not objFileSys.FileExists(objProgramFiles.path & "\VMware\VMware Consolidated Backup Framework\vcbMounter.exe") Then
Wscript.Echo "Please install VCB on this computer!","Script Dependency Issue!"
wscript.quit
End If
'Mounts Each VM
For Each strVM in wscript.arguments
If Not UCase(objWshNetwork.ComputerName) = UCase(strVM) Then
If objFileSys.FolderExists(strBackupfol & "\" & strVM) Then
Wscript.Echo(Now & " VM has been mounted already: " & strVM)
Else
Wscript.Echo(Now & " Mounting VM: " & strVM)
strCmd = strvcbMounter & " -h " & strhost & " -u " & strusername & " -p " & strpassword & " -a " & strVMSName & ":" & strVM & " -r " _
& Chr(34) & strBackupfol & "\" & strVM & "" & Chr(34) & " -m " & strVCBTT & " -t file -L4"
'& " >" & Chr(34) & strBackupfol & "\" & PDate & "_" & strVM & ".log" & Chr(34)
'Wscript.Echo(strCmd)
objWshShell.run strCmd,10,True
End If
Else
Wscript.Echo objWshNetwork.ComputerName & " can not backup its self!!"
End If
Next
'Creates Diff Archives
For Each strVM in wscript.arguments
If Not UCase(objWshNetwork.ComputerName) = UCase(strVM) Then
If objFileSys.FolderExists(strBackupfol & "\" & StrVM & "\letters") Then
'Find out how many day ago the last full backup was for the VM
intDateDiff = VMDateDiff(strBackupfol & "\" & StrVM & "_FullBackup.log")
If (intDateDiff > 0) AND (intDateDiff < intForceFull) Then
Wscript.Echo(Now & " Days since " & strVM & " had full backup: " & intDateDiff)
Wscript.Echo(Now & " Starting differential backup of: " & strVM)
'Changed current Directory for 7-zip
objWshShell.CurrentDirectory = strBackupfol & "\" & StrVM & "\letters"
'Creates text file of all changed files
Set log = objFileSys.OpenTextFile(strOutputFolder & "\" & PDate & "_" & strVM & "Diff.txt", 2,true,0)
'Set log = objFileSys.OpenTextFile(strBackupfol & "\" & StrVM & "\" & PDate & "_Diff.txt", 2,true,0)
Subfolders objFileSys.GetFolder(strBackupfol & "\" & StrVM & "\letters" )
log.close
'Creates Diff Archive including all files in text file
strArguments = strExeArgsBase & " " & Chr(34) & strOutputFolder & "\" & PDate & "_" & strVM & "_Diff.exe" & Chr(34) _
&" -w" & strOutputFolder & "\ @" & strOutputFolder & "\" & PDate & "_" & strVM & "Diff.txt"
If Not objWshShell.Run(Chr(34) & strExe & Chr(34) & " " & strArguments, 10, True ) = 0 Then
Wscript.Echo("7-zip Error")
Wscript.Echo("Executable: " & strExe)
Wscript.Echo("Arguments: " & strArguments)
End If
'Clean up
objFileSys.DeleteFile strOutputFolder & "\" & PDate & "_" & strVM & "Diff.txt"
'Update/Creates Log for Diff backup
Set log = objFileSys.OpenTextFile(strBackupfol & "\" & StrVM & "_FullBackup.log", 2,true,0)
log.writeline Now
log.close
Else
Wscript.Echo(Now & " Starting full backup of: " & strVM)
'Creates Archive including all files in text file
strArguments = strExeArgsBase & " " & Chr(34) & strOutputFolder & "\" & PDate & "_" & strVM & "_Full.exe" & Chr(34) _
&" -w" & strOutputFolder & "\ " & Chr(34) & strBackupfol & "\" & StrVM & "\letters\*" & Chr(34)
If Not objWshShell.Run(Chr(34) & strExe & Chr(34) & " " & strArguments, 10, True ) = 0 Then
Wscript.Echo("7-zip Error")
Wscript.Echo("Executable: " & strExe)
Wscript.Echo("Arguments: " & strArguments)
End If
'Update/Creates Log for Diff backup
Set log = objFileSys.OpenTextFile(strBackupfol & "\" & StrVM & "_FullBackup.log", 2,true,0)
log.writeline Now
log.close
End If
Else
Wscript.Echo(Now & " VM Not Mounted skipping: " & strVM)
End If
'Changed current Directory for unMounting
objWshShell.CurrentDirectory = strBackupfol & "\"
End If
Next
'Un-mounts VMs
For Each strVM in wscript.arguments
If Not UCase(objWshNetwork.ComputerName) = UCase(strVM) Then
Wscript.Echo(Now & " UnMounting VM: " & strVM)
strCmd = strvcbMounter & " -h " & strhost & " -u " & strusername & " -p " & strpassword & " -U " _
& Chr(34) & strBackupfol & "\" & strVM & "" & Chr(34)
'Wscript.Echo(strCmd)
objWshShell.run strCmd,10,True
End IF
Next
'End of Script
wscript.quit
Function PDate()
Dim intDate
intDate = Year(Date())
If Len(Month(Date())) = 1 Then
intDate = intDate & "0" & Month(Date())
Else
intDate = intDate & Month(Date())
End If
If Len(Day(Date())) = 1 Then
intDate = intDate & "0" & Day(Date())
Else
intDate = intDate & Day(Date())
End If
PDate = intDate
End Function
Function VMDateDiff(ByVal strVMDateFile )
'Find out how many day ago the last full backup was
Dim strLog
Dim log
Dim objFileSys
Dim intOut
Dim strDate
Set objFileSys = CreateObject( "Scripting.FileSystemObject" )
If objFileSys.FileExists(strVMDateFile) Then
Set log = objFileSys.OpenTextFile(strVMDateFile,1)
Do Until log.AtEndOfStream
strLog = log.ReadLine
If Not strLog = "" Then
strDate = strLog
End If
Loop
log.close
'wscript.echo("Number of days since last Full update: " & DateDiff("D",strLog,Now))
intOut = DateDiff("D",strDate,Now)
Else
intOut = -1
End If
VMDateDiff = intOut
End Function
Sub SubFolders(Folder)
On Error Resume Next
Dim Subfolder
For Each Subfolder in Folder.SubFolders
ListFiles Subfolder
SubFolders Subfolder
Next
End Sub
Sub ListFiles(Subfolder)
Dim files
Dim folderIdx
Set files = Subfolder.Files
For Each folderIdx In files
If DateDiff("D",folderIdx.DateLastModified,Now) < intDateDiff Then
If Not folderIdx.name = "pagefile.sys" Then
log.WriteLine Right(folderIdx.path, (Len(folderIdx.path) - Len(objWshShell.CurrentDirectory & "\")))
'Wscript.Echo Right(folderIdx.path, (Len(folderIdx.path) - Len(objWshShell.CurrentDirectory & "\"))) & " " & DateDiff("D",folderIdx.DateLastModified,Now)
Else
'Wscript.Echo Right(folderIdx.path, (Len(folderIdx.path) - Len(objWshShell.CurrentDirectory & "\"))) & " " & DateDiff("D",folderIdx.DateLastModified,Now)
End If
End If
Next
End Sub
Sub ScriptMode(strMode)
Dim strPath, strCommand, strWinDir
Dim strArg, objArgs
Dim objShell
Set objShell = CreateObject("Wscript.Shell")
If Not LCase(Right(Wscript.FullName, 11)) = strMode & ".exe" Then
strPath = Wscript.ScriptFullName
strWinDir = objShell.ExpandEnvironmentStrings("%WinDir%")
If Not objShell.ExpandEnvironmentStrings("%ProgramFiles(x86)%") = "" Then
strCommand = strWinDir & "\SysWOW64\" & strMode & ".exe " & Chr(34) & strPath & chr(34)
Set objArgs = wscript.arguments
For Each strArg in objArgs
strCommand = strCommand & " " & strArg
Next
objShell.Run(strCommand)
Wscript.Quit
Else
strCommand = strWinDir & "\system32\" & strMode & ".exe " & Chr(34) & strPath & chr(34)
Set objArgs = wscript.arguments
For Each strArg in objArgs
strCommand = strCommand & " " & strArg
Next
objShell.Run(strCommand)
Wscript.Quit
End If
End If
End Sub
-
Good script, thanx for this job, used as basis for my own backups.
--
V.