5 minute read

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:

'''''''''''''''''''''''''''''''''''''''''''''''''''''
 ''SharePoint Backup Script
 ''
 ''This is a VBScript for scheduling the SharePoint backup.
 '''''''''''''''''''''''''''''''''''''''''''''''''''''

 'string E-Mail from
 Const strFrom = "toemail@smeikkie.nl"

 'string E-Mail To
 Const strTo = "fromemail@smeikkie.nl"

 'string Mail Server
 Const strMailserver = "mail.test.nl"

 'string backup directory for the sharepoint backup
 Const strBackUpDir = "\\MOSS2007DEV\SharepointBackup"

 'string with the backupmethod
 Const BackUpMethod = "full"

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

 'Clean the sharepoint backup folder. Old backups will be deleted!!
 Call ClearFolder(strBackUpDir)

 'Retrieve the 12hive from sharepoint from the registery
 Dim strRegKey
 strRegKey = objShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\12.0\Location")

 'Add the bin folder to the 12hive folder
 Dim strMOSSPath
 strMOSSPath = strRegKey & "BIN\"

 'Setting the BackUp Location
 Dim strBackupLocation
 strBackUpLocation = strBackUpDir

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

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

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

 ' 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("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.")
 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")
 For Each Node In NodeList
 If (Node.text) <> "0" Then
 ' Backup errors were generated
 Call SendEmail("SharePoint Backup Process Failed", "The SharePoint Backup Process failed with errors. Please check the errors and run the backup process again.")
 Else
 ' No backup errors were generated
 Call SendEmail("SharePoint Backup Process Completed Successfully", "The SharePoint Backup Process completed without errors")
 End If
 Next

 Exit Do
 End If

 Loop

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

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

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

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

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

 End Sub

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

 'create message
 Set objEmail = CreateObject("CDO.Message")
 objEmail.From = strFrom
 objEmail.To = strTo
 objEmail.Subject = subject
 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

 ''Method for Logging the outcome off the backup to the application event log
 ''The subject off the event
 ''The Body text off the event
 Sub WriteEvent(subject,body)