IBackupRestore.OnBackup method

Creates and stores the backup copy of the content component.

Namespace:  Microsoft.SharePoint.Administration.Backup
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Function OnBackup ( _
    sender As Object, _
    args As SPBackupInformation _
) As Boolean
'Usage
Dim instance As IBackupRestore
Dim sender As Object
Dim args As SPBackupInformation
Dim returnValue As Boolean

returnValue = instance.OnBackup(sender, _
    args)
bool OnBackup(
    Object sender,
    SPBackupInformation args
)

Parameters

  • sender
    Type: System.Object

    The object that initiated the backup operation.

Return value

Type: System.Boolean
true if successful; otherwise, false.

Remarks

If your content class has no content outside of any IBackupRestore child objects it may have, your implementation should simply set the CurrentProgess() to at least 50 percent and return true as seen in the following example. Do not call the OnBackup method of any IBackupRestore child objects.

public Boolean OnBackup(Object sender, SPBackupInformation args)
{
    if (args == null)
    {
        throw new ArgumentNullException("args");
    }
    args.CurrentProgress = 50;
    return true;
}
Public Function OnBackup(ByVal sender As Object, ByVal args As SPBackupInformation) As Boolean
    If args Is Nothing Then
        Throw New ArgumentNullException("args")
    End If
    args.CurrentProgress = 50
    Return True
End Function

If your class does have content outside of any IBackupRestore child objects it may have, your implementation must copy this content to args.Location. Return false, if for any reason the copy of content fails.

The following example shows the overall structure of a substantive implementation of OnBackup():

public Boolean OnBackup(Object sender, SPBackupInformation args)
{
    if (args == null)
    {
        throw new ArgumentNullException("args");
    }
    args.CurrentProgress = 50;
    Boolean successSignal = true;

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

    return successSignal;
}
Public Function OnBackup(ByVal sender As Object, ByVal args As SPBackupInformation) As Boolean
    If args Is Nothing Then
        Throw New ArgumentNullException("args")
    End If
    args.CurrentProgress = 50
    Dim successSignal As Boolean = True

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

    Return successSignal
End Function

If a Windows service, or some application, needs to be stopped or paused during backup, you can do so at the beginning of OnBackup(Object, SPBackupInformation). (Restart the service or application in OnBackupComplete.) Do not do this work in OnPrepareBackup The latter method is called for every component, even if it is not being backed up; but OnBackupComplete is called only for components that are backed up, so there is no guarantee that a service or application stopped in the prepare backup stage would get restarted.

The OnBackup method will not run if OnPrepareBackup returns false. If OnBackup returns false, the OnBackupComplete method will not run.

Examples

The following example shows an implementation of OnBackup that copies files to the backup location. FrontEndFilePaths is a private field. It is a collection of strings holding the paths of the files that are to be backed up.

public Boolean OnBackup(Object sender, SPBackupInformation args)
{
    if (args == null)
    {
        throw new ArgumentNullException("args");
    }
    
    Boolean successSignal = true;

    foreach (String path in FrontEndFilePaths)
    {
        FileInfo file = new FileInfo(path);
        try
        {
            file.CopyTo(args.Location + @"\" + file.Name, true);
            args.Log(SPBackupRestoreLogSeverity.Verbose, "Backed up " + file.Name);
        }
        catch (Exception e)
        {
            args.Log(SPBackupRestoreLogSeverity.Verbose, file.Name + " not backed up: " + e.Message);
            successSignal = false;
        }
    }

    args.CurrentProgress = 50;
    return successSignal;
}
Public Function OnBackup(ByVal sender As Object, ByVal args As SPBackupInformation) As Boolean
    If args Is Nothing Then
        Throw New ArgumentNullException("args")
    End If

    Dim successSignal As Boolean = True

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

    args.CurrentProgress = 50
    Return successSignal
End Function

See also

Reference

IBackupRestore interface

IBackupRestore members

Microsoft.SharePoint.Administration.Backup namespace