Microsoft Playground

A Playground for Microsoft Technologies
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.
  1: ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  2: ''SharePoint Backup Script
  3: ''
  4: ''This is a VBScript for scheduling the SharePoint backup.
  5: ''The script has the ability to be scheduled as an differential
  6: ''backup or a full backup. This script will also send an email
  7: ''when the job is finished.
  8: ''
  9: ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 10: 
 11: ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 12: ''declaration off constants that are needed to perform the backup
 13: ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 14: 'email address from
 15: Const strFrom = ""
 16:  
 17: 'email address to
 18: Const strTo = ""
 19:  
 20: 'email subject timeout
 21: Const strSubjectTimeOut = "Sharepoint Backup Process - Time Out"
 22:  
 23: 'message for the time out
 24: 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."
 25:  
 26: 'email subject failed
 27: Const strSubjectFailes = "Sharepoint Backup Process - Failed" 
 28:  
 29: 'email message failed
 30: Const strFailedMessage = "The SharePoint Backup Process failed with errors. Please check the errors and run the backup process again."
 31:  
 32: 'email subject success
 33: Const strSubjectSucceeded = "Sharepoint Backup Process - Succeeded"
 34:  
 35: 'email message success
 36: Const strSucceededMessage = "The SharePoint Backup Process completed without errors."
 37:  
 38: 'backup enviroment
 39: Const strEnviroment = "Intranet"
 40:  
 41: 'url off the mail server
 42: Const strMailserver = ""
 43:  
 44: 'backup location, this has to be a shared drive
 45: Const strBackUpDir = ""
 46:  
 47: 'backupmethod full or differential
 48: Const strBackUpMethod = "differential"
 49:  
 50:  
 51: ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 52: ''Beginning the backup process
 53: ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 54: 
 55: 'object for checking succeeded update
 56: bool = "false"
 57:  
 58: 'create windows shell object
 59: Set objShell = CreateObject("WScript.Shell")
 60:  
 61: if strBackUpMethod = "full" Then
 62:     Call ClearFolder(strBackUpDir)
 63: End If
 64:  
 65: 'retrieve the 12hive from sharepoint from the registery
 66: strMossPath = objShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\12.0\Location") & "BIN\"
 67:  
 68: 'Define the path off the sharepoint location. This will point to the file with specific information about the backup.
 69: SharPointBackupRestoreTable = strBackUpDir & "\spbrtoc.xml"
 70:  
 71: 'Ensure that we run in the sharepoint folder
 72: objShell.CurrentDirectory = strMossPath
 73:  
 74: 'Execute the stsadm backup command
 75: objShell.Exec ("stsadm -o backup -directory " & strBackUpDir & " -backupmethod " & strBackUpMethod & " ")
 76:  
 77: ' This Do loop checks the status of the backup process every minute.
 78: ' If the backup process hasn't completed within 60 minutes an email is sent to the
 79: ' Sharepoint administrator notifying him/her about this, otherwise an email is sent
 80: ' notifying the SharePoint Administrator of the outcome of the backup
 81: Do
 82:  
 83: loopCounter = loopCounter + 1
 84:  
 85: If count > 60 Then
 86: 'Send the e-mail that it took 60 minutes
 87: Call SendEmail(strSubject, strTimeOutMessage, strEnviroment)
 88: End If
 89: ' Wait for 1 minute
 90: WScript.Sleep 60000
 91:  
 92: ' Check if the backup process (i.e. stsadm.exe) is currently running
 93: strComputer = "."
 94: Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 95: Set sharepointProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'stsadm.exe'")
 96:  
 97: If (sharepointProcess.count) = 0 Then
 98: 'Backup process has ended therefore check the SharePoint Backup Restore Table to analyse the outcome of the backup
 99: Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
100: objXMLDoc.async = False
101: objXMLDoc.load(SharPointBackupRestoreTable)
102: Set NodeList = objXMLDoc.documentElement.selectNodes("//SPErrorCount")
103: 'check each node
104: For Each Node In NodeList
105: If (Node.text) <> "0" Then
106: bool = "false"
107: Else
108: bool = "true"
109: End If
110: Next
111:  
112: 'send message
113: If bool="false" Then
114: ' Backup errors were generated
115: Call SendEmail(strSubjectFailed, strFailedMessage, strEnviroment)
116: Else
117: ' No backup errors were generated
118: Call SendEmail(strSubjectSucceeded, strSucceededMessage, strEnviroment)
119: End If
120: Exit Do
121: End If
122:  
123: Loop
124:  
125: ''Method for cleaning out the backup folder. It will delete all the old backups
126: ''<strfolder>The String Off The Folder To Cleam</strfolder>
127: Sub ClearFolder(strfolder)
128:  
129: 'Get file object
130: Set fso=CreateObject("Scripting.FileSystemObject")
131: CleanPath = fso.GetFolder(strfolder)
132:  
133: 'Delete all files
134: For Each file In fso.GetFolder(CleanPath).Files
135: file.delete
136: Next
137:  
138: 'Loop trough the subfolders and put them in an array
139: arrFolders = Array()
140: For Each oFolder In fso.GetFolder(strfolder).SubFolders
141: intCount = UBound(arrFolders) + 1
142: ReDim Preserve arrFolders(intCount)
143: arrFolders(intCount) = oFolder.Path
144: Next
145:  
146: 'Loop trough the array and delete the subfolders
147: For n = 0 To UBound(arrFolders)
148: fso.DeleteFolder arrFolders(n), True
149: Next
150:  
151: End Sub
152:  
153: ''Mehtod for sending the e-mail
154: ''<subject>The subject off the email</subject>
155: ''<body>The Body text off the e-mail</body>
156: Sub SendEmail (subject, body, enviroment)
157:  
158: 'create message
159: Set objEmail = CreateObject("CDO.Message")
160: objEmail.From = strFrom
161: objEmail.To = strTo
162: objEmail.Subject = subject & " (" & strBackUpMethod & ") " & "- " & enviroment 
163: objEmail.Textbody = body
164:  
165: 'Mail configuration
166: objEmail.Configuration.Fields.Item _
167: ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
168: objEmail.Configuration.Fields.Item _
169: ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strMailserver
170: objEmail.Configuration.Fields.Item _
171: ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
172: objEmail.Configuration.Fields.Update
173:  
174: 'Send E-mail
175: objEmail.Send
176: 'Write to the event log
177: Call WriteEvent(subject,body)
178: End Sub
179:  
180: ''Method for Logging the outcome off the backup to the application event log
181: ''<subject>The subject off the event</subject>
182: ''<body>The Body text off the event</body>
183: Sub WriteEvent(subject,body)
184: If subject = "SharePoint Backup Process Completed Successfully" Then
185: objShell.LogEvent 0, body
186: Else
187: objShell.LogEvent 1, body
188: end If
189: 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:

Comments

debug.write("Microsoft Playground") said:

Update: Version 2.0 available of the script here For one of our clients we had to schedule the SharePoint

# October 26, 2009 8:29 AM

debug.write("Microsoft Playground") said:

Update: Version 2.0 available of the script here For one of our clients we had to schedule the SharePoint

# October 26, 2009 8:29 AM

debug.write("Microsoft Playground") said:

On this page you can find several useful download links for tools and documentation. Besides 3rd party

# October 27, 2009 10:48 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: (Required)

Maik van der Gaag

Maik van der Gaag
MCPD