Click here to Skip to main content
15,867,488 members
Articles / Programming Languages / C#

SharePoint Backup

Rate me:
Please Sign up or sign in to vote.
3.15/5 (6 votes)
25 Jul 2007CPOL 41.2K   398   14   4
A SharePoint backup utility.

Introduction

Typically, SharePoint backup is performed using the stsadm utility. Unfortunately, after setting up STSADM.exe to backup a SharePoint site, the backup directory will keep growing daily. The SPBackup utility eliminates the manual weekly task of removing old backups. It eliminating old backups from the file system and removes the backup references from the history log file (spbrtoc.xml).

Using the code

The utility performs a backup by calling the stsadm utility.

C#
rFilename = rbinFolder + @"\stsadm.exe";
rArguments = "-o backup -directory " + rDirectory + " -backupmethod full";
Process proc = Process.Start(rFilename, rArguments);
proc.WaitForExit();

After the backup is complete, the utility parses the spbrotoc.xml file to determine the number of backups present in the backup folder. If the number exceeds a defined maximum limit (which is passed as a command line argument), the old backups are removed from the file system and the history log (spbrtoc.xml) is updated.

C#
string rHistoryFile = rDirectory + @"\" + "spbrtoc.xml";
XmlDocument doc = new XmlDocument();
doc.Load(rHistoryFile);XmlNodeList nodelist = 
    doc.SelectNodes("/SPBackupRestoreHistory/SPHistoryObject");

if (nodelist.Count > nMaxBackups)
{
    XmlNode node = doc.SelectSingleNode("/SPBackupRestoreHistory");
    while (node.ChildNodes.Count != nMaxBackups)
    {
        XmlNode Directorynode = node.LastChild.SelectSingleNode("SPDirectoryName");
        // Delete folder 
        Directory.Delete(rDirectory + @"\" + Directorynode.InnerXml, true);
        node.RemoveChild(node.LastChild);
    }
}
doc.Save(rHistoryFile);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Malta Malta
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralWorks great! Pin
bvermilion9-Jan-09 5:34
bvermilion9-Jan-09 5:34 
Thank you so much. I can't believe MS didn't include such a feature to be begin with. Good addition and a big time saver!

Thank you!

Brent
GeneralThanks. Great help! I added it to www.sharepoint-tools.de Pin
dknyoli17-Jun-08 2:34
dknyoli17-Jun-08 2:34 
Generalgetting error.... Pin
Bh@nu29-Aug-07 0:54
Bh@nu29-Aug-07 0:54 
GeneralRe: getting error.... Pin
Simon Bonello29-Aug-07 1:42
Simon Bonello29-Aug-07 1:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.