如何:以编程方式备份和还原单个网站集

上次修改时间: 2010年11月4日

适用范围: SharePoint Foundation 2010

本主题介绍如何以编程方式备份和还原单独的网站集。

备份或还原网站集

  1. 向 Visual Studio 项目中添加对 Microsoft.Sharepoint 的引用。

  2. 添加针对 Microsoft.SharePoint 和 Microsoft.SharePoint.Administration 的 Using 语句。

  3. 添加以下行以获取对服务器场及其服务集合的引用。

    SPFarm myFarm = SPFarm.Local;
    SPServiceCollection myServices = myFarm.Services;
    
    Dim myFarm As SPFarm = SPFarm.Local
    Dim myServices As SPServiceCollection = myFarm.Services
    
  4. 通过使用 Web 服务的 Id() 属性值 Guid 来获取对 Web 服务的引用,该 Web 服务用于发布承载网站集的 Web 应用程序。

    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)
    

    如果您不知道发布 Web 服务的应用程序的 Id(),则可以遍历所有服务并报告服务的 Name()TypeNameId()。示例如下:

    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. 获取对承载网站集的 Web 应用程序的引用。如果您知道 Web 应用程序的 URL,则可以使用静态 Lookup() 方法来获取引用。此外,还可以使用应用程序的 Id() 属性值 Guid。下面的代码演示了第二种方法。

    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)
    

    如果您不知道承载网站集的 Web 应用程序的 Id(),则可以遍历所有 Web 应用程序并报告应用程序的 Name()TypeNameId()。示例如下:

    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. 获取对 Web 应用程序的网站集集合的引用。

    SPSiteCollection mySiteCols = myApp.Sites;
    
    Dim mySiteCols As SPSiteCollection = myApp.Sites
    
  7. 若要备份网站集,请调用 Backup() 方法。当参数传递以下内容时将采用此方法:

    • 网站集的完整 URL;即,其首要网站的完整 URL。

    • 将保存压缩的网站集内容的文件的完整路径和文件名。

    • 如果操作应覆盖具有相同名称的现有备份文件,则为 True;如果不应覆盖,则为 false。

    mySiteCols.Backup(@"https://Server/sites/MySiteCollection", @"\\OtherServer\WSSBackups\SiteCollections\BackupOfMySiteCollection", true);
    
    mySiteCols.Backup("http:// Server/sites/MySiteCollection", "\\OtherServer\WSSBackups\SiteCollections\BackupOfMySiteCollection", True)
    
  8. 若要还原网站集,请调用 Restore() 方法。它采用的参数与 Backup() 方法采用的参数相同。Boolean 参数指示在指定的 URL 中已经存在网站集的情况下是否应覆盖该网站集。

    mySiteCols.Restore(@"https://Server/sites/MySiteCollection", @"\\OtherServer\WSSBackups\SiteCollections\BackupOfMySiteCollection", true);
    
    mySiteCols.Restore("http:// Server/sites/MySiteCollection", "\\OtherServer\WSSBackups\SiteCollections\BackupOfMySiteCollection", True)
    

示例

下面的示例演示以编程方式备份或还原网站集的一种简单方法。您将需要用部署中的实际值替换所有 Guid 值,并用部署的实际 URL 和路径替换 Backup 和 Restore 方法中的占位符值。

// 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);

SPSite 类未实现 IBackupRestore,并且 Backup()Restore() 方法未使用 Microsoft.SharePoint.Administration.Backup 命名空间的功能。这意味着网站集的备份和还原记录 保留在备份目录中的历史记录文件 (spbrtoc.xml) 中。同样,备份和还原数据未存储在 spbackup.xml 或 sprestore.xml 文件中,并且这些网站集操作未记录在 spbackup.log 或 sprestore.log 文件中。

如果想对备份和还原网站集的操作进行任何类型的日志记录,您必须对自己的系统进行编程。在 SharePoint Foundation 中不支持写入到系统创建的 spbrtoc.xml,spbackup.xml、sprestore.xml、spbackup.log 和 sprestore.log 文件,也不支持移动、删除或重命名这些文件。不过,您可以创建文件以便将系统创建的文件中的数据与网站集备份和还原数据进行合并。

请参阅

任务

如何:以编程方式备份内容

如何:以编程方式还原内容

如何:创建可以备份和还原的内容类

如何:扩展 STSADM 实用工具

引用

Microsoft.SharePoint.Administration.Backup

Backup

Restore

概念

使用 SharePoint Foundation 备份/还原对象模型进行编程

其他资源

Stsadm.exe 命令行工具