共用方式為


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

Applies to: SharePoint Foundation 2010

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 Microsoft.Sharepoint.

  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;
    
    Dim myFarm As SPFarm = SPFarm.Local
    Dim myServices As SPServiceCollection = 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];
    
    Dim serviceID As New Guid("21d91b29-5c5b-4893-9264-4e9c758618b4")
    Dim webPubService As SPWebService = CType(myServices(serviceID), SPWebService)
    

    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();
        }
    }
    
    For Each service As SPService In myServices
       If TypeOf service Is SPWebService Then
          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()
       End If
    Next service
    
  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];
    
    Dim myApps As SPWebApplicationCollection = webPubService.WebApplications
    Dim appID As New Guid("10ea4e6f-ae37-4909-b04f-f516c066bc37")
    Dim myApp As SPWebApplication = 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();
    }
    
    For Each app As SPWebApplication 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()
    Next app
    
  6. Get a reference to the Web application's collection of site collections.

    SPSiteCollection mySiteCols = myApp.Sites;
    
    Dim mySiteCols As SPSiteCollection = 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(@"https://Server/sites/MySiteCollection", @"\\OtherServer\WSSBackups\SiteCollections\BackupOfMySiteCollection", true);
    
    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(@"https://Server/sites/MySiteCollection", @"\\OtherServer\WSSBackups\SiteCollections\BackupOfMySiteCollection", true);
    
    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(@"https://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(@"https://Server/sites/MySiteCollection", @"\\OtherServer\WSSBackups\SiteCollections\BackupOfMySiteCollection", true);
' Get a reference to the Web application publishing
' Web service.
Dim myFarm As SPFarm = SPFarm.Local
Dim myServices As SPServiceCollection = myFarm.Services
Dim serviceID As New Guid("21d91b29-5c5b-4893-9264-4e9c758618b4")
Dim webPubService As SPWebService = CType(myServices(serviceID), SPWebService)

' Get a reference to the Web application that hosts the 
' site collection.
Dim myApps As SPWebApplicationCollection = webPubService.WebApplications
Dim appID As New Guid("10ea4e6f-ae37-4909-b04f-f516c066bc37")
Dim myApp As SPWebApplication = 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. 
Dim mySiteCols As SPSiteCollection = 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 SharePoint Foundation. 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 SharePoint Foundation Backup/Restore Object Model

Other Resources

Stsadm.exe command-line tool