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:

'''''''''''''''''''''''''''''''''''''''''''''''''''''
 ''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)
 If subject = "SharePoint Backup Process Completed Successfully" Then
 objShell.LogEvent 0, body
 Else
 objShell.LogEvent 1, body
 end If
 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/

Related Posts

Microsoft Office SharePoint Server 2007 on Windows Server 2008 R2 A few days ago I started setting up an internal development environment for SharePoint. This environment needed to exist of a Domain server, Database...
SharePoint 2007 Service Pack 2 Yesterday Service Pack 2 was released for SharePoint. In my post “Office Service Pack will be available on 28 April” you can read about some changes ...
February Cumulative Update Uber Package In my post a week ago I announced that the February Cumulative update was available for download but it could only be used by users who had the Decem...
February Cumulative Update The new cumulative update for Microsoft Office SharePoint Server 2007 and Windows SharePoint Services is available for you to apply to you server far...
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 b...
Integration Reporting Services with SharePoint 2007 in Native Mode It is possible to integrate SQL Reporting Services with SharePoint 2007, but know the question is how you integrate it with SharePoint. The easiest w...

One comment

Leave a Reply

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