home screen

Search



Number Of Result : 0

Result :


Wednesday, March 31, 2010

Backup and Restore Site Collection in SharePoint - Migrate to new Site Url

- Using stsadm command

internal static void MoveSiteCollection(string existingUrl, string newUrl)
{
/*stsadm.exe -o backup
-url
-filename
[-overwrite]*/
string filename = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".bak");

string cmd = string.Format(" -o backup -url \"{0}\" -filename \"{1}\"", existingUrl, filename);
if (RunStsAdmOperation(cmd.ToString(), true) != 0)
throw new SPException("Error occured backing up site for move.\r\nCOMMAND: stsadm.exe" + cmd);
/*stsadm.exe -o deletesite
-url
-deleteadaccounts */
cmd = string.Format(" -o deletesite -url \"{0}\"", existingUrl);

if (RunStsAdmOperation(cmd.ToString(), true) != 0)
throw new SPException("Error occured deleting source site for move.\r\nCOMMAND: stsadm.exe" + cmd);

/*stsadm.exe -o restore
23: -url
24: -filename
25: [-hostheaderwebapplicationurl ]
26: [-overwrite]*/

cmd = string.Format(" -o restore -url \"{0}\" -filename \"{1}\"", newUrl, filename);

try
{
if (RunStsAdmOperation(cmd.ToString(), true) != 0)
throw new SPException("Error occured restoring site for move.\r\nCOMMAND: stsadm.exe" + cmd);
}
catch (SPException)
{
Console.WriteLine("\r\nSite backup file can be located here: {0}", filename);
throw;
}
File.Delete(filename);
}



internal static int RunStsAdmOperation(string args, bool quiet)
{
string stsadmPath = Path.Combine(SPUtility.GetGenericSetupPath("BIN"), "stsadm.exe");

return RunCommand(stsadmPath, args, quiet);
}



internal static int RunCommand(string fileName, string args, bool quiet)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.FileName = fileName;
startInfo.Arguments = args;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;

Process proc = new Process();
try
{
proc.ErrorDataReceived += new DataReceivedEventHandler(Process_ErrorDataReceived);
if (!quiet)
proc.OutputDataReceived += new DataReceivedEventHandler(Process_OutputDataReceived);

proc.StartInfo = startInfo;
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();

return proc.ExitCode;
}
finally
{
proc.Close();
}
}



static void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}



static void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data == "Operation completed successfully.")
return;

Console.WriteLine(e.Data);
}



- Using Data Object Model

public static void BackupAndRestore()
{
// Get a reference to the Web application publishing
// Web service.
//SPFarm myFarm = SPFarm.Local;

using (SPSite site = new SPSite("http://vinh:2804"))
{
// As alternative to the preceding three lines, you can use
// the following when you know the URL of the Web application:
// SPWebApplication myApp = SPWebApplication.Lookup(url_of_Web_app)

// Get a reference to the Web application's collection of
// site collections.
SPSiteCollection mySiteCols = site.WebApplication.Sites;

// Back up a specified site collection.
mySiteCols.Backup(@"http://vinh:2804/sites/Dev123", @"c:\data.dat", true);

using (SPSite site123 = new SPSite("http://vinh:2804/sites/Dev123"))
{
Console.WriteLine(site123.ID);
site123.Delete();

}

// Restoring the site collection is identical to the preceding
// code except that the "Restore" is used in place of "Backup".
//
mySiteCols.Restore(@"http://vinh:2804/sites/Dev", @"c:\data.dat", true);

using (SPSite site123 = new SPSite("http://vinh:2804/sites/Dev"))
{
Console.WriteLine(site123.ID);
}
}
}

No comments: