Microsoft Playground

A Playground for Microsoft Technologies
Scheduling SharePoint Backup

Update:Version 2.0 available of the script here

For one of our clients we had to schedule the SharePoint Backup what isn’t possible trough the interface, so we went looking for some possible solutions.

The solutions we came across are:

  • Performing the backup every time by hand (isn’t scheduling after all).
  • Creating a batch file to perform the backup. And schedule it in windows with scheduled tasks.
  • Creating a VBScript for the backup and scheduling it in windows with scheduled tasks.

We choose the third solution because we found a create article on the internet from Rabi Achrafi. In this article he shows his version of a VBScript he used. The great thing about the script was that it sends an e-mail to a specified e-mail address when the job is finished and gives back the completion status of the job.

The script did not completely cover our needs so we changed the file a little bit. The main thanks still goes out to Rabi Achrafi.

Besides the small things we changed we also added a method for deleting all the files that reside in the shared folder. Because those files where already taped. The file we created looked something like this:

  1: '''''''''''''''''''''''''''''''''''''''''''''''''''''
  2: ''SharePoint Backup Script
  3: ''
  4: ''This is a VBScript for scheduling the SharePoint backup.
  5: '''''''''''''''''''''''''''''''''''''''''''''''''''''
  6: 
  7: 'string E-Mail from
  8: Const strFrom = "toemail@smeikkie.nl"
  9:  
 10: 'string E-Mail To
 11: Const strTo = "fromemail@smeikkie.nl"
 12:  
 13: 'string Mail Server
 14: Const strMailserver = "mail.test.nl"
 15:  
 16: 'string backup directory for the sharepoint backup
 17: Const strBackUpDir = "\\MOSS2007DEV\SharepointBackup"
 18:  
 19: 'string with the backupmethod
 20: Const BackUpMethod = "full"
 21:  
 22: 'Create windows shell object
 23: Set objShell = CreateObject("WScript.Shell")
 24:  
 25: 'Clean the sharepoint backup folder. Old backups will be deleted!!
 26: Call ClearFolder(strBackUpDir)
 27:  
 28: 'Retrieve the 12hive from sharepoint from the registery
 29: Dim strRegKey
 30: strRegKey = objShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\12.0\Location")
 31:  
 32: 'Add the bin folder to the 12hive folder
 33: Dim strMOSSPath
 34: strMOSSPath = strRegKey & "BIN\"
 35:  
 36: 'Setting the BackUp Location
 37: Dim strBackupLocation
 38: strBackUpLocation = strBackUpDir
 39:  
 40: 'Define the path off the sharepoint location. This will point to the file with specific information about the backup.
 41: Dim SharPointBackupRestoreTable
 42: SharPointBackupRestoreTable = strBackUpLocation & "\spbrtoc.xml"
 43:  
 44: 'Ensure that we run in the sharepoint folder
 45: objShell.CurrentDirectory = strMOSSPath
 46:  
 47: 'Execute the stsadm backup command
 48: objShell.Exec ("stsadm -o backup -directory " & strBackupLocation & " -backupmethod " & BackUpMethod & " ")
 49:  
 50: ' This Do loop checks the status of the backup process every minute.
 51: ' If the backup process hasn't completed within 60 minutes an email is sent to the
 52: ' Sharepoint administrator notifying him/her about this, otherwise an email is sent
 53: ' notifying the SharePoint Administrator of the outcome of the backup
 54: Do
 55: loopCounter = loopCounter + 1
 56:  
 57: If count > 60 Then
 58: 'Send the e-mail that it took 60 minutes
 59: Call SendEmail("SharePoint Backup Process", "SharePoint backup process has been running for over 60 minutes. Please check progress of backup. To make sure everything is going as it should be going.")
 60: End If
 61: ' Wait for 1 minute
 62: WScript.Sleep 60000
 63:  
 64: ' Check if the backup process (i.e. stsadm.exe) is currently running
 65: strComputer = "."
 66: Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 67: Set sharepointProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'stsadm.exe'")
 68:  
 69: If (sharepointProcess.count) = 0 Then
 70: 'Backup process has ended therefore check the SharePoint Backup Restore Table to analyse the outcome of the backup
 71: Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
 72: objXMLDoc.async = False
 73: objXMLDoc.load(SharPointBackupRestoreTable)
 74: Set NodeList = objXMLDoc.documentElement.selectNodes("//SPErrorCount")
 75: For Each Node In NodeList
 76: If (Node.text) <> "0" Then
 77: ' Backup errors were generated
 78: Call SendEmail("SharePoint Backup Process Failed", "The SharePoint Backup Process failed with errors. Please check the errors and run the backup process again.")
 79: Else
 80: ' No backup errors were generated
 81: Call SendEmail("SharePoint Backup Process Completed Successfully", "The SharePoint Backup Process completed without errors")
 82: End If
 83: Next
 84:  
 85: Exit Do
 86: End If
 87:  
 88: Loop
 89:  
 90: ''Method for cleaning out the backup folder. It will delete all off the old backups!!!
 91: ''The String Off The Folder To Cleam
 92: Sub ClearFolder(strfolder)
 93:  
 94: 'Get file object
 95: Set fso=CreateObject("Scripting.FileSystemObject")
 96: SharepointBackupDir = fso.GetFolder(strfolder)
 97:  
 98: 'Delete all files
 99: For Each file In fso.GetFolder(strfolder).Files
