Are you trying to restore a database in alwayson availability group?
This guide is for you.
Sometimes, we may need to restore the database that is a part of alwayson availability group in order to fulfil our business needs. However, restoring a database in the availability group is different than restoring a normal database. It is not possible to restore the Availability Group database directly like the normal database. You can come across the below error message.
The operation cannot be performed on database “DBNAME” because it is involved in a database mirroring session or an availability group. Some operations are not allowed on a database that is participating in a database mirroring session or in an availability group.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform database related errors.
In this context, we shall look into how to restore a database in alwayson availability group.
Here, you will learn how to restore the database in the Availability Group.
Here are the steps that we follow to restore the database.
First, we remove the identified database from the AOAG configuration.
Note: If you want to restore the primary database in the AOAG configuration then you should remove it from the primary replica. Because it will automatically remove this database from AOAG configuration on secondary replica as well. So there is no need to separately remove the secondary databases from the AOAG configuration.
Here is the T-SQL statement that we use to remove the primary database from the AOAG configuration:
ALTER AVAILABILITY GROUP Avail_Group REMOVE DATABASE DBName
–Change the name of Avail_Group to your Availability group.
–Change the database name from DBName to your primary database.
After removing the primary database from the availability group, it will disappear from the Availability Database folder showing under the Availability group name on both replicas.
Now, we shall restore the database by running the below T-SQL statements.
i. First, we restore the primary database with the help of the backup file:
RESTORE DATABASE DBName
FROM Disk= ‘F:\Backups\DBName_Full.bak’
WITH NORECOVERY, REPLACE;
ii. Next, we restore log backup till the time we want to recover the database:
RESTORE LOG DBName
FROM Disk= ‘F:\Backups\DBName_TLog.trn’
WITH NORECOVERY, REPLACE, STOPAT = ‘Dec 21, 2020 8:00 AM’;
iii. Finally, we bring the database Online:
RESTORE DATABASE DBName WITH RECOVERY;
After restoring the database and once it comes online, we will go ahead to add this database to the availability group again.
In order to re-add this database to the availability group, we first prepare the corresponding secondary database.
For that, we first need to take a full backup and immediate transaction log backup of the newly restored primary database.
Then we copy these files to a secondary replica and restore the corresponding secondary database in norecovery mode.
After that, we restore the corresponding secondary database with the help of above-copied files in norecovery mode with REPLACE option.
Now, we run the below T-SQL command to add this database to the existing availability group:
ALTER AVAILABILITY GROUP Avail_Group ADD DATABASE DBName;
GO
— Connect to the server instance that hosts the primary replica.
— Add an existing database to the availability group.
Finally, we ensure to configure the secondary database on the secondary replica. However, if we don’t want to add this database to the availability group then we can ignore this step and use the database for transactions.
Also, it is possible to use that database from a secondary replica for any purpose. And can bring it online by running the below command on the secondary replica.
–Run-on secondary replica in case you don’t want to configure this database in AOAG rather want to use it for transactions:
RESTORE DATABASE DBName WITH RECOVERY;
That's it! Now, we are done with the database restore in Alwayson Availability Group.
This article will guide you on steps to restore a #database in alwayson availability group.
If you receive an #error that the database is in use, try to set the user to single user mode: USE master; GO ALTER DATABASE Database_name SET SINGLE_USER WITH ROLLBACK IMMEDIATE; Then try the restore with #recovery command again. Also, make sure you are on the latest service pack or cumulative update.
Creating a #SQL #Server AlwaysOn Availability Group.
Open SQL Server Management Studio and connect to the SQL Server instance. Expand the AlwaysOn High Availability folder in the Object Explorer. Right-click on the Availability Groups folder and select the New Availability Group Wizard.
To Restore a Database in a SQL Server AlwaysOn Availability Group:
1. Make the secondary server from which the backups were taken the primary server.
2. Remove the database to be restored from the AlwaysOn Availability Group.
3. Restore the database.
4. Add the database back into the Always Availability Group.