Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
full-wbadmin-backup-script.vbs
ForceScriptEngine("cscript")
Const ForReading = 1
strComputer = "."
strDate = Replace(FormatDateTime(Date(),2),"/","-")
set objShell = CreateObject("WScript.Shell")
strComputer = objShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
strUserDomain = objShell.ExpandEnvironmentStrings( "%USERDOMAIN%")
'Build the structure of file locations, recipients, log files etc....
strSharePath = "\\server\share\Backups"
strDomainPath = strSharePath & "\" & strUserDomain
strComputerPath = strDomainPath & "\" & strComputer
strServerFolderStatePath = strComputerPath & "\AllCritical"
strServerFolderDatePath = strServerFolderStatePath & "\" & strDate
strServerFolderCopyPath = strServerFolderDatePath & "\WindowsImageBackup" ' target for copy
strLocalPath="E:"
strFullBackupPath = strLocalPath & "\WindowsImageBackup"
strArchivePath="E:\AllCriticalBackup"
boolSuccess="False"
strRecipients="someone@blah.com;someone_else@blah.com"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
("Select * from Win32_Service where (displayname = 'sophos autoupdate service') or (displayname = 'sophos anti-virus') or (displayname = 'sophos agent')")
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Delete the previous backup folder
If objFso.FolderExists(strFullBackupPath) Then
objFSO.DeleteFolder strFullBackupPath,True
End If
strCommand="C:\Windows\System32\cmd.exe /c Wbadmin start backup -backuptarget:" & strLocalPath & " -allCritical -quiet > " & strLocalPath & "\" & "Output_" & strDate & ".txt"
'Stop AV services, to speed up the backup process. Start again once complete.
SophosService("stop")
objShell.Run strCommand,1,true
SophosService("start")
Set objFile = objFSO.OpenTextFile(strLocalPath & "\Output_" & strDate & ".txt", ForReading)
'Simple test to verify that the backup succeeded
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine,"The backup operation to E: is starting.") Then
strOperation=strLine
End If
If InStr(strLine,"The backup operation successfully completed.") Then
strResult=strLine
boolSuccess="True"
End If
Loop
objFile.Close
If err.number=0 And boolSuccess="True" Then
If Not objFso.FolderExists(strSharePath) Then
objFso.CreateFolder(strSharePath)
End If
If Not objFso.FolderExists(strDomainPath) Then
objFso.CreateFolder(strDomainPath)
End If
If Not objFso.FolderExists(strComputerPath) Then
objFso.CreateFolder(strComputerPath)
End If
If Not objFso.FolderExists(strServerFolderStatePath) Then
objFso.CreateFolder(strServerFolderStatePath)
End If
If Not objFso.FolderExists(strServerFolderDatePath) Then
objFso.CreateFolder(strServerFolderDatePath)
End If
If Not objFso.FolderExists(strServerFolderCopyPath) Then
objFso.CreateFolder(strServerFolderCopyPath)
End If
If objFso.FolderExists(strServerFolderCopyPath) Then
On Error resume next
Err.Number=0
objFSO.CopyFolder strFullBackupPath, strServerFolderCopyPath
If Err.Number <> 0 Then
strError = "Error copying files to " & strServerFolderCopyPath & "!" & VBCrLf & "Error Number: " & Err.Number & VBCrLf & "Error Description: " & Err.Description & VBCrLf & "Error Source: " & Err.Source
strSubject="File copy operation on " & strComputer & " failed - see error below"
strTextBody=strError
eMail strRecipients,strSubject,strTextBody
Else
strRemoteTarget = "Files copied to: " & strServerFolderCopyPath
End If
On Error goto 0
End If
If Not objFso.FolderExists(strArchivePath) Then
objFso.CreateFolder(strArchivePath)
End If
If Not objFso.FolderExists(strArchivePath & "\" & strDate) Then
objFso.CreateFolder(strArchivePath & "\" & strDate)
End If
If objFso.FolderExists(strArchivePath & "\" & strDate) Then
'Copy the folder to retain the last backup as an immediately usable backup, without having to move files around....
On Error resume next
Err.Number=0
objFSO.CopyFolder strFullBackupPath, strArchivePath & "\" & strDate & "\WindowsImageBackup"
If Err.Number <> 0 Then
strError = "Error copying files to " & strArchivePath & "!" & VBCrLf & "Error Number: " & Err.Number & VBCrLf & "Error Description: " & Err.Description & VBCrLf & "Error Source: " & Err.Source
strSubject="File copy operation on " & strComputer & " failed - see error below"
strTextBody=strError
eMail strRecipients,strSubject,strTextBody
End If
Err.Number=0
objFSO.MoveFile strLocalPath & "\" & "Output_" & strDate & ".txt", strArchivePath & "\" & strDate & "\"
If Err.Number <> 0 Then
strError = "Error moving files to " & strArchivePath & "!" & VBCrLf & "Error Number: " & Err.Number & VBCrLf & "Error Description: " & Err.Description & VBCrLf & "Error Source: " & Err.Source
strSubject="File move operation on " & strComputer & " failed - see error below"
strTextBody=strError
eMail strRecipients,strSubject,strTextBody
End If
On Error goto 0
End If
End If
'Now tidy up the historical local and remote backups, by calling an external PowerShell script:
strPSLocalCommand = "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe C:\BackupScript\DELETEOLD_SIGNED.PS1 -folderpath " & strArchivePath & " -fileage 3 -logfile " & strArchivePath & "\CleanupLog_" & strDate & ".txt -verboselog"
'Wscript.Echo "strPSLocalCommand: " & strPSLocalCommand
objShell.Run strPSLocalCommand,1,FALSE
strPSRemoteCommand = "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe C:\BackupScript\DELETEOLD_SIGNED.PS1 -folderpath " & strServerFolderStatePath & " -fileage 14 -logfile " & strServerFolderStatePath & "\CleanupLog_" & strDate & ".txt -verboselog"
'Wscript.Echo "strPSRemoteCommand: " & strPSRemoteCommand
objShell.Run strPSRemoteCommand,1,FALSE
If boolSuccess="True" Then
GetFSMO
strSubject="All Critical Backup of " & strComputer & " completed successfully"
strTextBody=GetFSMO & VBCrLf & strOperation & VBCrLf & strResult & VBCrLf & strRemoteTarget
eMail strRecipients,strSubject,strTextBody
End If
If boolSuccess<>"True" Then
strSubject="All Critical Backup of " & strComputer & " failed - PLEASE CHECK!"
strTextBody=strOperation & VBCrLf & " !! An Error Occurred During The Backup Operation!!"
eMail strRecipients,strSubject,strTextBody
End If
'#####################################################################################
Sub SophosService(args)
If args="stop" Then
For each objService in colServiceList
'Wscript.Echo "Stopping: " & objService.name
errReturn = objService.StopService()
Next
End If
If args="start" Then
For each objService in colServiceList
'Wscript.Echo "Starting: " & objService.name
errReturn = objService.StartService()
Next
End If
End Sub
'#####################################################################################
Function GetFSMO()
strDomain = "YourDomain.com"
strReport=""
set objRootDSE = GetObject("LDAP://" & strDomain & "/RootDSE")
strDomainDN = objRootDSE.Get("defaultNamingContext")
strSchemaDN = objRootDSE.Get("schemaNamingContext")
strConfigDN = objRootDSE.Get("configurationNamingContext")
' PDC Emulator
set objPDCFsmo = GetObject("LDAP://" & strDomainDN)
strReport = "PDC Emulator: " & objPDCFsmo.fsmoroleowner & VbCrLF
' RID Master
set objRIDFsmo = GetObject("LDAP://cn=RID Manager$,cn=system," & strDomainDN)
strReport = strReport & "RID Master: " & objRIDFsmo.fsmoroleowner & VbCrLF
' Schema Master
set objSchemaFsmo = GetObject("LDAP://" & strSchemaDN)
strReport = strReport & "Schema Master: " & objSchemaFsmo.fsmoroleowner & VbCrLF
' Infrastructure Master
set objInfraFsmo = GetObject("LDAP://cn=Infrastructure," & strDomainDN)
strReport = strReport & "Infrastructure Master: " & objInfraFsmo.fsmoroleowner & VbCrLF
' Domain Naming Master
set objDNFsmo = GetObject("LDAP://cn=Partitions," & strConfigDN)
strReport = strReport & "Domain Naming Master: " & objDNFsmo.fsmoroleowner & VbCrLF
GetFSMO=strReport
End Function
'#####################################################################################
Function eMail(Recipients,Subject,TextBody)
Dim objEmail
' CREATE THE MESSAGE OBJECT USING CDO
Set objEmail = CreateObject("CDO.Message")
' ASSIGN MESSAGE PROPERTIES (TO, FROM, SUBJECT, ETC.)
objEmail.From = "GenericAddress@blah.com"
objEmail.To = Recipients
objEmail.Subject = Subject
objEmail.Textbody = TextBody
' DEFINE SEND PROPERTIES AND SEND THE MAIL
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "YourSMTPServer.blah.com"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send
End Function
'#####################################################################################
Sub ForceScriptEngine(strScriptEng)
' Forces this script to be run under the desired scripting host.
' Valid arguments are "wscript" or "cscript".
' The command line arguments are passed on to the new call.
Dim arrArgs
Dim strArgs
For Each arrArgs In WScript.Arguments
strArgs = strArgs & " " & Chr(34) & arrArgs & Chr(34)
Next
If Lcase(Right(Wscript.FullName, 12)) = "\wscript.exe" Then
If Instr(1, Wscript.FullName, strScriptEng, 1) = 0 Then
CreateObject("Wscript.Shell").Run "cscript.exe //Nologo " & _
Chr(34) & Wscript.ScriptFullName & Chr(34) & strArgs
Wscript.Quit
End If
Else
If Instr(1, Wscript.FullName, strScriptEng, 1) = 0 Then
CreateObject("Wscript.Shell").Run "wscript.exe " & Chr(34) & _
Wscript.ScriptFullName & Chr(34) & strArgs
Wscript.Quit
End If
End If
End Sub
'#####################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.