Restoring a VSS snapshot to an AlwaysOn Availability Group using SMO RRS feed

  • Question

  • I'm writing a VSS requestor and I'm able to take snapshots of databases that are in the primary replica of an AlwaysOn Availability Group; however, I'm having trouble restoring them.  I understand to restore a database to an Availability Group you need to remove the database from the Availability Group, do the restore, then add the database back to the Availability Group.  I'd like to do this programatically in my VSS requestor using SMO.

    I have successfully written the code that removes the database from the Availability Group and then does the VSS restore.  But I can't figure out the code to add the database back to the Availability Group it was in.  The code I have tried looks like:

    server = new Server(ServerName);
    AvailabilityGroup ag = server.AvailabilityGroups[AvailabilityGroupName];
    AvailabilityDatabase dbToJoin = new AvailabilityDatabase(ag, DatabaseName);

    However, this results in the following exception:

    Microsoft.SqlServer.Management.Smo.InvalidSmoOperationException: You cannot execute this operation since the object has not been created.

    This is from code that I found here - http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.availabilitydatabase.joinavailaiblitygroup(v=sql.110).aspx, but updated with corrections in RC0.

    What is the correct SMO for adding a database back to an AvailabilityGroup?

    I'm also curious as to what happens to the secondary replicas as this time?  Do secondary replicas automatically get re-connected and synchronization re-start after the restore?



    Friday, January 20, 2012 9:07 PM

All replies

  • After some experimentation I've been able to get my code to work on the primary node.  The correct thing to do is to call the Create() method on the AvailabilityDatabase object not JoinAvailabilityGroup().

    I'm still having problems with the replica though.  I have one secondary replica that is left in the state of Restoring.  If I try to manually join the replica back to the AvailabilityGroup on the secondary I just get an error saying "The remote copy of database "xxx" is not recovered far enough to enable databse mirroring or to join it to the availability group."

    What's needed to get the replicas back into the AvailabilityGroup and synchronizing again?

    Tuesday, January 24, 2012 10:39 PM
  • have you done anything in your primary replica? if so, please do a TLog backup and restore that in your secondary db then try to rejoin db.
    Friday, January 27, 2012 11:42 PM