'########## THIS VBSCRIPT ENUMERATES ALL INFORMATION STORES '########## AND CREATES NECESSARY BACKUP JOB SELECTION FILES AND BATCH FILES '########## THEN IF REQUESTED IT RUNS THE BACKUP JOBS '########## THEN IT SCHEDULES ITSELF FOR THE NEXT DAY '########## AFTER EACH BACKUP JOB, IT SENDS BACKUP LOGS TO A SPECIFIED E-MAIL ADDRESS '########## IT ALSO MAKES A SUMMARY OF ALL LOGS AND SENDS TO A SPECIFIED E-MAIL ADDRESS '########## IF REQUESTED IT CAN PEFORM A TWO-DAY BACKUP ROTATION '########## WRITTEN BY ANDREY FYODOROV '########## Please run the script from a command prompt (DOS) window '########## cscript backupexchange.vbs - to setup and schedule backup jobs for tomorrow. '########## cscript backupexchange.vbs Rotate2 - to setup and schedule backup jobs for tomorrow using 2-day rotation. '########## cscript backupexchange.vbs BackupNow - to setup and schedule backup jobs for tomorrow and perform an immediate backup. '########## cscript backupexchange.vbs BackupNow Rotate2 - to setup and schedule backup jobs for tomorrow using 2-day rotation and perform an immediate backup. '########## COPY THE BACKUPEXCHANGE.VBS TO D:\NTBACKUP '########## THE SCRIPT ASSUMES THAT THE TARGET BACKUP VOLUME IS G: '########## IT CREATES G:\NTBACKUP DIRECTORY WITH \SELECTIONS, \CMDs, \LOGs, and \Exchange Backups SUBDIRECTORIES CONST LogMailerDomain = "@MyDomainName.com" 'this will be in the From address of e-mailed logs and reports CONST ReportRecipientAddress = "Exchange2003BackupReports@MyDomainName.com" 'this is where backup reports will be sent daily CONST LogRecipientAddress = "Exchange2003BackupLogs@MyDomainName.com" 'this is where backup logs will be sent daily On Error Resume Next 'wscript.echo wscript.fullname & " " & wscript.ScriptName If InStr(wscript.fullname,"cscript.exe") = 0 then wscript.echo "This script should be run using 'cscript.exe backupexchange.vbs [BackupNow [Rotate 2]]'. Terminating script." MsgBox "You cannot run this script by double-clicking." & vbCrLf &_ "Please run the script from a command prompt (DOS) window" & vbCrLf & vbCrLf &_ "cscript backupexchange.vbs - to setup and schedule backup jobs for tomorrow." & vbCrLf & vbCrLf &_ "cscript backupexchange.vbs Rotate2 - to setup and schedule backup jobs for tomorrow using 2-day rotation." & vbCrLf & vbCrLf &_ "cscript backupexchange.vbs BackupNow - to setup and schedule backup jobs for tomorrow and perform an immediate backup." & vbCrLf & vbCrLf &_ "cscript backupexchange.vbs BackupNow Rotate2 - to setup and schedule backup jobs for tomorrow using 2-day rotation and perform an immediate backup." wscript.sleep 2000 wscript.quit End If Set Args = Wscript.Arguments strSwitch1 = Args(0) strSwitch2 = Args(1) strSwitch3 = Args(2) Wscript.Echo "SWITCHES >>>>>>>>>>>>>" & strSwitch1 & " " & strSwitch2 & " " & strSwitch3 '########## CHECK IF ACTIVE NODE Set objShell = CreateObject("WScript.Shell") Set objExec = objShell.Exec("CMD /cDIR G:") strDirG = objExec.StdOut.ReadAll set objShell = Nothing If Len(strDirG)>0 Then 'DO ALL THIS IF DRIVE G: IS ACCESSIBLE (ACTIVE NODE) '########## GET THE NODE NAME Set WS = WScript.CreateObject("WScript.Shell") strNodeName = ws.ExpandEnvironmentStrings("%COMPUTERNAME%") Wscript.Echo "This Machine Name - " & strNodeName set WS = Nothing '########## GET A LIST OF ALL EXCHANGE SERVERS '########## AND FIND THE LOCAL SERVER'S EXCHANGE VS NAME '########## IN CASE THIS IS A CLUSTER Const strComputerName = "LocalHost" Const WMINameSpace = "root/cimv2/applications/exchange" Const WMIInstance = "ExchangeServerState" Set ExchangeList = GetObject("winmgmts:{impersonationLevel=impersonate}!//" _ & strComputerName & "/" & WMINameSpace).InstancesOf (WMIInstance) For each ExchangeServer in ExchangeList If Instr(Ucase(strNodeName), Ucase(ExchangeServer.Name)) Then strExServerName = ExchangeServer.Name Next Wscript.Echo "Exchange Server Name = " & strExServerName '########## CHECK PASSED ARGUMENTS If Ucase(strSwitch1) = "GETLOG" Then 'ARGUMENT CHECK FetchLog strSwitch2,strSwitch3 ElseIf Ucase(strSwitch1) = "MAKESUMMARY" Then 'ARGUMENT CHECK fnMakeLogSummary Else '########## MAKE SURE TASK SCHEDULER SERVICE IS ENABLED AND STARTED strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\cimv2") Set colServiceList = objWMIService.ExecQuery _ ("Select * from Win32_Service where Name = 'Schedule'") For Each objService in colServiceList Wscript.Echo objService.Name errReturnCode = objService.Change( , , , , "Automatic") If objService.State <> "Running" Then objService.StartService() Else objService.StopService() Wscript.Echo "Stopping Task Scheduler" 'RESTARTING THE SCHEDULE SERVICE SO THAT AT JOB IDs DON'T GROW HIGH, PURELY ESTHETICAL Wscript.Sleep 5000 objService.StartService() Wscript.Echo "Starting Task Scheduler" Wscript.Sleep 5000 End If Next '########### CHECK PREVIOUS BACKUP SIZE, will be used in the BackupPlan e-mail If Ucase(strSwitch1) = "BACKUPNOW" Then Set fso = CreateObject("Scripting.FileSystemObject") If fso.FolderExists("G:\NTBACKUP\Exchange Backups") = True Then Set fldExchangeBackups = fso.GetFolder("G:\NTBACKUP\Exchange Backups") wscript.echo "LAST BACKUP SIZE = " & fldExchangeBackups.Size iLastBackupSize = fldExchangeBackups.Size ' msgBodyText = msgBodyText & "Size of Last Backup = " & FormatNumber(iLastBackupSize/1024/1024/1024,2) & "GB" & VbCrLf Else iLastBackupSize = -1 'this will mean that the backup has not been run before End If Set fso = Nothing End If '########### CREATE G:\NTBACKUP\Exchange Backups IF NECESSARY Set wshShell = WScript.CreateObject ("WSCript.shell") ShellCommand = "CMD /C ""IF NOT EXIST G:\NTBACKUP md G:\NTBACKUP""" wshshell.run ShellCommand,0,True ShellCommand = "CMD /C ""IF NOT EXIST ""G:\NTBACKUP\Exchange Backups"" md ""G:\NTBACKUP\Exchange Backups""""" wshshell.run ShellCommand,0,True set wshshell = nothing '########### CLEAN OUT G:\NTBACKUP directory Set wshShell = WScript.CreateObject ("WSCript.shell") ShellCommand = "CMD /C ""IF EXIST G:\NTBACKUP\CMDs\NUL rd G:\NTBACKUP\CMDs /s /q""" wshshell.run ShellCommand,0,True ShellCommand = "CMD /C ""IF EXIST G:\NTBACKUP\SELECTIONS\NUL rd G:\NTBACKUP\SELECTIONS /s /q""" wshshell.run ShellCommand,0,True ShellCommand = "CMD /C ""IF EXIST G:\NTBACKUP\LOGS\NUL rd G:\NTBACKUP\LOGS /s /q""" wshshell.run ShellCommand,0,True If UCase(strSwitch1) = "BACKUPNOW" Then If UCase(strSwitch2) = "ROTATE2" Then ShellCommand = "CMD /C ""IF EXIST ""G:\NTBACKUP\Exchange Backups 2"" RD ""G:\NTBACKUP\Exchange Backups 2"" /s /q""" wshshell.run ShellCommand,0,True ShellCommand = "CMD /C ""REN ""G:\NTBACKUP\Exchange Backups"" ""Exchange Backups 2""""" wshshell.run ShellCommand,0,True Else ShellCommand = "CMD /C ""DEL ""G:\NTBACKUP\Exchange Backups\*.*"" /Q""" wshshell.run ShellCommand,0,True End If End If set wshshell = nothing '########### SHARE THE G:\NTBACKUP\Exchange Backups DIRECTORY Set wshShell = WScript.CreateObject ("WSCript.shell") ShellCommand = "NET SHARE ""Exchange Backups""=""G:\NTBACKUP\Exchange Backups"" /GRANT:AD\Arcada_NA,CHANGE /GRANT:NA-FTL\Arcada,CHANGE" wshshell.run ShellCommand,0,True set wshshell = nothing '########## START SRS AND STORAGE GROUP DISCOVERY DoSRS strExServerName 'call to function that will check if SRS is present and back it up if true Set objCDOEXMExchangeServer = CreateObject("CDOEXM.ExchangeServer") objCDOEXMExchangeServer.DataSource.Open(strExServerName) Set objStorageGroup = CreateObject("CDOEXM.StorageGroup") For Each urlStorageGroup In objCDOEXMExchangeServer.StorageGroups objStorageGroup.DataSource.Open (urlStorageGroup) Wscript.Echo "Storage Group Name -", objStorageGroup.Name EnumMailboxStoreDBs objStorageGroup,strExServerName 'EnumPublicStoreDBs objStorageGroup,strExServerName Next fnMakeBackupPlan 'call to function fnMakeBackupPlan that will create a list of backup jobs for summary reference SetNextTask 'call to function that will schedule the backup for next time End If 'ARGUMENT CHECK Else 'CHECKING IF ACTIVE NODE SetNextTask 'call to function that will schedule the backup for next time End If 'CHECKNG IF ACTIVE NODE '##################################################### '#################### FUNCTIONS ###################### '##################################################### '##################################################### '########## SCHEDULE SELF FOR NEXT RUN Private Function SetNextTask() iDay = DatePart("w", Date()) SELECT CASE iDay Case 6 strNextRun = "M" : strNextSummary = "S" Case 7 strNextRun = "M" : strNextSummary = "Su" Case 1 strNextRun = "M" : strNextSummary = "M" Case 2 strNextRun = "T" : strNextSummary = "T" Case 3 strNextRun = "W" : strNextSummary = "W" Case 4 strNextRun = "Th" : strNextSummary = "Th" Case 5 strNextRun = "F" : strNextSummary = "F" END SELECT 'strComputer = "." 'strNextRun = "1" 'Set objWMIService = GetObject("winmgmts:" _ ' & "{impersonationLevel=impersonate}!\\" _ ' & strComputer & "\root\cimv2") 'Set objNewJob = objWMIService.Get("Win32_ScheduledJob") 'errJobCreated = objNewJob.Create _ ' ("%WINDIR%\System32\cscript.exe D:\NTBACKUP\BackupExchange.vbs BackupNow","********180000.000000-240",FALSE,,strNextRun,FALSE,111) 'If errJobCreated <> 0 Then 'Wscript.Echo "Error on task creation" 'Else 'Wscript.Echo "Task created" 'End If '########## FIND AND DELETE IDENTICAL AT TASK IF IT EXISTS Set objShell = CreateObject("WScript.Shell") Set objExec = objShell.Exec("AT") Do Until objExec.Stdout.atEndOfStream strCurrAT = objExec.StdOut.ReadLine If Instr(UCase(strCurrAT),"\NTBACKUP\BACKUPEXCHANGE.VBS BACKUPNOW") Then If Instr(UCase(strCurrAT),Ucase("Next " & strNextRun)) Then strCurrAT = Trim(strCurrAT) splitAT = Split(strCurrAT," ") strDupTaskID = splitAT(0) ShellCommand = "AT " & strDupTaskID & " /DEL" objShell.run ShellCommand,0,True End If ElseIf Instr(UCase(strCurrAT),"\NTBACKUP\BACKUPEXCHANGE.VBS MAKESUMMARY") Then If Instr(UCase(strCurrAT),Ucase("Next " & strNextSummary)) Then strCurrAT = Trim(strCurrAT) splitAT = Split(strCurrAT," ") strDupTaskID = splitAT(0) ShellCommand = "AT " & strDupTaskID & " /DEL" objShell.run ShellCommand,0,True End If End If Loop set objShell = Nothing Set wshShell = WScript.CreateObject ("WSCript.shell") If UCase(strSwitch1) = "BACKUPNOW" Then If UCase(strSwitch2) = "ROTATE2" Then Wscript.Echo "Flags will be BackupNow Rotate2" ShellCommand = "AT 18:00 /NEXT:" & strNextRun & " %WINDIR%\System32\cscript.exe D:\NTBACKUP\BackupExchange.vbs BackupNow Rotate2" ShellCommand2 = "AT 7:00 /NEXT:" & strNextSummary & " %WINDIR%\System32\cscript.exe D:\NTBACKUP\BackupExchange.vbs MakeSummary" Else Wscript.Echo "Flags will be BackupNow" ShellCommand = "AT 18:00 /NEXT:" & strNextRun & " %WINDIR%\System32\cscript.exe D:\NTBACKUP\BackupExchange.vbs BackupNow" ShellCommand2 = "AT 7:00 /NEXT:" & strNextSummary & " %WINDIR%\System32\cscript.exe D:\NTBACKUP\BackupExchange.vbs MakeSummary" End If ElseIf UCase(strSwitch1) = "ROTATE2" Then Wscript.Echo "Flags will be BackupNow Rotate2" ShellCommand = "AT 18:00 /NEXT:" & strNextRun & " %WINDIR%\System32\cscript.exe D:\NTBACKUP\BackupExchange.vbs BackupNow Rotate2" ShellCommand2 = "AT 7:00 /NEXT:" & strNextSummary & " %WINDIR%\System32\cscript.exe D:\NTBACKUP\BackupExchange.vbs MakeSummary" Else Wscript.Echo "Flags will be BackupNow" ShellCommand = "AT 18:00 /NEXT:" & strNextRun & " %WINDIR%\System32\cscript.exe D:\NTBACKUP\BackupExchange.vbs BackupNow" ShellCommand2 = "AT 7:00 /NEXT:" & strNextSummary & " %WINDIR%\System32\cscript.exe D:\NTBACKUP\BackupExchange.vbs MakeSummary" End If wshshell.run ShellCommand,0,True wshshell.run ShellCommand2,0,True set wshshell = nothing End Function '########## ENUMERATE ALL MBX STORES IN THE STORAGE GROUP, '########## CONFIGURE COMMAND AND SELECTION FILES, '########## RUN BACKUP OF THE STORAGE GROUP Private Function EnumMailboxStoreDBs (objStorageGroup,strExServerName) Set objMailboxStoreDB = CreateObject("CDOEXM.MailBoxStoreDB") Dim objFSO, CmdFile, SelectionFile Set objFSO = CreateObject("Scripting.FileSystemObject") If objFSO.FolderExists("G:\NTBACKUP") = False Then objFSO.CreateFolder("G:\NTBACKUP") If objFSO.FolderExists("G:\NTBACKUP\CMDs") = False Then objFSO.CreateFolder("G:\NTBACKUP\CMDs") If objFSO.FolderExists("G:\NTBACKUP\SELECTIONS") = False Then objFSO.CreateFolder("G:\NTBACKUP\SELECTIONS") If objFSO.FolderExists("G:\NTBACKUP\LOGS") = False Then objFSO.CreateFolder("G:\NTBACKUP\LOGS") strSGCMDFileName = "G:\NTBACKUP\CMDs\" & objStorageGroup.Name & ".cmd" Set SGCmdFile = objFSO.CreateTextFile(strSGCMDFileName, True) For Each urlMailboxStoreDB In objStorageGroup.MailboxStoreDBs objMailboxStoreDB.DataSource.Open (urlMailboxStoreDB) Wscript.Echo " Store Name - ", objMailboxStoreDB.Name CreateBackupJob objMailboxStoreDB.Name,objStorageGroup.Name,strExServerName strCMDLine = "SET STARTTIME=%DATE% %TIME: =0%" SGCmdFile.WriteLine(strCmdLine) strCmdLine = "CALL ""G:\NTBACKUP\CMDs\" & objMailboxStoreDB.Name & """" SGCmdFile.WriteLine(strCmdLine) strCmdLine = "%WINDIR%\System32\cscript.exe D:\NTBACKUP\BackupExchange.vbs GETLOG " & """" & objMailboxStoreDB.Name & """" & " " & """" & CHR(37) & "STARTTIME" & CHR(37) & """" SGCmdFile.WriteLine(strCmdLine) ' strCMDLine = "PAUSE" ' SGCmdFile.WriteLine(strCmdLine) Next '########## CHECK IF STORAGE GROUP ALSO HAS A PUBLIC FOLDER STORE EnumPublicStoreDBs objStorageGroup,strExServerName,SGCmdFile SGCmdFile.Close Set objFSO = Nothing If Ucase(strSwitch1) = "BACKUPNOW" Then Set wshShell = WScript.CreateObject ("WSCript.shell") ShellCommand = """" & strSGCMDFileName & """" wshShell.run ShellCommand,,False set wshshell = nothing End If Wscript.Sleep 2000 '2 second delay before firing next storage group backup End Function '########## ENUMERATE ALL PUB STORES IN THE STORAGE GROUP Private Function EnumPublicStoreDBs (objStorageGroup,strExServerName,SGCmdFile) Set objPublicStoreDB = CreateObject("CDOEXM.PublicStoreDB") For Each urlPublicStoreDB In objStorageGroup.PublicStoreDBs objPublicStoreDB.DataSource.Open (urlPublicStoreDB) Wscript.Echo " Store Name - ", objPublicStoreDB.Name CreateBackupJob objPublicStoreDB.Name,objStorageGroup.Name,strExServerName strCMDLine = "SET STARTTIME=%DATE% %TIME: =0%" SGCmdFile.WriteLine(strCmdLine) strCmdLine = "CALL ""G:\NTBACKUP\CMDs\" & objPublicStoreDB.Name & """" SGCmdFile.WriteLine(strCmdLine) strCmdLine = "%WINDIR%\System32\cscript.exe D:\NTBACKUP\BackupExchange.vbs GETLOG " & """" & objPublicStoreDB.Name & """" & " " & """" & CHR(37) & "STARTTIME" & CHR(37) & """" SGCmdFile.WriteLine(strCmdLine) ' strCMDLine = "PAUSE" ' SGCmdFile.WriteLine(strCmdLine) Next End Function '########## BACKUP SRS IF PRESENT Private Function DoSRS (strExServerName) strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\cimv2") Set colServiceList = objWMIService.ExecQuery _ ("Select * from Win32_Service where Name = 'MSExchangeSRS'") For Each objService in colServiceList Wscript.Echo objService.Name & " - " & objService.StartMode If objService.StartMode <> "Disabled" Then Dim objFSO, CmdFile, SelectionFile Set objFSO = CreateObject("Scripting.FileSystemObject") If objFSO.FolderExists("G:\NTBACKUP") = False Then objFSO.CreateFolder("G:\NTBACKUP") If objFSO.FolderExists("G:\NTBACKUP\CMDs") = False Then objFSO.CreateFolder("G:\NTBACKUP\CMDs") If objFSO.FolderExists("G:\NTBACKUP\SELECTIONS") = False Then objFSO.CreateFolder("G:\NTBACKUP\SELECTIONS") If objFSO.FolderExists("G:\NTBACKUP\LOGS") = False Then objFSO.CreateFolder("G:\NTBACKUP\LOGS") strSelectionFileName = "G:\NTBACKUP\SELECTIONS\" & strExServerName & "-SRS.bks" Set SelectionFile = objFSO.CreateTextFile(strSelectionFileName, True) SelectionFile.Close strSelection = "JET " & strExServerName & "\Microsoft Site Replication Service\SRS Storage\" '########## NEED TO PIPE THIS THE SELECTION TEXT THROUGH CMD /U to get a UNICODE STRING Set wshShell = WScript.CreateObject ("WSCript.shell") ShellCommand = "cmd /u /c echo " & strSelection & ">" & """" & strSelectionFileName & """" wscript.echo ShellCommand wshshell.run ShellCommand,0,True set wshShell = nothing strCmdFileName = "G:\NTBACKUP\CMDs\" & strExServerName & "-SRS.cmd" Set CmdFile = objFSO.CreateTextFile(strCmdFileName, True) strCmdLine = "for /f ""tokens=2,3,4,5,6 delims=.:/ "" %%A IN (""%DATE%_%TIME: =0%"") DO SET DATETIME=-%%A-%%B-%%C-%%D-%%E" CmdFile.WriteLine(strCmdLine) strCMDLine = "SET STARTTIME=%DATE% %TIME: =0%" CmdFile.WriteLine(strCmdLine) strCmdLine = "reg add ""HKCU\Software\Microsoft\Ntbackup\Log Files"" /v ""Log File Count"" /t REG_DWORD /d 21 /f" CmdFile.WriteLine(strCmdLine) strCmdLine = "reg add ""HKCU\Software\Microsoft\Ntbackup\Backup Engine"" /v ""Logical Disk Buffer Size"" /t REG_SZ /d 64 /f" CmdFile.WriteLine(strCmdLine) strCmdLine = "reg add ""HKCU\Software\Microsoft\Ntbackup\Backup Engine"" /v ""Max Buffer Size"" /t REG_SZ /d 1024 /f" CmdFile.WriteLine(strCmdLine) strCmdLine = "reg add ""HKCU\Software\Microsoft\Ntbackup\Backup Engine"" /v ""Max Num Tape Buffers"" /t REG_SZ /d 16 /f" CmdFile.WriteLine(strCmdLine) strCmdLine = "%WINDIR%\system32\Ntbackup.exe backup ""@G:\NTBACKUP\SELECTIONS\" & strExServerName & "-SRS.bks"" /n """ & strExServerName & "-SRS" & """ /d """ & strExServerName & "-SRS" & """ /v:no /r:no /rs:no /hc:off /fu /m normal /j """ & strExServerName & "-SRS" & """ /l:s /f ""G:\NTBACKUP\Exchange Backups\" & strExServerName & "-SRS%DATETIME%.bkf""" CmdFile.WriteLine(strCmdLine) strCmdLine = "%WINDIR%\System32\cscript.exe D:\NTBACKUP\BackupExchange.vbs GETLOG " & """" & strExServerName & "-SRS" & """" & " " & """" & CHR(37) & "STARTTIME" & CHR(37) & """" CmdFile.WriteLine(strCmdLine) CmdFile.Close Set objFSO = Nothing If Ucase(strSwitch1) = "BACKUPNOW" Then Set wshShell = WScript.CreateObject ("WSCript.shell") ShellCommand = """" & strCmdFileName & """" wshShell.run ShellCommand,,False set wshshell = nothing End If End If Next End Function '########################################################################################################################### Private Function CreateBackupJob (strBackupName,strStorageGroup,strExServerName) Dim objFSO, CmdFile, SelectionFile Set objFSO = CreateObject("Scripting.FileSystemObject") If objFSO.FolderExists("G:\NTBACKUP") = False Then objFSO.CreateFolder("G:\NTBACKUP") If objFSO.FolderExists("G:\NTBACKUP\CMDs") = False Then objFSO.CreateFolder("G:\NTBACKUP\CMDs") If objFSO.FolderExists("G:\NTBACKUP\SELECTIONS") = False Then objFSO.CreateFolder("G:\NTBACKUP\SELECTIONS") strSelectionFileName = "G:\NTBACKUP\SELECTIONS\" & strBackupName & ".bks" Set SelectionFile = objFSO.CreateTextFile(strSelectionFileName, True) 'SelectionFile.WriteLine(strSelection) SelectionFile.Close strSelection = "JET " & strExServerName & "\Microsoft Information Store\" & strStorageGroup & "\" & strBackupName '########## NEED TO PIPE THIS THE SELECTION TEXT THROUGH CMD /U to get a UNICODE STRING Set wshShell = WScript.CreateObject ("WSCript.shell") ShellCommand = "cmd /u /c echo " & strSelection & ">" & """" & strSelectionFileName & """" wscript.echo ShellCommand wshshell.run ShellCommand,0,True set wshshell = nothing strCmdFileName = "G:\NTBACKUP\CMDs\" & strBackupName & ".cmd" Set CmdFile = objFSO.CreateTextFile(strCmdFileName, True) strCmdLine = "for /f ""tokens=2,3,4,5,6 delims=.:/ "" %%A IN (""%DATE%_%TIME: =0%"") DO SET DATETIME=-%%A-%%B-%%C-%%D-%%E" CmdFile.WriteLine(strCmdLine) strCmdLine = "reg add ""HKCU\Software\Microsoft\Ntbackup\Log Files"" /v ""Log File Count"" /t REG_DWORD /d 21 /f" CmdFile.WriteLine(strCmdLine) strCmdLine = "reg add ""HKCU\Software\Microsoft\Ntbackup\Backup Engine"" /v ""Logical Disk Buffer Size"" /t REG_SZ /d 64 /f" CmdFile.WriteLine(strCmdLine) strCmdLine = "reg add ""HKCU\Software\Microsoft\Ntbackup\Backup Engine"" /v ""Max Buffer Size"" /t REG_SZ /d 1024 /f" CmdFile.WriteLine(strCmdLine) strCmdLine = "reg add ""HKCU\Software\Microsoft\Ntbackup\Backup Engine"" /v ""Max Num Tape Buffers"" /t REG_SZ /d 16 /f" CmdFile.WriteLine(strCmdLine) strCmdLine = "%WINDIR%\system32\Ntbackup.exe backup ""@G:\NTBACKUP\SELECTIONS\" & strBackupName & ".bks"" /n """ & strBackupName & """ /d """ & strBackupName & """ /v:no /r:no /rs:no /hc:off /fu /m normal /j """ & strBackupName & """ /l:s /f ""G:\NTBACKUP\Exchange Backups\" & strBackupName & "%DATETIME%.bkf""" CmdFile.WriteLine(strCmdLine) CmdFile.Close Set objFSO = Nothing End Function '######### GET THE BACKUP LOG Private Function FetchLog(strJobName,strDateTime) Wscript.Echo "FETCH DATETIME = " & strDateTime const HKEY_LOCAL_MACHINE = &H80000002 const HKEY_CURRENT_USER = &H80000001 On Error Resume Next strComputer = "." Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_ strComputer & "\root\default:StdRegProv") oReg.GetDWORDValue HKEY_LOCAL_MACHINE,"SYSTEM\CurrentControlSet\Control\TimeZoneInformation","ActiveTimeBias",dwTimeZone Wscript.Echo "Time Zone " & dwTimeZone strDateTime = Mid(strDateTime,InStr(strDateTime," ")+1) strDateTime = Mid(strDateTime,1,InStr(strDateTime,".")-1) wscript.echo "Passed JOB NAME " & strJobName Wscript.Echo "Passed DATE TIME " & strDateTime iBackupDateTimeStamp = DateDiff("s", "01/01/1970 00:00:00", strDateTime) '##### ADJUST TIMESTAMP FOR TIMEZONE iBackupDateTimeStamp = iBackupDateTimeStamp + dwTimeZone*60 Wscript.Echo "Passed Time Stamp " & iBackupDateTimeStamp strKeyPath = "Software\Microsoft\Ntbackup\Log Files" Wscript.Echo strKeyPath oReg.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubKeys For Each subkey In arrSubKeys strSubKeyPath = strKeyPath & "\" & subkey Wscript.Echo "Log Subkey Path " & strSubKeyPath oReg.GetStringValue HKEY_CURRENT_USER,strSubKeyPath,"Job Name",strRegJobName Wscript.Echo "FOUND REGISTRY JOB NAME " & strRegJobName oReg.GetDWORDValue HKEY_CURRENT_USER,strSubKeyPath,"Date/Time Used",iRegDateTime Wscript.Echo "FOUND REGISTRY JOB DATE-TIME " & iRegDateTime If Ucase(strRegJobName) = Ucase(strJobName) Then If iRegDateTime >= iBackupDateTimeStamp Then strSplitLogKey = Split(subkey,"#") End If End If Next strLogNumber = strSplitLogKey(1) Wscript.echo strLogNumber SELECT CASE strLogNumber Case 1 strLogNumber = "01" Case 2 strLogNumber = "02" Case 3 strLogNumber = "03" Case 4 strLogNumber = "04" Case 5 strLogNumber = "05" Case 6 strLogNumber = "06" Case 7 strLogNumber = "07" Case 8 strLogNumber = "08" Case 9 strLogNumber = "09" END SELECT Wscript.Echo "Adjusted log number " & strLogNumber strLogName = "backup" & strLogNumber & ".log" wscript.Echo "Log Name " & strLogName set WshShell = WScript.CreateObject("WScript.Shell") Set WshSysEnv = WshShell.Environment("PROCESS") For Each Env in WshSysEnv splitEnv = Split(Env,"=") If Ucase(splitEnv(0)) = "USERPROFILE" Then strProfilePath = splitEnv(1) next set WshShell = Nothing wscript.echo "Profile Path " & strProfilePath strBackupLogPath = strProfilePath & "\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data" Wscript.Echo "Backup Log Path " & strBackupLogPath Set fso = CreateObject("Scripting.FileSystemObject") Set fLogFile = fso.GetFile(strBackupLogPath & "\" & strLogName) strLogCopy = "G:\NTBACKUP\LOGS\" & strJobName & "-" & strDateTime & ".log" strLogCopy = Replace(strLogCopy,"/","_") strLogCopy = Replace(strLogCopy,":","_") strLogCopy = Replace(strLogCopy,"G_\","G:\") Wscript.Echo "COPYING LOG TO " & strLogCopy fLogFile.Copy(strLogCopy) Set fMailFile = fso.OpenTextFile(strBackupLogPath & "\" & strLogName,1,FALSE,-1) '-1 means open as UNICODE 'The ReadAll method reads the entire file into the variable BodyText msgBodyText = fMailFile.ReadAll 'Close the read log file fMailFile.Close Set fMailFile = Nothing Dim objMessage Set objMessage = CreateObject("CDO.Message") objMessage.Subject = strJobName & " " & strDateTime objMessage.From = """NTBACKUP-" & strExServerName & """" & " " objMessage.To = LogRecipientAddress wscript.echo "Backup Log Message will be sent to " & objMessage.To objMessage.TextBody = msgBodyText objMessage.AddAttachment strLogCopy objMessage.Send Set msgBodyText = Nothing Set objMessage = Nothing 'Set fConvertLogFile = fso.OpenTextFile(strLogCopy,1,FALSE,-1) '-1 means open as UNICODE ''The ReadAll method reads the entire file into the variable BodyText 'strText = fConvertLogFile.ReadAll ''Close the read log file 'fConvertLogFile.Close 'Set fConvertLogFile = fso.OpenTextFile(strLogCopy,2,True,0) 'fConvertLogFile.Write(strText) 'fConvertLogFile.Close 'Set fConvertLogFile = Nothing Set fLogFile = Nothing Set fso = Nothing End Function '######################################################## '########## WRITE BackupPlan.txt file and send an e-mail Private Function fnMakeBackupPlan() If Ucase(strSwitch1) = "BACKUPNOW" Then Dim msgBodyText Set objWMIService = GetObject("winmgmts:") Set objLogicalDisk = objWMIService.Get("Win32_LogicalDisk.DeviceID='G:'") iTotalVolumeSize = objLogicalDisk.Size msgBodyText = msgBodyText & "Total Size of Backup Volume = " & FormatNumber(iTotalVolumeSize/1024/1024/1024,2) & "GB
" & VbCrLf iFreeSpace = objLogicalDisk.FreeSpace msgBodyText = msgBodyText & "Free Space on Backup Volume = " & FormatNumber(iFreeSpace/1024/1024/1024,2) & "GB
" & VbCrLf Set objWMIService = Nothing If iLastBackupSize >= 0 Then msgBodyText = msgBodyText & "Size of Last Backup = " & FormatNumber(iLastBackupSize/1024/1024/1024,2) & "GB
" & VbCrLf ElseIf iLastBackupSize = -1 Then msgBodyText = msgBodyText & VbCrLf & "
No previous backup found
" & VbCrLf End If If iFreeSpace < 1073741824 Then msgBodyText = msgBodyText & VbCrLf & "
There is less than 1GB of free disk space available. Next backup may not fit.
" & VbCrLf ElseIf iFreeSpace =< iLastBackupSize Then If Ucase(strSwitch2) = "ROTATE2" Then msgBodyText = msgBodyText & VbCrLf & "
There is not enough disk space for two-day rotation!!!
" & VbCrLf End If ElseIf (iTotalVolumeSize - iLastBackupSize) < 1073741824 Then msgBodyText = msgBodyText & VbCrLf & "
After last backup there was less than 1GB of free disk space available. This backup may not fit.
" & VbCrLf End If 'wscript.echo msgBodyText msgBodyText = msgBodyText & VbCrLf & "
STARTING BACKUP OF THESE INFORMATION STORES:
" & VbCrLf Set fso = CreateObject("Scripting.FileSystemObject") Set fldSelections = fso.GetFolder("G:\NTBACKUP\SELECTIONS") Set fFiles = fldSelections.files Set fBackupPlan = fso.CreateTextFile("G:\NTBACKUP\LOGs\BackupPlan.txt",True) For each fItem in fFiles strSelFileName = Left(fItem.Name, InstrRev(fItem.Name,".")-1) fBackupPlan.WriteLine(strSelFileName) msgBodyText = msgBodyText & ">>> " & strSelFileName & "
" & vbCRLF Next fBackupPlan.Close Set fBackupPlan = Nothing set fFiles = Nothing Set fldSelections = Nothing Set fso = Nothing Dim objMessage Set objMessage = CreateObject("CDO.Message") objMessage.Subject = "Starting Exchange Daily Backup to Disk" objMessage.From = """NTBACKUP-" & strExServerName & """" & " " objMessage.To = ReportRecipientAddress wscript.echo "Backup Plan Message will be sent to " & objMessage.To objMessage.HTMLBody = "" & msgBodyText & "" ' objMessage.TextBody = msgBodyText objMessage.Send Set msgBodyText = Nothing Set objMessage = Nothing End If End Function '########################################################## '########### CREATE LOG SUMMARY Private Function fnMakeLogSummary() Dim arrBadLogs() iNewTop = 0 Set fso = CreateObject("Scripting.FileSystemObject") Set fldLOGs = fso.GetFolder("G:\NTBACKUP\LOGs") Set fFiles = fldLOGs.files Set fBackupPlan = fso.OpenTextFile("G:\NTBACKUP\LOGs\BackupPlan.txt",1) Set fBackupSummary = fso.CreateTextFile("G:\NTBACKUP\LOGs\BackupSummary.txt",1) do until fBackupPlan.AtEndOfStream strPlanLine = fBackupPlan.Readline strConfirmed = ">>> BACKUP LOG FOR [" & strPlanLine & "] IS MISSING!!!
" strErrorReport = "
" For Each fLogFile in fFiles If Instr(Ucase(fLogFile.Name),Ucase(strPlanLine))>0 Then strConfirmed = ">>> Backup log for [" & strPlanLine & "] is present. Backup ran.
" If fLogFile.Size > 2 Then Set fReadLogFile = fso.OpenTextFile(fLogFile,1,FALSE,-1) '-1 for "As Unicode" strText = fReadLogFile.ReadAll fReadLogFile.Close Set fReadLogFile = fso.OpenTextFile(fLogFile,2,True,0) fReadLogFile.Write(strText) Set strText = Nothing fReadLogFile.Close Set fReadLogFile = fso.OpenTextFile(fLogFile,1) strReadLogText = fReadLogFile.ReadAll If Instr(Ucase(strReadLogText),"FAIL")>0 Then strErrorReport = "*** ERRORS WERE REPORTED IN THE [" & strPlanLine & "] LOG FILE *** Check attachment.

" ReDim Preserve arrBadLogs(iNewTop) arrBadLogs(iNewTop) = fLogFile iNewTop = iNewTop+1 ElseIf Instr(Ucase(strReadLogText),"ERROR")>0 Then strErrorReport = "*** ERRORS WERE REPORTED IN THE [" & strPlanLine & "] LOG FILE *** Check Attachment.

" ReDim Preserve arrBadLogs(iNewTop) arrBadLogs(iNewTop) = fLogFile iNewTop = iNewTop+1 Else strErrorReport = "*** Backup was successful.

" End If Else strErrorReport = "*** POSSIBLE PROBLEM - [" & strPlanLine & "] LOG FILE IS EMPTY *** Backup job may have not finished.

" End If End If Next fBackupSummary.WriteLine(strConfirmed) fBackupSummary.WriteLine(strErrorReport) fBackupSummary.WriteLine(vbCrLf) loop Set fFiles = Nothing fBackupPlan.Close Set fBackupPlan = Nothing If fso.FolderExists("G:\NTBACKUP\Exchange Backups") = True Then Set fldExchangeBackups = fso.GetFolder("G:\NTBACKUP\Exchange Backups") wscript.echo "LAST BACKUP SIZE = " & fldExchangeBackups.Size iLastBackupSize = fldExchangeBackups.Size Else iLastBackupSize = 0 End If Set objWMIService = GetObject("winmgmts:") Set objLogicalDisk = objWMIService.Get("Win32_LogicalDisk.DeviceID='G:'") iTotalVolumeSize = objLogicalDisk.Size strAppend = strAppend & VbCrLf & VbCrLf & "

Total Size of Backup Volume = " & FormatNumber(iTotalVolumeSize/1024/1024/1024,2) & "GB
" & VbCrLf iFreeSpace = objLogicalDisk.FreeSpace strAppend = strAppend & "Free Space on Backup Volume = " & FormatNumber(iFreeSpace/1024/1024/1024,2) & "GB
" & VbCrLf Set objWMIService = Nothing If iLastBackupSize >= 0 Then strAppend = strAppend & "Size of Last Backup = " & FormatNumber(iLastBackupSize/1024/1024/1024,2) & "GB
" & VbCrLf ElseIf iLastBackupSize = -1 Then strAppend = strAppend & VbCrLf & "
No previous backup found
" & VbCrLf End If If iFreeSpace < 1073741824 Then strAppend = strAppend & VbCrLf & "
There is less than 1GB of free disk space available. Next backup may not fit.
" & VbCrLf ElseIf iFreeSpace =< iLastBackupSize Then If Ucase(strSwitch2) = "ROTATE2" Then strAppend = strAppend & VbCrLf & "
There is not enough disk space for two-day rotation!!!
" & VbCrLf End If End If fBackupSummary.WriteLine(strAppend) fBackupSummary.Close Set fBackupSummary = fso.OpenTextFile("G:\NTBACKUP\LOGs\BackupSummary.txt",1) msgBodyText = fBackupSummary.ReadAll fBackupSummary.Close Set fBackupSummary = Nothing strSubjectAppendError = "" strSubjectAppendSuccess = " - All Backup Jobs Completed Successfully" If Instr(Ucase(msgBodyText),"ERROR")>0 Then strSubjectAppendError = strSubjectAppendError & " - One or More Jobs Completed with Errors" strSubjectAppendSuccess = "" End If If Instr(Ucase(msgBodyText),"PROBLEM")>0 Then strSubjectAppendError = strSubjectAppendError & " - One or More Jobs May be Hung" strSubjectAppendSuccess = "" End If If Instr(Ucase(msgBodyText),"MISSING")>0 Then strSubjectAppendError = strSubjectAppendError & " - One or More Logs is Missing" strSubjectAppendSuccess = "" End If Dim objMessage Set objMessage = CreateObject("CDO.Message") objMessage.Subject = "Backup Summary" & strSubjectAppendSuccess & strSubjectAppendError objMessage.From = """NTBACKUP-" & strExServerName & """" & " " objMessage.To = ReportRecipientAddress wscript.echo "Backup Summary Message will be sent to " & objMessage.To objMessage.HTMLBody = "" & msgBodyText & "" 'objMessage.TextBody = msgBodyText For Each BadLog in arrBadLogs Wscript.Echo "BAD LOG = " & BadLog objMessage.AddAttachment BadLog Next objMessage.Send Set msgBodyText = Nothing Set objMessage = Nothing Set strErrorReport = NOthing fReadLogFile.Close Set fReadLogFile = Nothing Set strReadLogText = Nothing Set fso = Nothing End Function