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.
rFilename = rbinFolder + @"\stsadm.exe";
rArguments = "-o backup -directory " + rDirectory + " -backupmethod full";
Process proc = Process.Start(rFilename, rArguments);
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.
string rHistoryFile = rDirectory + @"\" + "spbrtoc.xml";
XmlDocument doc = new XmlDocument();
doc.Load(rHistoryFile);XmlNodeList nodelist =
if (nodelist.Count > nMaxBackups)
XmlNode node = doc.SelectSingleNode("/SPBackupRestoreHistory");
while (node.ChildNodes.Count != nMaxBackups)
XmlNode Directorynode = node.LastChild.SelectSingleNode("SPDirectoryName");
Directory.Delete(rDirectory + @"\" + Directorynode.InnerXml, true);