IBackupRestore.OnRestore 方法

读取备份内容并将其复制到目标位置的还原操作。

命名空间:  Microsoft.SharePoint.Administration.Backup
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
Function OnRestore ( _
    sender As Object, _
    args As SPRestoreInformation _
) As Boolean
用法
Dim instance As IBackupRestore
Dim sender As Object
Dim args As SPRestoreInformation
Dim returnValue As Boolean

returnValue = instance.OnRestore(sender, _
    args)
bool OnRestore(
    Object sender,
    SPRestoreInformation args
)

参数

返回值

类型:System.Boolean
true如果成功 ;否则为false。

备注

如果您的内容类可以迁移,则您的代码应检查所用的还原方法,并调用 Rename()(如果还原方法是 New)。

如果您的内容类不有它可能具有的任何IBackupRestore子对象之外的任何内容,您的实现应只需将**CurrentProgess()**设置为至少 50%并返回true ,如下面的示例所示。请不要调用任何IBackupRestore子对象的OnRestore方法。

public Boolean OnRestore(Object sender, SPRestoreInformation args)
{
    if (args == null)
    {
        throw new ArgumentNullException("args");
    }
    if (args.RestoreMethod == SPRestoreMethodType.New)
    {
        args.Rename();
    }

    args.CurrentProgress = 50;
    return true;
}
Public Function OnRestore(ByVal sender As Object, ByVal args As SPRestoreInformation) As Boolean
    If args Is Nothing Then
        Throw New ArgumentNullException("args")
    End If
    If args.RestoreMethod = SPRestoreMethodType.New Then
        args.Rename()
    End If

    args.CurrentProgress = 50
    Return True
End Function

如果您的类在它可能具有的任何 IBackupRestore 子对象之外仍然有 内容,则您的实现必须将此内容复制到还原目标位置。如果由于任何原因导致内容复制失败,则将返回 false。

下面的示例演示 OnRestore() 的独立实现的总体结构:

public Boolean OnRestore(Object sender, SPRestoreInformation args)
{
    if (args == null)
    {
        throw new ArgumentNullException("args");
    }
    if (args.RestoreMethod == SPRestoreMethodType.New)
    {
        args.Rename();
    }

    args.CurrentProgress = 50;
    Boolean successSignal = true;

    // TODO: Implement copying your content to the destination.
    //       If the copy fails, set successSignal to false.

    return successSignal;
}
Public Function OnRestore(ByVal sender As Object, ByVal args As SPRestoreInformation) As Boolean
    If args Is Nothing Then
        Throw New ArgumentNullException("args")
    End If
    If args.RestoreMethod = SPRestoreMethodType.New Then
        args.Rename()
    End If

    args.CurrentProgress = 50
    Dim successSignal As Boolean = True

    ' TODO: Implement copying your content to the destination.
    '       If the copy fails, set successSignal to false.

    Return successSignal
End Function

如果 Windows 服务或应用程序,需要停止或恢复期间暂停,则可以这样开头的OnRestore。(在重新启动服务或应用程序OnPostRestore(Object, SPBackupInformation)。)不**OnPreRestore(Object, SPBackupInformation)**后, 一种方法称为对于每个组件,请在此工作,即使它不正在恢复 ;但只为还原的组件调用OnBackupComplete ,准备阶段以便在中停止的服务或应用程序不能保证会获得重新启动。

如果OnPreRestore返回false,则不会运行的OnRestore方法。如果OnRestore返回false,则不会运行的OnPostRestore方法。

示例

下面的示例演示了OnRestore ,将文件还原到前端服务器的实现。FrontEndFilePaths是一个私有字段。它包含的路径所要还原的文件的集合。

public Boolean OnRestore(Object sender, SPRestoreInformation args)
{
    if (args == null)
    {
        throw new ArgumentNullException("args");
    }

    // If the CriticalFiles object was deleted from the farm after it was
    // backed up, restore it to the configuration database.
    CriticalFiles cf = SPFarm.Local.GetChild<CriticalFiles>(this.Name);
    if (cf == null)
    {
        this.Update();
        args.Log(SPBackupRestoreLogSeverity.Verbose, this.Name + " added back to configuration database.");
    }

    Boolean successSignal = true;

    // TODO: The following loop restores files to the local server. If there are 
    //       multiple front end servers, your code must iterate through all of 
    //       SPFarm.Local.Servers and restore the same files to every server whose
    //       Role property is SPServerRole.WebFrontEnd

    foreach (String path in FrontEndFilePaths)
    {
        FileInfo backupCopy = new FileInfo(path);
        FileInfo file = new FileInfo(args.Location + @"\" + backupCopy.Name);
        try
        {
            file.CopyTo(path, true);
            args.Log(SPBackupRestoreLogSeverity.Verbose, "Restored " + file.Name);
        }
        catch (Exception e)
        {
            args.Log(SPBackupRestoreLogSeverity.Verbose, file.Name + " not restored: " + e.Message);
            successSignal = false;
        }
    }
    
    args.CurrentProgress = 50;
    return successSignal;
}
Public Function OnRestore(ByVal sender As Object, ByVal args As SPRestoreInformation) As Boolean
    If args Is Nothing Then
        Throw New ArgumentNullException("args")
    End If

    ' If the CriticalFiles object was deleted from the farm after it was
    ' backed up, restore it to the configuration database.
    Dim cf As CriticalFiles = SPFarm.Local.GetChild(Of CriticalFiles)(Me.Name)
    If cf Is Nothing Then
        Me.Update()
        args.Log(SPBackupRestoreLogSeverity.Verbose, Me.Name & " added back to configuration database.")
    End If

    Dim successSignal As Boolean = True

    ' TODO: The following loop restores files to the local server. If there are 
    '       multiple front end servers, your code must iterate through all of 
    '       SPFarm.Local.Servers and restore the same files to every server whose
    '       Role property is SPServerRole.WebFrontEnd

    For Each path As String In FrontEndFilePaths
        Dim backupCopy As New FileInfo(path)
        Dim file As New FileInfo(args.Location & "\" & backupCopy.Name)
        Try
            file.CopyTo(path, True)
            args.Log(SPBackupRestoreLogSeverity.Verbose, "Restored " & file.Name)
        Catch e As Exception
            args.Log(SPBackupRestoreLogSeverity.Verbose, file.Name & " not restored: " & e.Message)
            successSignal = False
        End Try
    Next path

    args.CurrentProgress = 50
    Return successSignal
End Function

另请参阅

引用

IBackupRestore 接口

IBackupRestore 成员

Microsoft.SharePoint.Administration.Backup 命名空间