Scheduling SharePoint Backup : Part 2

5 minute read

A while ago I posted a SharePoint Backup script so that it is possible to schedule a SharePoint Backup. I had to make some adjustments to the script because it contains small errors. The adjustments I had to make were:
  • When you perform multiple differential backups in a week you will receive multiple e-mails for one backup.
  • I have created more properties so that you can alter the script from the top.
  • You do not have to delete a method when you choose the differential backup method
  • I cleaned the script up a little bit.
 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 ''SharePoint Backup Script
 ''
 ''This is a VBScript for scheduling the SharePoint backup.
 ''The script has the ability to be scheduled as an differential
 ''backup or a full backup. This script will also send an email
 ''when the job is finished.
 ''
 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 ''declaration off constants that are needed to perform the backup
 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 'email address from
 Const strFrom = ""

 'email address to
 Const strTo = ""

 'email subject timeout
 Const strSubjectTimeOut = "Sharepoint Backup Process - Time Out"

 'message for the time out
 Const strTimeOutMessage = "The SharePoint backup process has been running for over 60 minutes. Please check the progress of the backup. To make sure everything is going as it should be going."

 'email subject failed
 Const strSubjectFailes = "Sharepoint Backup Process - Failed"

 'email message failed
 Const strFailedMessage = "The SharePoint Backup Process failed with errors. Please check the errors and run the backup process again."

 'email subject success
 Const strSubjectSucceeded = "Sharepoint Backup Process - Succeeded"

 'email message success
 Const strSucceededMessage = "The SharePoint Backup Process completed without errors."

 'backup enviroment
 Const strEnviroment = "Intranet"

 'url off the mail server
 Const strMailserver = ""

 'backup location, this has to be a shared drive
 Const strBackUpDir = ""

 'backupmethod full or differential
 Const strBackUpMethod = "differential"

 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 ''Beginning the backup process
 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

 'object for checking succeeded update
 bool = "false"

 'create windows shell object
 Set objShell = CreateObject("WScript.Shell")

 if strBackUpMethod = "full" Then
     Call ClearFolder(strBackUpDir)
 End If

 'retrieve the 12hive from sharepoint from the registery
 strMossPath = objShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\12.0\Location") & "BIN\"

 'Define the path off the sharepoint location. This will point to the file with specific information about the backup.
 SharPointBackupRestoreTable = strBackUpDir & "\spbrtoc.xml"

 'Ensure that we run in the sharepoint folder
 objShell.CurrentDirectory = strMossPath

 'Execute the stsadm backup command
 objShell.Exec ("stsadm -o backup -directory " & strBackUpDir & " -backupmethod " & strBackUpMethod & " ")

 ' This Do loop checks the status of the backup process every minute.
 ' If the backup process hasn't completed within 60 minutes an email is sent to the
 ' Sharepoint administrator notifying him/her about this, otherwise an email is sent
 ' notifying the SharePoint Administrator of the outcome of the backup
 Do

 loopCounter = loopCounter + 1

 If count > 60 Then
 'Send the e-mail that it took 60 minutes
 Call SendEmail(strSubject, strTimeOutMessage, strEnviroment)
 End If
 ' Wait for 1 minute
 WScript.Sleep 60000

 ' Check if the backup process (i.e. stsadm.exe) is currently running
 strComputer = "."
 Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 Set sharepointProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'stsadm.exe'")

 If (sharepointProcess.count) = 0 Then
 'Backup process has ended therefore check the SharePoint Backup Restore Table to analyse the outcome of the backup
 Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
 objXMLDoc.async = False
 objXMLDoc.load(SharPointBackupRestoreTable)
 Set NodeList = objXMLDoc.documentElement.selectNodes("//SPErrorCount")
 'check each node
 For Each Node In NodeList
 If (Node.text) <> "0" Then
 bool = "false"
 Else
 bool = "true"
 End If
 Next

 'send message
 If bool="false" Then
 ' Backup errors were generated
 Call SendEmail(strSubjectFailed, strFailedMessage, strEnviroment)
 Else
 ' No backup errors were generated
 Call SendEmail(strSubjectSucceeded, strSucceededMessage, strEnviroment)
 End If
 Exit Do
 End If

 Loop

 ''Method for cleaning out the backup folder. It will delete all the old backups
 ''<strfolder>The String Off The Folder To Cleam</strfolder>
 Sub ClearFolder(strfolder)

 'Get file object
 Set fso=CreateObject("Scripting.FileSystemObject")
 CleanPath = fso.GetFolder(strfolder)

 'Delete all files
 For Each file In fso.GetFolder(CleanPath).Files
 file.delete
 Next

 'Loop trough the subfolders and put them in an array
 arrFolders = Array()
 For Each oFolder In fso.GetFolder(strfolder).SubFolders
 intCount = UBound(arrFolders) + 1
 ReDim Preserve arrFolders(intCount)
 arrFolders(intCount) = oFolder.Path
 Next

 'Loop trough the array and delete the subfolders
 For n = 0 To UBound(arrFolders)
 fso.DeleteFolder arrFolders(n), True
 Next

 End Sub

 ''Mehtod for sending the e-mail
 ''<subject>The subject off the email</subject>
 ''<body>The Body text off the e-mail</body>
 Sub SendEmail (subject, body, enviroment)

 'create message
 Set objEmail = CreateObject("CDO.Message")
 objEmail.From = strFrom
 objEmail.To = strTo
 objEmail.Subject = subject & " (" & strBackUpMethod & ") " & "- " & enviroment
 objEmail.Textbody = body

 'Mail configuration
 objEmail.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
 objEmail.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strMailserver
 objEmail.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
 objEmail.Configuration.Fields.Update

 'Send E-mail
 objEmail.Send
 'Write to the event log
 Call WriteEvent(subject,body)
 End Sub