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)

bool OnBackup(
	Object sender,
	SPBackupInformation args
)

Parameters

sender
Type: System.Object

The object that initiated the backup operation.

args
Type: Microsoft.SharePoint.Administration.Backup.SPBackupInformation

An SPBackupInformation object that contains data about the operation.

Return value

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

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;
}

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;
}

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.

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;
}
Show: