Condividi tramite


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

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.

Warning

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

    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;
        }
    }
    
    Public Sub OnPreRestore(ByVal args As SPDatabaseSnapshotRestoreEvent)
            If args Is Nothing Then
                    Throw New ArgumentNullException("args")
            End If
    
            Try
                    Me.Status = SPObjectStatus.Offline
                    Me.Update()
                    Thread.Sleep(5000)
            Catch e1 As exception
                    Dim e As New Exception(String.Format("Restoration from {0} cancelled because pre-restoration steps could not be executed.", args.Snapshot.Name), exception)
                    Throw e
            End Try
    End Sub
    
  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);
    }
    
    Public Sub OnPostRestore(ByVal args As SPDatabaseSnapshotRestoreEvent)
            If args Is Nothing Then
                    Throw New ArgumentNullException("args")
            End If
    
            Me.Status = SPObjectStatus.Online
            Me.Update(True)
    End Sub
    

    Note

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

See Also

Tasks

How to: Create a Content Class That Can Be Backed Up and Restored

Reference

IBackupRestore

IDatabaseSnapshotRestore

SPDatabaseSnapshot.Restore()

SPDatabase