如何:以编程方式还原内容
上次修改时间: 2010年11月4日
适用范围: SharePoint Foundation 2010
本主题介绍如何创建从备份中还原 SharePoint Foundation 内容组件的应用程序。本主题假定您已熟悉在 SharePoint Foundation 中备份和还原数据概述和使用 SharePoint Foundation 备份/还原对象模型进行编程。
还原内容组件
向 Visual Studio 项目中添加对 Microsoft.SharePoint 的引用,并向代码文件中添加用于 Microsoft.SharePoint.Administration 和 Microsoft.SharePoint.Administration.Backup 命名空间的 using 语句。
在 Main 方法中,通过使用静态 GetRestoreSettings 方法创建 SPRestoreSettings 对象。为第一个参数传递存储备份的路径。为第二个参数传递某个 SPRestoreMethodType 值的字符串版本。
SPRestoreSettings settings = SPBackupRestoreSettings.GetRestoreSettings((@"\\Server\WSSBackups", "Overwrite");
Dim settings As SPRestoreSettings = SPBackupRestoreSettings.GetRestoreSettings(("\\Server\WSSBackups", "Overwrite")
提示用户指定要还原的内容组件,并将其名称分配给 IndividualItem 属性。若要查看在最后一次完整备份中包括的、可以是还原操作对象的服务器场上的组件名称的详细列表,可以在服务器命令行处运行 stsadm -o restore -showtree 命令。若要指定不同的完整备份包,请使用 -backupid 参数。此外,还可以访问管理中心应用程序中的"操作">"执行还原操作"。若要指定整个服务器场,请将"Farm"用作名称。(将该属性设置为 null 也将选择整个服务器场来进行备份,并应假定在所有后续代码中都使用 IndividualItem 来按名称标识要还原的组件。有关示例,请参阅步骤 9 中的 FindItems() 方法的使用。)
Console.Write("Enter name of component to restore (default is whole farm):"); settings.IndividualItem = Console.ReadLine();
Console.Write("Enter name of component to restore (default is whole farm):") settings.IndividualItem = Console.ReadLine()
若要从最近备份之外的备份中执行还原操作,请通过将备份包的 GUID 分配给 BackupId 属性来标识该程序包。特定备份位置的每个备份操作的记录都存储在该位置根部的 spbrtoc.xml 中。每个备份和还原操作均由文件中的 <SPHistoryObject> 元素表示。如果是备份操作,则 <SPHistoryObject> 元素的 <IsBackup> 子元素为"True"。而 <SPHistoryObject> 元素的 <SPId> 元素则包含备份的 GUID。
备注
若要以编程方式获取所有备份和还原操作的列表,请使用 GetHistory() 方法。此方法将返回包含 SPBackupRestoreHistoryObject 对象的 SPBackupRestoreHistoryList 对象。所包含的每个对象都代表一种操作并且将其 GUID 保存在 SelfId 属性中。
settings.BackupId = new Guid("GUID");
settings.BackupId = New Guid("GUID")
也可以设置 IsVerbose 和 UpdateProgress 属性之一或同时设置这两个属性。(有关这些属性的详细信息,请参阅针对这些属性的参考主题。)
settings.IsVerbose = true; settings.UpdateProgress = 10;
settings.IsVerbose = True settings.UpdateProgress = 10
如有必要,可以设置 FarmAdminLoginName 和 FarmAdminLoginPassword() 属性。
settings.FarmAdminLoginName = "Bob"; settings.FarmAdminPassword = "7*j2U";
settings.FarmAdminLoginName = "Bob" settings.FarmAdminPassword = "7*j2U"
使用 CreateBackupRestore() 方法创建还原操作。(还会创建操作的历史记录对象。)
Guid restore = SPBackupRestoreConsole.CreateBackupRestore(settings);
Dim restore As Guid = SPBackupRestoreConsole.CreateBackupRestore(settings)
如果用户界面要求用户键入一个组件名称而不是从列表中选取一个组件名称,则您必须确保输入的名称与某个组件的名称完全匹配。向 Main 方法中添加以下行。
SPBackupRestoreObject node = EnsureUniqueValidComponentName(settings, ref restore);
Dim node As SPBackupRestoreObject = EnsureUniqueValidComponentName(settings, restore)
添加以下 EnsureUniqueValidComponentName 方法的实现。使用 FindItems() 方法可检索其名称与用户输入的名称完全匹配的内容对象的集合。如果未找到匹配项,则提示用户重试。如果找到多个匹配项,则提示用户输入更加具体的信息。如果用户输入的组件名称明确而有效,则会获取对表示用户要还原的组件的 SPBackupRestoreObject 对象的引用。
private static SPBackupRestoreObject EnsureUniqueValidComponentName(SPBackupRestoreSettings settings, ref Guid operationGUID) { SPBackupRestoreObjectCollection list = SPBackupRestoreConsole.FindItems(operationGUID, settings.IndividualItem); SPBackupRestoreObject component = null; if (list.Count <= 0) { Console.WriteLine("There is no component with that name. Run again with a new name."); Console.WriteLine("Press Enter to continue."); Console.ReadLine(); } else if (list.Count > 1) // The component name specified is ambiguous. Prompt user to be more specific. { Console.WriteLine("More than one component matches the name you entered."); Console.WriteLine("Run again with one of the following:"); for (int i = 0; i < list.Count; i++) { Console.WriteLine("\t{0}", list[i].ToString()); } Console.WriteLine("Press Enter to continue."); Console.ReadLine(); } else { component = list[0]; } return component; }
Private Shared Function EnsureUniqueValidComponentName(ByVal settings As SPBackupRestoreSettings, ByRef operationGUID As Guid) As SPBackupRestoreObject Dim list As SPBackupRestoreObjectCollection = SPBackupRestoreConsole.FindItems(operationGUID, settings.IndividualItem) Dim component As SPBackupRestoreObject = Nothing If list.Count <= 0 Then Console.WriteLine("There is no component with that name. Run again with a new name.") Console.WriteLine("Press Enter to continue.") Console.ReadLine() ElseIf list.Count > 1 Then ' The component name specified is ambiguous. Prompt user to be more specific. Console.WriteLine("More than one component matches the name you entered.") Console.WriteLine("Run again with one of the following:") For i As Integer = 0 To list.Count - 1 Console.WriteLine(vbTab & "{0}", list(i).ToString()) Next i Console.WriteLine("Press Enter to continue.") Console.ReadLine() Else component = list(0) End If Return component End Function
在 Main 方法中,创建仅当 EnsureUniqueValidComponentName 方法已返回有效的节点时才将运行的条件结构。
if (node != null) { // TODO: Set the restore operation as the active operation // and run it. }
If node IsNot Nothing Then ' TODO: Set the restore operation as the active operation ' and run it. End If
将上一步骤中的"TODO"行替换为以下代码。这会使用 SetActive() 方法将操作设置为活动操作,并进行测试以验证操作是否成功。如果操作失败(当另一个备份或还原操作正在进行时将出现此情况),则将向应用程序的用户界面报告错误。
if (SPBackupRestoreConsole.SetActive(restore) == true) { // TODO: Run the operation. See next step. } else { // Report through your UI that another backup // or restore operation is underway. Console.WriteLine("Another backup or restore operation is already underway. Try again when it ends."); }
If SPBackupRestoreConsole.SetActive(restore) = True Then ' TODO: Run the operation. See next step. Else ' Report through your UI that another backup ' or restore operation is underway. Console.WriteLine("Another backup or restore operation is already underway. Try again when it ends.") End If
在 SetActive() 调用成功时将运行的代码分支中,使用 Run() 方法运行操作。检测操作是否成功。如果操作失败,则将操作失败的消息报告给用户界面。以下代码将替换上一步骤中的"TODO"行。
if (SPBackupRestoreConsole.Run(restore, node) == false) { // Report "error" through your UI. String error = SPBackupRestoreConsole.Get(restore).FailureMessage; Console.WriteLine(error); }
If SPBackupRestoreConsole.Run(restore, node) = False Then ' Report "error" through your UI. Dim [error] As String = SPBackupRestoreConsole.Get(restore).FailureMessage Console.WriteLine([error]) End If
使用 Remove() 方法清除还原内容。将下面的代码添加到步骤 10 中所插入的右大括号前面。
// Clean up the operation. SPBackupRestoreConsole.Remove(restore); Console.WriteLine("Restore attempt complete. Press Enter to continue."); Console.ReadLine();
' Clean up the operation. SPBackupRestoreConsole.Remove(restore) Console.WriteLine("Restore attempt complete. Press Enter to continue.") Console.ReadLine()
示例
下面的代码演示如何对内容组件的还原操作进行编程。用备份位置的路径替换占位符 \\Server\WSSBackups。运行库将自动查找该位置中的最近备份。
using System;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.Backup;
namespace MyCompany.SharePoint.Administration.Backup
{
class Restore
{
static void Main(string[] args)
{
// Create the restore settings.
SPRestoreSettings settings = SPBackupRestoreSettings.GetRestoreSettings(@"\\Server\WSSBackups", "Overwrite");
// Identify the content component to restore.
Console.Write("Enter name of component to restore (default is whole farm):");
settings.IndividualItem = Console.ReadLine();
// Set optional operation parameters.
settings.IsVerbose = true;
settings.UpdateProgress = 10;
// Create the restore operation and return its ID.
Guid restore = SPBackupRestoreConsole.CreateBackupRestore(settings);
SPBackupRestoreObject node = EnsureUniqueValidComponentName(settings, ref restore);
if (node != null)
{
// Set the restore as the active job and run it.
if (SPBackupRestoreConsole.SetActive(restore) == true)
{
if (SPBackupRestoreConsole.Run(restore, node) == false)
{
// Report "error" through your UI.
String error = SPBackupRestoreConsole.Get(restore).FailureMessage;
Console.WriteLine(error);
}
}
else
{
// Report through your UI that another backup
// or restore operation is underway.
Console.WriteLine("Another backup or restore operation is already underway. Try again when it ends.");
}
// Clean up the operation.
SPBackupRestoreConsole.Remove(restore);
Console.WriteLine("Restore attempt complete. Press Enter to continue.");
Console.ReadLine();
}
}// end Main
private static SPBackupRestoreObject EnsureUniqueValidComponentName(SPBackupRestoreSettings settings, ref Guid operationGUID)
{
SPBackupRestoreObjectCollection list = SPBackupRestoreConsole.FindItems(operationGUID, settings.IndividualItem);
SPBackupRestoreObject component = null;
if (list.Count <= 0)
{
Console.WriteLine("There is no component with that name. Run again with a new name.");
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
}
else if (list.Count > 1) // The component name specified is ambiguous. Prompt user to be more specific.
{
Console.WriteLine("More than one component matches the name you entered.");
Console.WriteLine("Run again with one of the following:");
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("\t{0}", list[i].ToString());
}
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
}
else
{
component = list[0];
}
return component;
}// end EnsureUniqueValidComponentName
}// end Restore class
}// end namespace
Imports System
Imports Microsoft.SharePoint.Administration
Imports Microsoft.SharePoint.Administration.Backup
Namespace MyCompany.SharePoint.Administration.Backup
Friend Class Restore
Shared Sub Main(ByVal args() As String)
' Create the restore settings.
Dim settings As SPRestoreSettings = SPBackupRestoreSettings.GetRestoreSettings("\\Server\WSSBackups", "Overwrite")
' Identify the content component to restore.
Console.Write("Enter name of component to restore (default is whole farm):")
settings.IndividualItem = Console.ReadLine()
' Set optional operation parameters.
settings.IsVerbose = True
settings.UpdateProgress = 10
' Create the restore operation and return its ID.
Dim restore As Guid = SPBackupRestoreConsole.CreateBackupRestore(settings)
Dim node As SPBackupRestoreObject = EnsureUniqueValidComponentName(settings, restore)
If node IsNot Nothing Then
' Set the restore as the active job and run it.
If SPBackupRestoreConsole.SetActive(restore) = True Then
If SPBackupRestoreConsole.Run(restore, node) = False Then
' Report "error" through your UI.
Dim [error] As String = SPBackupRestoreConsole.Get(restore).FailureMessage
Console.WriteLine([error])
End If
Else
' Report through your UI that another backup
' or restore operation is underway.
Console.WriteLine("Another backup or restore operation is already underway. Try again when it ends.")
End If
' Clean up the operation.
SPBackupRestoreConsole.Remove(restore)
Console.WriteLine("Restore attempt complete. Press Enter to continue.")
Console.ReadLine()
End If
End Sub ' end Main
Private Shared Function EnsureUniqueValidComponentName(ByVal settings As SPBackupRestoreSettings, ByRef operationGUID As Guid) As SPBackupRestoreObject
Dim list As SPBackupRestoreObjectCollection = SPBackupRestoreConsole.FindItems(operationGUID, settings.IndividualItem)
Dim component As SPBackupRestoreObject = Nothing
If list.Count <= 0 Then
Console.WriteLine("There is no component with that name. Run again with a new name.")
Console.WriteLine("Press Enter to continue.")
Console.ReadLine()
ElseIf list.Count > 1 Then ' The component name specified is ambiguous. Prompt user to be more specific.
Console.WriteLine("More than one component matches the name you entered.")
Console.WriteLine("Run again with one of the following:")
For i As Integer = 0 To list.Count - 1
Console.WriteLine(vbTab & "{0}", list(i).ToString())
Next i
Console.WriteLine("Press Enter to continue.")
Console.ReadLine()
Else
component = list(0)
End If
Return component
End Function ' end EnsureUniqueValidComponentName
End Class ' end Restore class
End Namespace ' end namespace
请参阅
任务
引用
Microsoft.SharePoint.Administration.Backup
概念
使用 SharePoint Foundation 备份/还原对象模型进行编程