Scheduling SharePoint Backup : Part 2

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

 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
 Set NodeList = objXMLDoc.documentElement.selectNodes("//SPErrorCount")
 'check each node
 For Each Node In NodeList
 If (Node.text) <> "0" Then
 bool = "false"
 bool = "true"
 End If

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


 ''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

 '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

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

 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 _
 ("") = 2
 objEmail.Configuration.Fields.Item _
 ("") = strMailserver
 objEmail.Configuration.Fields.Item _
 ("") = 25

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

 ''Method for Logging the outcome off the backup to the application event log
 ''<subject>The subject off the event</subject>
 ''<body>The Body text off the event</body>
 Sub WriteEvent(subject,body)
 If subject = "SharePoint Backup Process Completed Successfully" Then
 objShell.LogEvent 0, body
 objShell.LogEvent 1, body
 end If
 End Sub

If you want to change the settings off this VBScript you only have to change the values at the top off the file:

  • strFrom: The e-mail address off the person where you would like to send the e-mail.
  • strTo: The e-mail address of the person where the e-mail comes from.
  • strMailserver:The mail server for sending the e-mail.
  • strBackUpDir:Backup location.
  • BackUpMethod:The Backup method you can choose full or differential.
  • strEnviroment:The environment where you run the backup script. If you use multiple environments and send it to one address you can see about witch environment the message is about.
  • strSucceededMessage:The message when the backup is succeeded.
  • strSubjectTimeOut:The subject for the timeout email.
  • strTimeOutMessage:The message for the timeout email.
  • strSubjectFailes:The subject for the failed email.
  • strFailedMessage:The message for the failed email.
  • strSubjectSucceeded:The subject for the message when the backup succeeded

If you want to schedule the execution off the VBScript you can read my previous post : here. If you want to download the file you can click here: SharePoint Backup

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.