new1234.jpg
Home > VMware > VMware: HOWTO make VCB backup easy with vcbBackup.vbs

VMware: HOWTO make VCB backup easy with vcbBackup.vbs

To install vcb follow the instructions on this link.

The first time I came in contact with vcb I created some batch files to make vcb snapshots. I encountered all kind of problems. Snapshots that vcb didn’t remove. unexpected errors no vss support and some more issues.

To overcome some of these problems I created a vbs script vcbBackup.vbs. The full howto is comming soon for now only the code.

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const EVENT_TYPE_SUCCESS = 0
Const EVENT_TYPE_ERROR = 1
Const EVENT_TYPE_WARNING = 2
Const EVENT_TYPE_INFORMATION = 4
Const EVENT_TYPE_AUDITSUCCESS = 8
Const EVENT_TYPE_AUDITFAILURE = 16

Dim oFileSys, oEditLog, WshShell, oLongPath, oReadvcbVmNamesList 
Dim intReturn
Dim pathConsolidatedBackup, pathLogs, pathLogsShort, pathConsolidatedBackupShort
Dim arg, xCount, argVMFilterOption, bDeleteDestFolder, strInfo
Dim tStartTime, tEndTime, intMinutes

Set oShell = WScript.CreateObject("WScript.Shell")
Set oFileSys = CreateObject("Scripting.FileSystemObject")

If WScript.Arguments.Count = 0 And WScript.Arguments.Count <= 3 Then
   WScript.Echo  "no argument on the command line." & vbCrLf & _
				"<mount point> <username> <password> <vcserver> <optional: moref:vm-xxx> <optional: createlist>" & vbCrLf & _ 
				""& vbCrLf & _
				"<mount point> = Destination location" & vbCrLf & _
				"<username> = Vutrual Center username" & vbCrLf & _
				"<password> = Vutrual Center Password" & vbCrLf & _
				"<vcserver> = Vurtual Center FQDN" & vbCrLf & _
				"<optional: moref:vm-xxx> = to backup one server use moref:vm-xxx" & vbCrLf & _
				"<optional: createlist> = Create vcbVmNames.list.all"
   WScript.Quit    
End If

pathConsolidatedBackup = "C:\Apps\VMware\VMware Consolidated Backup Framework"
pathConsolidatedBackupShort = MakeShortPath(oFileSys, pathConsolidatedBackup)
pathvcbVmNameslist =  "C:\Apps\vcbBackup" 
pathLogs = "C:\Apps\VMWARE_Backup_Logs"
pathLogsShort = MakeShortPath(oFileSys, pathLogs)
argVMFilterOption = "any:"
bDeleteDestFolder = True 
pathMountPoint = WScript.Arguments(0)

argUsername = WScript.Arguments(1)
argPassword = WScript.Arguments(2)

serverConsolidatedBackup = WScript.Arguments(3)
'xCount = 0

If WScript.Arguments.Count = 5 Then  
	If InStr(WScript.Arguments(4), "moref:vm") > 0 Then
		argVMFilterOption = WScript.Arguments(4)
	elseif InStr(WScript.Arguments(4), "CreateList") > 0 Then
		argVMFilterOption = WScript.Arguments(4)
	Else
		Wscript.Echo "Input is incorrect <moref:vm-xxx>"
		WScript.Quit 
	End If
End If 

if argVMFilterOption = "CreateList" then
	tStartTime = Now()
	intReturn = oShell.Run("cmd /c """ & pathConsolidatedBackup & "\vcbVmName.exe"" -h " & serverConsolidatedBackup & " -u " & argUsername & " -p " & argPassword & " -s any: >" & pathvcbVmNameslist & "\vcbVmNames.list.all",0,True)
	tEndTime = Now()
	Call CreateEvent(oShell, EVENT_TYPE_SUCCESS, "vcbVmNames.list.all created"  & Chr(13) & "Elapsed time: " & TimeSpan(tStartTime, tEndTime))
	WScript.Quit
