Microsoft Knowledge Base Email Alertz

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

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
KBAlertz referrals get
** SIX MONTHS FREE **


Community Site



We Send hundreds of thousands of emails using ASP.NET Email


ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




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

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

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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104 (https://partner.microsoft.com/global/30000104)

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice (http://support.microsoft.com/gp/advisoryservice)

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)

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
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please