100: file.delete
101: Next
102:  
103: 'Loop trough the subfolders and put them in an array
104: arrayFolders = Array()
105: For Each objectFolder In fso.GetFolder(strfolder).SubFolders
106: intCount = UBound(arrayFolders) + 1
107: ReDim Preserve arrayFolders(intCount)
108: arrayFolders(intCount) = objectFolder.Path
109: Next
110:  
111: 'Loop trough the array and delete the subfolders
112: For n = 0 To UBound(arrayFolders)
113: fso.DeleteFolder arrayFolders(n), True
114: Next
115:  
116: End Sub
117:  
118: ''Mehtod for sending the e-mail
119: ''The subject off the email
120: ''The Body text off the e-mail
121: Sub SendEmail (subject, body)
122:  
123: 'create message
124: Set objEmail = CreateObject("CDO.Message")
125: objEmail.From = strFrom
126: objEmail.To = strTo
127: objEmail.Subject = subject
128: objEmail.Textbody = body
129:  
130: 'Mail configuration
131: objEmail.Configuration.Fields.Item _
132: ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
133: objEmail.Configuration.Fields.Item _
134: ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strMailserver
135: objEmail.Configuration.Fields.Item _
136: ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
137: objEmail.Configuration.Fields.Update
138:  
139: 'Send E-mail
140: objEmail.Send
141: 'Write to the event log
142: Call WriteEvent(subject,body)
143: End Sub
144:  
145: ''Method for Logging the outcome off the backup to the application event log
146: ''The subject off the event
147: ''The Body text off the event
148: Sub WriteEvent(subject,body)
149: If subject = "SharePoint Backup Process Completed Successfully" Then
150: objShell.LogEvent 0, body
151: Else
152: objShell.LogEvent 1, body
153: end If
154: End Sub

If you want to change the settings off this VBScript you only have to change the 5 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 incremental. If you choose incremental you will have to delete the following line from the script: Call ClearFolder(strBackUpDir)

For scheduling the execution off the VBScript you will have to take the following actions:

  • Step 1: Go to Start-Control Panel-Scheduled Tasks.
  • Step 2: Select ‘Add Scheduled Task’.
  • Step 3: Follow the wizard, and if you finish it SharePoint backup is scheduled.

Update: Somebody made me ware off the fact that you can convert your vbs to a exe file on this site: http://www.vbs2exe.com/

Comments

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

Maciej Soltysiak said:

This is good stuff, thanks!

# December 8, 2009 9:21 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: (Required)

Maik van der Gaag

Maik van der Gaag
MCPD