다음을 통해 공유


How to: Programmatically Back Up and Restore a Single Site Collection

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

This topic describes how to back up and restore individual site collections programmatically.

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.

    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 Guid which is the value of its Id property.

    Guid serviceID = new Guid("21d91b29-5c5b-4893-9264-4e9c758618b4");
    SPWebService webPubService = (SPWebService)myServices[serviceID];
    

    If you do not know the Id of the application publishing Web service, you can iterate through all the services and report their Name, TypeName, and Id. The following is an example:

    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 Lookup method. Alternatively, you can use the application's Guid which is the value of its Id property. The following code shows the second method.

    SPWebApplicationCollection myApps = webPubService.WebApplications;
    Guid appID = new Guid("10ea4e6f-ae37-4909-b04f-f516c066bc37");
    SPWebApplication myApp = myApps[appID];
    

    If you do not know the Id of the Web application that hosts your site collection, you can iterate through all the Web applications and report their Name, TypeName, and Id. The following is an example:

    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.

    SPSiteCollection mySiteCols = myApp.Sites;
    
  7. To back up a site collection, call the 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.

    mySiteCols.Backup(@"http://Server/sites/MySiteCollection", @"\\OtherServer\WSSBackups\SiteCollections\BackupOfMySiteCollection", true);
    
  8. To restore a site collection, call the Restore method. It takes the same parameters as the Backup method. The Boolean parameter indicates whether the site collection should be overwritten if it already exists at the specified URL.

    mySiteCols.Restore(@"http://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 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(@"http://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(@"http://Server/sites/MySiteCollection", @"\\OtherServer\WSSBackups\SiteCollections\BackupOfMySiteCollection", true);

The SPSite class does not implement IBackupRestore and the Backup and 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

Tasks

How to: Programmatically Back Up Content

How to: Programmatically Restore Content

How to: Create a Content Class That Can Be Backed Up and Restored

How to: Extend the STSADM Utility

Reference

Microsoft.SharePoint.Administration.Backup

Backup

Restore

Concepts

Programming with the Windows SharePoint Services Backup/Restore Object Model

Other Resources

Stsadm.exe command-line tool