else
	intReturn = oShell.Run("cmd /c """ & pathConsolidatedBackup & "\vcbVmName.exe"" -h " & serverConsolidatedBackup & " -u " & argUsername & " -p " & argPassword & " -s any: >" & pathvcbVmNameslist & "\vcbVmNames.list.all",0,True)	
end if 

If argVMFilterOption = "any:" Then  
	tStartTime = Now()
	Call RemoveSnapshots(oShell, oFileSys, pathvcbVmNameslist & "\vcbVmNames.list", pathMountPoint, pathConsolidatedBackup, serverConsolidatedBackup, pathLogsShort, pathConsolidatedBackupShort)
	Set oReadvcbVmNamesList = oFileSys.OpenTextFile(pathvcbVmNameslist & "\vcbVmNames.list",ForReading,True)
	tEndTime = Now()
	Call CreateEvent(oShell, EVENT_TYPE_SUCCESS, "Remove old snapshots operation completed successfully"  & Chr(13) & "Elapsed time: " & TimeSpan(tStartTime, tEndTime))
Else
	tStartTime = Now()
	intReturn = oShell.Run("cmd /c """ & pathConsolidatedBackup & "\vcbVmName.exe"" -h " & serverConsolidatedBackup & " -u " & argUsername & " -p " & argPassword & " -s " & argVMFilterOption & " >" & pathvcbVmNameslist & "\vcbVmNames.list.signle",0,True)
	Call RemoveSnapshots(oShell, oFileSys, pathvcbVmNameslist & "\vcbVmNames.list.signle", pathMountPoint, pathConsolidatedBackup, serverConsolidatedBackup, pathLogsShort, pathConsolidatedBackupShort)
	Set oReadvcbVmNamesList = oFileSys.OpenTextFile(pathvcbVmNameslist & "\vcbVmNames.list.signle",ForReading,True)
	tEndTime = Now()
	Call CreateEvent(oShell, EVENT_TYPE_SUCCESS, "Remove old snapshot operation completed successfully"  & Chr(13) & "Elapsed time: " & TimeSpan(tStartTime, tEndTime))
End If

