Once upon a time there was a SharePoint consultant named Pepper. She wanted to learn about SQL Server Administration/DBA work. While she was a master SharePoint consultant at her office in Bethel she grew curious to learn SQL Server. She wanted to be independent when dealing with SQL server chores like creating maintaining database, taking backups, creating jobs and things alike. Being busy as she is at her current task on working with SharePoint she would sometimes squeeze in some time to poke around on SQL server and learn about it. Our coffee discussions would turn out to be SQL discussions and how SharePoint uses SQL as its backend.
Last week, I got a call from Pepper at noon timeframe … Now that does not happen and I was sure there was something fishy…
Pepper: Tony, is it normal for database to get in RESTORING state after backup?
I said in my usual tone of ‘romcasim’
Me: depends on what state you wanted it to be in
Pepper: I am in no mood to play ‘take-my-hint’ I need to get back to testing something please tell me what did I do wrong.
Me: OK OK.. hummmm tell me what all you did so I can help
Pepper: Let me ping you.
Here is what I learnt from her chat messages. Pepper wanted to test a few things which involved her taking database backup and transaction log backup. So she scrambled on the net and got the syntax quick without bothering to read much. She did her test runs or code and ran the backup.
BACKUP DATABASE [BKP] TO DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\full_backup.bak' WITH NOFORMAT, NOINIT, NAME = N'BKP-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10 GO
Then again some testing from SharePoint and now Ran below to take Tlog backup
BACKUP LOG [BKP] TO DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\tlog_backup.trn' WITH NO_TRUNCATE, NOFORMAT, NOINIT, NAME = N'BKP-TLog Backup', SKIP, NOREWIND, NOUNLOAD, NORECOVERY, STATS = 10 GO
Now this is where I figured the mistake the “NORECOVERY” clause. As per MSDN documentation
Backs up the tail of the log and leaves the database in the RESTORING state. NORECOVERY is useful when failing over to a secondary database or when saving the tail of the log before a RESTORE operation.
To perform a best-effort log backup that skips log truncation and then take the database into the RESTORING state atomically, use the NO_TRUNCATE and NORECOVERY options together.
After having explained what that clause does, the obvious question pops out.
Pepper: Tony, all that is nice to know. How do I get out of the RESTORING state?
I gave her the following command, which she happily executed and voila DB was back online.
RESTORE DATABASE BKP WITH RECOVERY
Pepper: I owe you a coffee. Starbucks @ 5?
I was thinking in my head – One more midday coffee is going to be over the Tail Log backup discussion. As expected, she asked questions and I decided to share them here.
Question #1: Can this happen from user interface also?
Answer #1: Yes. Here is the option in SQL Server Management Studio (SSMS)
Question #2: But why would someone do that? Why would someone take production database into restoring state?
Answer #2: These kind of log backups are called as tail log backups. Imagine a situation where DBA has configured log shipping. As a part of DR drill, application team wants to move production workload to secondary server. One DR drill is complete, the old primary should again take primary role. If we don’t want to reinitialize the log-shipping via full backup then here are the steps.
- Disable all log shipping jobs (on primary and secondary)
- Restore all pending transaction logs which are not yet applied on secondary with norecovery.
- Take tail log backup with norecovery. This would leave primary database in restoring state.
- Restore this tail log backups on secondary database using “with recovery” clause.
- This would bring secondary open for read/write activities and testing.
- Once testing completes, take a tail log back-up from current primary (secondary initially)
- Restore that backup with recovery on current secondary (primary initially)
- Enabled all log shipping jobs which were disabled in first step.
That was an eye-opener for Pepper and at least, she paid for my coffee.
Reference: Pinal Dave (https://blog.sqlauthority.com)