Microsoft KB Archive/941330

= How to automate the deletion of backups in SharePoint Server 2007 and in Windows SharePoint Services 3.0 by using a Visual Basic script =

Article ID: 941330

Article Last Modified on 10/31/2007

-

APPLIES TO


 * Microsoft Office SharePoint Server 2007
 * Microsoft Windows SharePoint Services 3.0

-



INTRODUCTION
This article describes how to automate the deletion of backups in Microsoft Office SharePoint Server 2007 and in Windows SharePoint Services 3.0 by using a Microsoft Visual Basic script. This article contains an example Visual Basic script that deletes backups that are more than a certain number of days old.



MORE INFORMATION
Backups should have a configurable retention policy. Examples of retention policies are as follows:
 * Backups are deleted after a certain number of days.
 * The number of backups that are retained is limited.

You can also manually delete backups. However, manually deleting backups does not clean the manifest file (Spbrtoc.xml) correctly. To resolve this issue, use a script to delete backups.

Note You must have sufficient permissions to perform this procedure. Typically, deleting backups should be performed by a member of the Local Administrator group on the computer that contains the shared folder for the backups. In any case, the person who runs the script must have Read permissions, Write permissions, and Delete permissions to the shared folder for the backups and to the backup files that are to be deleted.

Warning Use of the code in this article is at your own risk. Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure. However, they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, visit the following Microsoft Web site:

http://www.microsoft.com/partner/referral/

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

Example script
The following example Visual Basic script automates the deletion of backups. In this script, backups that are more than a certain number of days old are deleted. '      Title: BackupCleanUp ' Description: Deletes SharePoint 2007 backups that are older than a specified '             number of days and then removes the backups from the backup history.

Dim nNumberOfDays Dim strTOCFile Dim dtDeleteDate

Set objXML = CreateObject(&quot;Microsoft.XMLDOM&quot;) Set objFS = CreateObject(&quot;Scripting.FileSystemObject&quot;) Set objLog = objFS.OpenTextFile(&quot;BackupCleanUp.log&quot;,8,true)

' Validate command line arguments and initialize data. If WScript.Arguments.Count = 2 Then If IsNumeric(WScript.Arguments(0)) Then nNumberOfDays = CInt(WScript.Arguments(0)) dtDeleteDate = DateAdd(&quot;d&quot;,nNumberOfDays*-1,Now) Else WScript.Echo &quot; must be an integer value.&quot; End If       strTOCFile = WScript.Arguments(1) Else WScript.Echo &quot;Usage: BackupCleanUp  &quot; WScript.Quit End If

objLog.WriteLine(Now &vbTab& &quot;Start: Clean up backups older than &quot; &nNumberOfDays& &quot; days from &quot; &strTOCFile& &quot;.&quot;)

' Load the SharePoint backup and restore the TOC file. objXML.Async = false objXML.Load(strTOCFile)

If objXML.ParseError.ErrorCode <> 0 Then objLog.WriteLine(Now &vbTab& &quot;Error: Could not load the SharePoint Backup / Restore History.&quot; &vbCrLf&_                    Now &vbTab& &quot;Reason: &quot; &objXML.ParseError.Reason& &quot;.&quot;) WScript.Quit End If

' Delete backup nodes that are older than the deletion date. For Each objNode in objXML.DocumentElement.ChildNodes If CDate(objNode.SelectSingleNode(&quot;SPFinishTime&quot;).Text) < dtDeleteDate Then If objNode.SelectSingleNode(&quot;SPIsBackup&quot;).Text = &quot;True&quot; Then objFS.DeleteFolder(objNode.SelectSingleNode(&quot;SPBackupDirectory&quot;).Text) objLog.WriteLine(Now &vbTab& &quot;Deleted: &quot; &objNode.SelectSingleNode(&quot;SPBackupDirectory&quot;).Text& &quot;.&quot;) objXML.DocumentElement.RemoveChild(objNode) End If        End If Next

' Save the XML file with the old nodes removed. objXML.Save(strTOCFile) objLog.WriteLine(Now &vbTab& &quot;Finish: Completed backup clean up.&quot;)

