Samstag, 19. Dezember 2009

ISA 2006 Backup Script

' based on: http://www.it-training-grote.de/download/isabackup/backup.htm

' Usage: cscript /nologo ISABackup.vbs strBackupShare

' - strBackupShare (String) UNC Path of the Share where the Backup will be stored


' Should the Script send any notification E-Mails
Const blSendMails = true

' The E-Mail Server to use when sending Backup notifications
Const strEmailServer = server.local"

' The E-Mail Address from which the Mails will appear to come from
Const strEmailFromAddr = "postmaster@domain.tld"

' The Recipients od the Backup notifications. Multiple Recipients are to be separated with a semicolon.
Const strEmailRecipients = "info@domain.tld"

Private Sub SendEmail (ByVal strFrom, strRecipient, strSubject, strText, strServer, isUrgent)

Dim objEmail

Set objEmail = CreateObject("CDO.Message")

objEmail.From = strFrom
objEmail.To = strRecipient
objEmail.Subject = strSubject
objEmail.Textbody = strText

If isUrgent Then

With objEmail.Fields

.Item("urn:schemas:mailheader:X-MSMail-Priority") = "High"
.Item("urn:schemas:mailheader:X-Priority") = 2
.Item("urn:schemas:httpmail:importance") = 2
.Update

End With

End If

With objEmail.Configuration.Fields

.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strServer
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update

End With

objEmail.Send

End Sub

Function LogConsoleOutput (ByVal strText)

strText = GetTimeString & ": " & strText

objEventLogFile.WriteLine strText

strConsoleOutput = strConsoleOutput & strText & vbCrLf

WScript.echo strText

End Function

Function AppendZeroToTen (byVal Input)

If (Input < 10) Then
AppendZeroToTen = "0" & Input
Else
AppendZeroToTen = Input
End If

End Function

Function GetDateString

Dim dtmCurrentDate, dtmDay, dtmMonth, dtmYear

dtmCurrentDate = date()

dtmDay = DatePart("d", dtmCurrentDate)
dtmMonth = DatePart("m", dtmCurrentDate)
dtmYear = DatePart("yyyy", dtmCurrentDate)

GetDateString = AppendZeroToTen(dtmDay) & "." &_
AppendZeroToTen(dtmMonth) & "." &_
AppendZeroToTen(dtmYear)

End Function

Function GetTimeString

Dim dtmCurrentTime, dtmHour, dtmMinute, dtmSecond

dtmCurrentTime = time()

dtmHour = DatePart("h", dtmCurrentTime)
dtmMinute = DatePart("n", dtmCurrentTime)
dtmSecond = DatePart("s", dtmCurrentTime)

GetTimeString = AppendZeroToTen(dtmHour) & ":" &_
AppendZeroToTen(dtmMinute) & ":" &_
AppendZeroToTen(dtmSecond)

End Function

Function DeleteIfExists (byVal strFile)

if objFSO.FileExists(strFile) Then

objFSO.DeleteFile(strFile)

End If

End Function

Dim objFSO : set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objNetWork : set objNetWork = CreateObject("WScript.Network")
Dim strBackupShare : strBackupShare = WScript.Arguments(0)
Dim objXMLDOM : set objXMLDOM = CreateObject("Msxml2.DOMDocument")
Dim objFPC : set objFPC = WScript.CreateObject("fpc.Root")
Dim objISAArray : set objISAArray = objFPC.GetContainingArray
Dim strFileName
Dim blErrorsOccurred : blErrorsOccurred = false
Dim strHostName : strHostName = objNetWork.ComputerName
Dim strEmailSubject : strEmailSubject = strHostName & ": Backup am " & GetDateString & " durchgeführt"
Dim strConsoleOutput


strScriptPath = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
strEventLogFile = strScriptPath & "logs\" & strHostName & "-D" & Replace(GetDateString,".","") & "-T" & Replace(GetTimeString,":","") & ".txt"

DeleteIfExists (strEventLogFile)
set objEventLogFile = objFSO.CreateTextFile(strEventLogFile, CopyOverwrite)

LogConsoleOutput "Starting Backup"
LogConsoleOutput "Logging to Text File " & strEventLogFile

strFileName = strBackupShare & "\" & strHostName & "-D" & Replace(GetDateString,".","") & "-T" & Replace(GetTimeString,":","") & ".xml"

LogConsoleOutput "Exporting Configuration"

objISAArray.Export objXMLDOM, 0

LogConsoleOutput "Saving exported Configuration to File " & strFileName

objXMLDOM.save(strFileName)

LogConsoleOutput "Backup Finished"

If blSendMails Then

Call SendEmail (strEmailFromAddr, strEmailRecipients, strEmailSubject, strConsoleOutput, strEmailServer, blErrorsOccurred)

End If

objEventLogFile.Close

Keine Kommentare: