Help and Support

Article ID: 941330 - Last Review: June 16, 2008 - Revision: 2.1

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

Expand all | Collapse all

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:
https://partner.microsoft.com/40011340 (https://partner.microsoft.com/40011340)
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 (http://support.microsoft.com/default.aspx?scid=fh;en-us;cntactms)

Sample 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. 

Setlocale(1033)
Dim nNumberOfDays
Dim strTOCFile
Dim dtDeleteDate

Dim sTemp

Set objXML = CreateObject("Microsoft.XMLDOM")
Set objFS  = CreateObject("Scripting.FileSystemObject")
Set objLog = objFS.OpenTextFile("BackupCleanUp.log",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("d",nNumberOfDays*-1,Now)
    Else
        WScript.Echo "<NumberOfDays> must be an integer value."
    End If
        strTOCFile = WScript.Arguments(1)
Else
    WScript.Echo "Usage: BackupCleanUp <NumberOfDays> <PathToTOC>"
    WScript.Quit
End If

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

' 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& "Error: Could not load the SharePoint Backup / Restore History." &vbCrLf&_
                     Now() &vbTab& "Reason: " &objXML.ParseError.Reason& ".") 
    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("SPFinishTime").Text) < dtDeleteDate Then
        If objNode.SelectSingleNode("SPIsBackup").Text = "True" Then
            
sTemp = mid(objNode.SelectSingleNode("SPBackupDirectory").Text,1,len(objNode.SelectSingleNode("SPBackupDirectory").Text)-1)

'objFS.DeleteFolder(mid(objNode.SelectSingleNode("SPBackupDirectory").Text),1,len(objNode.SelectSingleNode("SPBackupDirectory").Text)-1)
objFS.DeleteFolder sTemp




            objLog.WriteLine(Now() &vbTab& "Deleted: " &objNode.SelectSingleNode("SPBackupDirectory").Text& ".")
            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& "Finish: Completed backup clean up.")

REFERENCES

For more information about how to back up data and how to restore data by using the Microsoft SharePoint Central Administration Web site, visit the following Microsoft Web site:
http://technet2.microsoft.com/windowsserver/WSS/en/library/64171b8c-5608-4e69-881a-67996080b7ff1033.mspx (http://technet2.microsoft.com/windowsserver/WSS/en/library/64171b8c-5608-4e69-881a-67996080b7ff1033.mspx)

APPLIES TO
  • Microsoft Office SharePoint Server 2007
  • Microsoft Windows SharePoint Services 3.0
Keywords: 
kbhowto kbinfo kbexpertiseinter KB941330

Article Translations