Click here to Skip to main content
15,891,828 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.3K   398   14  
A SharePoint backup utility.
////////////////////////////////////////////////////////////////////////////////////////////
/// sbonello@gmail.com
///////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Xml;
using System.Net.Mail;
using System.Net;

namespace SPBackup
{
    class Program
    {
        static int Main(string[] args)
        {
            string  rbinFolder = string.Empty;
            int     nMaxBackups = 0;
            string rDirectory = string.Empty; 
            
            if (args.Length < 1)
            {
                DisplayError();
                return 1;
            }

            int nIdx = 0;

            if (args[0] != "-directory")
            {
                DisplayError();
                return 1;
            }
            nIdx++;
            rDirectory = args[nIdx];

            // Check that the two folders exist
            if (!Directory.Exists(rDirectory))
            {
                Console.WriteLine("\n Invalid backup folder");
                return 1;
            }


            try
            {
                while (nIdx < args.Length)
                {
                    switch (args[nIdx])
                    {
                        case "-bin":
                            rbinFolder = args[++nIdx];
                            break;
                        case "-maxbkp":
                            nMaxBackups = int.Parse(args[++nIdx]);
                            break;
                    }
                    nIdx++;
                }
            }
            catch 
            {
                DisplayError();
                return 1;
            }

            string rFilename = string.Empty;
            string rArguments = string.Empty;

            rFilename = rbinFolder + @"\stsadm.exe";
            rArguments = "-o backup -directory " + rDirectory + " -backupmethod full";

            Process proc = Process.Start(rFilename, rArguments);
            proc.WaitForExit();

            if (proc.ExitCode != 0)
            {
                Console.WriteLine("\n Failed to perform backup");
            }
            else
            {
                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);
            }

            return 0;
        }



        static private void DisplayError()
        {
            Console.WriteLine("\n Usage: SPbackup -directory <Backup Folder> \n\t\t[-bin <SharePoint stsadm bin folder>]\n\t\t[-maxbkp <Max backups (default 7)>]");
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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