How to: Programmatically Back Up and Restore a Single Site Collection
Introduction
This post describes how to back up and restore individual site collections programmatically.
Note: Unless explicitly stated otherwise, all classes referred to in this post are in the Microsoft.SharePoint.Administration or Microsoft.SharePoint namespaces (not Microsoft.SharePoint.Administration.Backup).
Procedures
To Back Up or Restore a Site Collection |
1. Add to your Visual Studio project a reference to Windows SharePoint Services. 2. Add using statements for Microsoft.SharePoint and Microsoft.SharePoint.Administration. 3. Add the following lines to obtain a reference to the farm and its collection of services. [C#] SPFarm myFarm = SPFarm.Local; SPServiceCollection myServices = myFarm.Services; 4. Obtain a reference to the Web service that publishes the Web application that hosts your site collection by using the service's System.Guid which is the value of its SPWebService.Id property. [C#] Guid serviceID = new Guid("21d91b29-5c5b-4893-9264-4e9c758618b4"); SPWebService webPubService = (SPWebService)myServices[serviceID]; If you do not know the SPWebService.Id of the application publishing Web service, you can iterate through all the services and report their SPWebService.Name, SPWebService.TypeName, and SPWebService.Id. The following is an example: [C#] foreach (SPService service in myServices) { if (service is SPWebService) { Console.WriteLine("Web service name:" + webService.Name); Console.WriteLine("Web service type:" + webService.TypeName); Console.WriteLine("Web service ID:" + webService.Id); Console.WriteLine(); Console.Readline(); } } 5. Obtain a reference to the Web application that hosts your site collection. If you know the URL of the Web application you can obtain a reference with the static SPWebApplication.Lookup method. Alternatively, you can use the application's System.Guid which is the value of its SPWebApplication.Id property. The following code shows the second method. [C#] SPWebApplicationCollection myApps = webPubService.WebApplications; Guid appID = new Guid("10ea4e6f-ae37-4909-b04f-f516c066bc37"); SPWebApplication myApp = myApps[appID]; If you do not know the SPWebApplication.Id of the Web application that hosts your site collection, you can iterate through all the Web applications and report their SPWebApplication.Name, SPWebApplication.TypeName, and SPWebApplication.Id. The following is an example: [C#] foreach (SPWebApplication app in webApps) { Console.WriteLine("Web application name:" + app.Name); Console.WriteLine("Web application type:" + app.TypeName); Console.WriteLine("Web application ID:" + app.Id); Console.WriteLine(); Console.Readline(); } 6. Get a reference to the Web application's collection of site collections. [C#] SPSiteCollection mySiteCols = myApp.Sites; 7. To back up a site collection, call the SPSiteCollection.Backup method. As parameters pass the following: · The full URL of the site collection; that is, the full URL of its Top Level Web site. · The full path and file name of the file that will hold the compressed content of the site collection. · True, if the operation should overwrite an existing backup file of the same name; false, if it should not. [C#] mySiteCols.Backup(@"htt//Server/sites/MySiteCollection", @"\\OtherServer\WSSBackups\SiteCollections\BackupOfMySiteCollection", true); 8. To restore a site collection, call the SPSiteCollection.Restore method. It takes the same parameters as the SPSiteCollection.Backup method. The Boolean parameter indicates whether the site collection should be overwritten if it already exists at the specified URL. [C#] mySiteCols.Restore(@"htt//Server/sites/MySiteCollection", @"\\OtherServer\WSSBackups\SiteCollections\BackupOfMySiteCollection", true); |
Example
The following example shows a simple way to programmatically back up or restore a site collection. You will need to replace all the System.Guid values with actual values from your deployment and replace the placeholder values in the Backup and Restore methods with actual URLs and paths from your deployment.
// Get a reference to the Web application publishing
// Web service.
SPFarm myFarm = SPFarm.Local;
SPServiceCollection myServices = myFarm.Services;
Guid serviceID = new Guid("21d91b29-5c5b-4893-9264-4e9c758618b4");
SPWebService webPubService = (SPWebService)myServices[serviceID];
// Get a reference to the Web application that hosts the
// site collection.
SPWebApplicationCollection myApps = webPubService.WebApplications;
Guid appID = new Guid("10ea4e6f-ae37-4909-b04f-f516c066bc37");
SPWebApplication myApp = myApps[appID];
// 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 = myApp.Sites;
// Back up a specified site collection.
mySiteCols.Backup(@"htt//Server/sites/MySiteCollection", @"\\OtherServer\WSSBackups\SiteCollections\BackupOfMySiteCollection", true);
// Restoring the site collection is identical to the preceding
// code except that the "Restore" is used in place of "Backup".
//
// mySiteCols.Restore(@"htt//Server/sites/MySiteCollection", @"\\OtherServer\WSSBackups\SiteCollections\BackupOfMySiteCollection", true);
Comments
The Microsoft.SharePoint.SPSite class does not implement Backup.IBackupRestore and the SPSiteCollection.Backup and SPSiteCollection.Restore methods do not use the facilities of the Microsoft.SharePoint.Administration.Backup namespace. This means that records of backups and restorations of site collections are not kept in a history file (spbrtoc.xml) in the backup directory. Similarly, backup and restoration data is not stored in spbackup.xml or sprestore.xml files, neither are these site collection operations logged in spbackup.log or sprestore.log files.
If you want to do any kind of logging of backups and restorations of site collection operations, you will have to program your own system. Writing to the system-created spbrtoc.xml, spbackup.xml, sprestore.xml, spbackup.log, and sprestore.log files is not supported in Windows SharePoint Services 3.0. Neither is moving them, deleting them, or renaming them. However, you can create files that merge data from the system-created files with data from your site collection backups and restorations.
See Also
Programming with the Windows SharePoint Services Backup/Restore Object Model
How to: Programmatically Back Up Content
How to: Programmatically Restore Content
How to: Create a Content Class That Can Be Backed Up and Restored
Comments
Anonymous
February 07, 2008
PingBack from http://blogs.msdn.com/sharepointdeveloperdocs/archive/2008/02/07/programming-with-the-windows-sharepoint-services-backup-restore-object-model.aspxAnonymous
February 25, 2008
The comment has been removed