Export (0) Print
Expand All

How to: Create a Database Class That Can Be Restored From a Snapshot

SharePoint 2010

Last modified: January 13, 2010

Applies to: SharePoint Foundation 2010

If you have a custom database component hosted in either the Enterprise or Developer editions of Microsoft SQL Server that you want to be restorable from a snapshot through the Microsoft SharePoint Foundation UI or object model, you must represent the component with a class that implements the IDatabaseSnapshotRestore interface. This topic explains how to do that.

Caution note Caution

Snapshots require that the database be up and running, so a snapshot cannot be a general-purpose backup for the database. Hence, in a realistic scenario, your class must also implement the IBackupRestore interface. Accordingly, this topic assumes that you have carried out the procedures in How to: Create a Content Class That Can Be Backed Up and Restored and that the class that implements IDatabaseSnapshotRestore is the same class that you created in the latter topic.

To Implement the Members of IDatabaseSnapshotRestore

  1. Implement the OnPreRestore(SPDatabaseSnapshotRestoreEvent) method. Provide any custom logic that needs to execute before a database is restored from a snapshot.

    Important note Important

    Your implementation must throw an exception if it cannot execute any necessary precondition to restoration. For more information about this requirement, see the reference topics for the overloads of SPDatabaseSnapshot.Restore().

    The following example shows an implementation that takes the database offline and then pauses the thread for 5 seconds before letting the restoration proceed. In this sample, "this" refers to an object of a custom type called SupplementalDatabase that a developer has derived from the SPDatabase class.

    
    public void OnPreRestore(SPDatabaseSnapshotRestoreEvent args)
    {
        if (args == null)
        {
            throw new ArgumentNullException("args");
        }
    
        try
        {
            this.Status = SPObjectStatus.Offline;
            this.Update();
            Thread.Sleep(5000);
        }
        catch (exception)
        {
            Exception e = new Exception(String.Format("Restoration from {0} cancelled because pre-restoration steps could not be executed.", args.Snapshot.Name), exception);
            throw e;
        }
    }
    
    
  2. Implement the OnPostRestore(SPDatabaseSnapshotRestoreEvent) method. Provide any custom logic that needs to execute after a database is restored from a snapshot. The following example shows an implementation that sets the database back online. In this sample, "this" refers to an object of a custom type called SupplementalDatabase that a developer has derived from the SPDatabase class.

    
    public void OnPostRestore(SPDatabaseSnapshotRestoreEvent args)
    {
        if (args == null)
        {
            throw new ArgumentNullException("args");
        }
    
        this.Status = SPObjectStatus.Online;
        this.Update(true);
    }
    
    
    NoteNote

    Your custom class will implement two OnPostRestore methods, with different signatures; the first from IBackupRestore and the other from IDatabaseSnapshotRestore.

Show:
© 2015 Microsoft