Do While Not oReadvcbVmNamesList.AtEndOfStream
	strText = oReadvcbVmNamesList.ReadLine()
	If Instr(strText, "moref:vm") > 0 Then
		strFileUID = MakeTimeStamp()
		strMoRef = strText
		
		strText = oReadvcbVmNamesList.ReadLine()
		strFileName =  Right(strText,Len(strText)-5)
		strFileName = Trim(Replace(strFileName,",", ""))
		strFileName = Trim(Replace(strFileName," ", "_"))
		strFileName = Trim(Replace(strFileName,":", "_"))
		
		strText = oReadvcbVmNamesList.ReadLine()
		strUUID =  Split(strText,":")
		
		tStartTime = Now()
		intReturn = oShell.Run("cmd /c """ & pathConsolidatedBackup & "\vcbMounter"" -h " & serverConsolidatedBackup & " -u " & argUsername & " -p " & argPassword & " -a " & strMoRef & " -r " & pathMountPoint & "\" & strFileName & "." & strUUID(1) & "-fullVM -t fullvm -m san -L 6 >" & pathLogsShort & "\" & strFileName & "_" & strFileUID & "_makesnapshot.txt",0,True)
		tEndTime = Now()	 

		strErrMessage = strFileName & Chr(13) & strMoRef & Chr(13) & strUUID(0) & ":" & strUUID(1) & Chr(13) & "Processed " & CalculateFolderSize(oFileSys, pathMountPoint & "\" & strFileName & "." & strUUID(1) & "-fullVM") & " bytes in "  & TimeSpan(tStartTime, tEndTime) & Chr(13) & "For more info see log file : " & pathLogs & "\" & strFileName & "_" & strFileUID & "_makesnapshot.txt"
		
		Call CheckIFSnapShotISSuccess(oFileSys, pathLogs & "\" & strFileName & "_" & strFileUID & "_makesnapshot.txt", pathMountPoint & "\" & strFileName & "." & strUUID(1) & "-fullVM", strMessage)
	End If
Loop

oReadvcbVmNamesList.Close
Set oReadvcbVmNamesList = Nothing
Set oFilesys = Nothing  
Set oShell = Nothing 

WScript.Quit

'***************************************************************************************************************************************
'******* FUNCTIONS *********************************************************************************************************************
'***************************************************************************************************************************************
Function FindRemoveSnapshot(oShell, oFileSys, pathConsolidatedBackup, serverConsolidatedBackup, strMoRef, strFileUID, pathLogs, strFileName, pathLogsShort, pathConsolidatedBackupShort)
	Dim strText, strSSidSnapShot, intReturn, oReadFoundSnapShot, oReadFoundSnapShotCH
	Dim bExitLoopCH
	
	bExitLoopCH = false
	
	intReturn = oShell.Run("cmd /c """ & pathConsolidatedBackupShort & "\vcbSnapshot.exe"" -h " & serverConsolidatedBackup & " -u " & argUsername & " -p " & argPassword & " -f " & strMoRef & " _VCB-BACKUP_ >" & pathLogsShort & "\" & strFileName & "_" & strFileUID & "_foundsnapshot.txt",0,True)
	Set oReadFoundSnapShot = oFileSys.OpenTextFile(pathLogsShort & "\" & strFileName & "_" & strFileUID & "_foundsnapshot.txt",ForReading,True)	
	Do While Not oReadFoundSnapShot.AtEndOfStream
		strText = oReadFoundSnapShot.ReadLine()
		If Instr(strText, "SsId:snapshot") > 0 Then
			intReturn = oShell.Run("cmd /c """ & pathConsolidatedBackupShort & "\vcbSnapshot.exe"" -h " & serverConsolidatedBackup & " -u " & argUsername & " -p " & argPassword & " -L 6 -d " & strMoRef & " " & Trim(strText) & " >" & pathLogsShort & "\" & strFileName & "_" & strFileUID & "_rmsnapshot.txt",0,True)
		End If
	Loop
	intReturn = oShell.Run("cmd /C " & pathConsolidatedBackupShort & "\vcbSnapshot.exe -h " & serverConsolidatedBackup & " -u " & argUsername & " -p " & argPassword & " -f " & strMoRef & " ""Consolidate Helper"" >" & pathLogsShort & "\" & strFileName & "_" & strFileUID & "_foundsnapshot_ch.txt",0,True)
	
	Set oReadFoundSnapShotCH = oFileSys.OpenTextFile(pathLogsShort & "\" & strFileName & "_" & strFileUID & "_foundsnapshot_ch.txt",ForReading,True)	
	Do While Not oReadFoundSnapShotCH.AtEndOfStream
		strText = oReadFoundSnapShotCH.ReadLine()
   
		If Instr(strText, "SsId:snapshot") > 0 Then
			intReturn = oShell.Run("cmd /c """ & pathConsolidatedBackup & "\vcbSnapshot.exe"" -h " & serverConsolidatedBackup & " -u " & argUsername & " -p " & argPassword & " -L 6 -d " & strMoRef & " " & Trim(strText) & " >" & pathLogsShort & "\" & strFileName & "_" & strFileUID & "_rmsnapshot.txt",0,True)
		End If
	Loop

	oReadFoundSnapShot.close
	oReadFoundSnapShotCH.close
	Set oReadFoundSnapShot = Nothing 
	Set oReadFoundSnapShotCH = Nothing
End Function

Function MakeTimeStamp()
	MakeTimeStamp = Year(now()) & Right("0" & Month(now()), 2) & Right("0" & Day(now()), 2) & Right("0" & Hour(now()), 2) & Right("0" & Hour(now()), 2) & Right("0" & Minute(now()), 2) & Right("0" & Second(now()), 2)
End Function

Function MakeUnmountDat(oFileSys, pathUnmountDat, strUUID)
	Set oMakeUnmountDat = oFileSys.CreateTextFile(pathUnmountDat & "\unmount.dat",True)
	oMakeUnmountDat.WriteLine("fullvm")
	oMakeUnmountDat.WriteLine("uuid:" & strUUID)
	oMakeUnmountDat.WriteLine("any:")
	oMakeUnmountDat.Close
	Set oMakeUnmountDat = Nothing
End Function

Function MakeShortPath(oFileSys,pathConsolidatedBackup)
	if oFileSys.FolderExists(pathConsolidatedBackup) then
		Set oLongPath = oFileSys.GetFolder(pathConsolidatedBackup)
		MakeShortPath = oLongPath.ShortPath
		If Err.Number <> 0 Then
			Err.Clear
			MakeShortPath = pathConsolidatedBackup
		End If
	end if
	Set oLongPath = Nothing 
End Function

Function RemoveSnapshots(oShell, oFileSys, pathVcbVmNamesList, pathMountPoint, pathConsolidatedBackup, serverConsolidatedBackup, pathLogsShort, pathConsolidatedBackupShort)
	Dim bReadUnmountDat ,oReadvcbVmNamesList
	Dim strReadUmountDatUUID, strReadUUID
	
	ReadUnmountDat = False
	Set oReadvcbVmNamesList = oFileSys.OpenTextFile(pathVcbVmNamesList,1,True)
	Do While Not oReadvcbVmNamesList.AtEndOfStream
		strText = oReadvcbVmNamesList.ReadLine()
		If Instr(strText, "moref:vm") > 0 Then
			strFileUID = MakeTimeStamp()
			strMoRef = strText
			
			strText = oReadvcbVmNamesList.ReadLine()
			strFileName =  Right(strText,Len(strText)-5)
			strFileName = Trim(Replace(strFileName,",", ""))
			strFileName = Trim(Replace(strFileName," ", "_"))
			strFileName = Trim(Replace(strFileName,":", "_"))
			
			strReadUUID = oReadvcbVmNamesList.ReadLine()
			strUUID =  Split(strReadUUID,":")
	 
			If oFileSys.FolderExists(pathMountPoint & "\" & strFileName & "." & strUUID(1) & "-fullVM") Then
				If oFileSys.FileExists(pathMountPoint & "\" & strFileName & "." & strUUID(1) & "-fullVM\unmount.dat") Then
					Set oReadUnmountDat = oFileSys.OpenTextFile(pathMountPoint & "\" & strFileName & "." & strUUID(1) & "-fullVM\unmount.dat",1,True)
					Do While Not oReadUnmountDat.AtEndOfStream
						strReadUmountDatUUID = oReadUnmountDat.ReadLine()
						If Instr(strReadUmountDatUUID, strReadUUID) > 0 Then
							ReadUnmountDat = True
						End If 	
					Loop 
					oReadUnmountDat.close
					If Not ReadUnmountDat Then
						Call MakeUnmountDat(oFileSys, pathMountPoint & "\" & strFileName & "." & strUUID(1) & "-fullVM", strUUID(1))
						intReturn = oShell.Run("cmd /c """ & pathConsolidatedBackup & "\vcbMounter"" -h " & serverConsolidatedBackup & " -u " & argUsername & " -p " & argPassword & " -U " & pathMountPoint & "\" & strFileName & "." & strUUID(1) & "-fullVM -L 6 >" & pathLogsShort & "\" & strFileName & "_" & strFileUID & "_unmount.txt",0,True)
					Else 
						intReturn = oShell.Run("cmd /c """ & pathConsolidatedBackup & "\vcbMounter"" -h " & serverConsolidatedBackup & " -u " & argUsername & " -p " & argPassword & " -U " & pathMountPoint & "\" & strFileName & "." & strUUID(1) & "-fullVM -L 6 >" & pathLogsShort & "\" & strFileName & "_" & strFileUID & "_unmount.txt",0,True)
					End If
				Else
					Call MakeUnmountDat(oFileSys, pathMountPoint & "\" & strFileName & "." & strUUID(1) & "-fullVM", strUUID(1))
					intReturn = oShell.Run("cmd /c """ & pathConsolidatedBackup & "\vcbMounter"" -h " & serverConsolidatedBackup & " -u " & argUsername & " -p " & argPassword & " -U " & pathMountPoint & "\" & strFileName & "." & strUUID(1) & "-fullVM -L 6 >" & pathLogsShort & "\" & strFileName & "_" & strFileUID & "_unmount.txt",0,True) 
				End If
			End If
			Call FindRemoveSnapshot(oShell, oFileSys, pathConsolidatedBackup, serverConsolidatedBackup, strMoRef, strFileUID, pathLogs, strFileName, pathLogsShort, pathConsolidatedBackupShort)
		End If
	Loop
	oReadvcbVmNamesList.close
	Set oReadUnmountDat = Nothing
	Set oReadvcbVmNamesList = Nothing 
End Function

Function CreateEvent(oShell, EventState, EventString)
		oShell.LogEvent EventState, EventString
		'{ERROR|WARNING|INFORMATION|SUCCESSAUDIT|FAILUREAUDIT}
		'Select Case EventState 
		'Case "EVENT_TYPE_SUCCESS"
		'	EventState = "INFORMATION"
		'	varEventID = 100
		'Case "EVENT_TYPE_ERROR" 
		'	EventState = "ERROR"
		'	varEventID = 101				
		'Case "EVENT_TYPE_WARNING"
		'	EventState = "WARNING"
		'	varEventID = 102	
		'End Select
		'WScript.Echo "cmd /c ""%windir%\system32\eventcreate /T " & EventState & " /ID " & varEventID & " /L " & "vcbBackup" & " /D " & EventString & """"
		'intReturn =  oShell.Run("cmd /c ""%windir%\system32\eventcreate /T " & EventState & " /ID " & varEventID & " /L " & "vcbBackup" & " /D " & EventString & """",0,True)
End Function

Function CheckIFSnapShotISSuccess(oFileSys, pathLogs, pathMountPoint, strMessage)
	Dim bFoundErr 
	Set oReadFoundSnapShot = oFileSys.OpenTextFile(pathLogs,1,True)
	
	bFoundErr = False
	Do While Not oReadFoundSnapShot.AtEndOfStream
		strText = oReadFoundSnapShot.ReadLine()
		If Instr(strText, "Successfully exported data to directory " & pathMountPoint) > 0 Then
			strText = oReadFoundSnapShot.ReadLine()
			If Instr(strText, "Operation completed successfully") > 0 Then
				WScript.Echo "Good" 
				Call CreateEvent(oShell, EVENT_TYPE_SUCCESS, "Operation completed successfully"  & Chr(13) & strErrMessage)
			End If
		End If
		If Instr(strText, "Error:") > 0 Then
			WScript.Echo "Error" 
			Call CreateEvent(oShell, EVENT_TYPE_ERROR, "An error occured operation Failed"  & Chr(13) & strText & Chr(13) & strErrMessage)
		End If
		
	Loop
	
	oReadFoundSnapShot.close
	Set oReadFoundSnapShot = Nothing 
End Function

Function TimeSpan(dt1, dt2) 
	If (isDate(dt1) And IsDate(dt2)) = false Then 
		TimeSpan = "00:00:00" 
		Exit Function 
	End If 
	
	seconds = Abs(DateDiff("S", dt1, dt2)) 
	minutes = seconds \ 60 
	hours = minutes \ 60 
	minutes = minutes mod 60 
	seconds = seconds mod 60 
	
	if len(hours) = 1 then hours = "0" & hours 
	
	TimeSpan = hours & ":" & RIGHT("00" & minutes, 2) & ":" & RIGHT("00" & seconds, 2) 
End Function 

Function CalculateFolderSize(oFileSys, objStartFolder)
	If oFileSys.FolderExists(objStartFolder) Then
		Set objFolder = oFileSys.GetFolder(objStartFolder) 
		Set colFiles = objFolder.Files 
		foldersize = 0
		For Each objFile in colFiles 
			foldersize = foldersize + Int(objFile.Size)
		Next 	
		CalculateFolderSize = FormatNumber(foldersize,0)
		Exit Function
	End If
		CalculateFolderSize = "not found" 
End Function

[ad#ad-banner]

Categories: VMware Tags: , , , ,