Automating WSS 3.0 Backups with a Script

Although SQL backups of the content databases for a Windows SharePoint Services farm can be used for data recovery, it’s usually a good idea to also perform backups through the stsadm.exe utility to facilitate site and object-level restores.   I recently took on a task to script a more robust solution for the automation of WSS farm backups, which I will describe here.

The stsadm.exe utility can be used to backup in two modes, site collection and catastrophic.  The site collection method backups up an individual site and content for an individual site, specified by URL, and the catastrophic backup method backs up the entire farm or a specified object in a full or differential mode.    I opted to go with the catastrophic backup method in this script to support differential backups and eliminate the requirement to enumerate individual sites for backup operations. 

WSS Backup Script Overview

The script is a bit too long to post in its entirity, but it can be downloaded here.   The script accepts three parameters:

  • The target backup directory path
  • The backup type (full or differential)
  • The number of backups to maintain

The backup operation is relatively simple:  the script uses the WScript.Shell.exec method to execute the stsadm.exe command (after querying the registry to determine the install path of WSS).

Command = sPath & “\STSADM.exe -o backup -Directory ” & BackupDir & ” -BackupMethod ” & BackupType

Set oExec = WshShell.Exec(command) 
While Not oExec.StdOut.AtEndOfStream 
    OutPut = oExec.StdOut.ReadAll
Wend 
Do While oExec.Status = 0
    WScript.Sleep 5000
Loop

To improve monitoring of the operation, the script performs a shell execution to the eventcreate.exe utility to log status to the Windows Application Log.   (Although the WScript.Shell supports basic EventLog logging, I wanted to control the event source and ID, so the eventcreate.exe utility seemed to be a better option).

If blSuccessful then
   Command =  “Eventcreate /L Application /T INFORMATION /ID 943 /SO “”SharePoint Backups”” /D “” ” & sMesg & “”
Else
   Command =  “Eventcreate /L Application /T WARNING /ID 944 /SO “”SharePoint Backups”” /D “” ” & sMesg & “”
End if
Set oExec = WshShell.Exec(command)

The most complex operation of this WSS backup automation script is the maintenance of old backups.  The stsadm backup operation maintains an XML file named spbrtoc.xml in the backup directory with meta-data related to past backups.   While an example of deleting backups older than a certain time interval can be found here, I wanted to maintain past backups based on a count (x number of fulls, x number of differentials).    To implement this, the script loads meta-data from the Table of Contents XML file into an array, determines the number of backups to be purged (correlated to the current backup operation type – full or differential), flags the oldest backups for deletion, and then deletes the related backup directories and XML nodes.  

Automating With System Center Operations Manager 2007

While this script could just be scheduled with the Windows Task Scheduler, I wanted to centralize the scheduling, parameter control, and monitoring with Ops Mgr.  I have created an Operations Manager 2007 Management Pack that includes two rules for scheduling of the backup script (full and differential).    The rules are targeted to the Microsoft Windows SharePoint Services 3.0 Server class and will utilize the WSS 3.0 Management Pack’s “WSS Action Account” runas profile for credentials to WSS.   Additionally, I have added an eventlog-based monitoring for monitoring for failed backups.   The schedule, backup directory target, and number of backups to keep can be overridden for each rule.   The rules and monitor are disabled by default but are enabled through overrides for any WSS servers added to the “WSS 3.0 Servers  for Automated Backup” group that is included in the MP.   Notes on setting the overrides for the parameters can be found in the rule descriptions.

The management pack and script can both be downloaded here.

Advertisement

About Kristopher Bash
Kris is a Senior Program Manager at Microsoft, working on UNIX and Linux management features in Microsoft System Center. Prior to joining Microsoft, Kris worked in systems management, server administration, and IT operations for nearly 15 years.

2 Responses to Automating WSS 3.0 Backups with a Script

  1. Fabian says:

    Hello Kris,

    thank you very much again for this great peece of work. I investigated the whole day to make this mp work with SCOM SP1 and my WSS 3.0 Serverfarm. And what can I say. It works great!

    One thing maybe to say. My WSS Serverfarm and the databases are on different servers. So you have to set an unc path in the rule. also you have to setup appropriate permissions to this folder. Otherwise the backup will run into an error:

    Source: SharePoint Backups
    Event ID:944
    SqlException: Cannot open backup device ‘k:\wssbackups\spbr00000000032.bak’
    Operating system error 3(error not found).
    BACKUP DATABASE is terminating abnormally

    Then it works also if the two parts of the WSS farm were seperated.

    Thanks again!!

  2. Jim D says:

    great script! thank you for sharing!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: