Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 Determine if database needs to be backed up

Author  Topic 

TJTodd
Starting Member

10 Posts

Posted - 2010-07-27 : 14:42:25
I have been trying to determine whether or not a given database is recoverable (able to perform a full recovery on the DB). Specifically I want to ensure that the recovery model was not changed to "simple" since the last full backup.

I know that I can simply query the current Recovery Model and if it is currently set to "simple", I can easily make the determination that it's not recoverable. The main situation that I would like to detect is a database that
1. Has a FULL Backup taken
2. Has the recovery model set to Simple
3. Has the recovery model set to Full
At the moment the database recovery model was set to simple - we lose recoverability. But when the recovery model is then set back to Full - it is not apparent that we have a problem.

Question is - how can we detect (in a T-SQL script) such situations so that we could then perform the needed backup?

Thanks in advance,
Tom

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-27 : 14:53:14
There's code in my custom backup script that checks this: http://weblogs.sqlteam.com/tarad/archive/2009/12/29/Backup-SQL-Server-2005-and-2008-DatabasesAgain.aspx

The specific part you should look at is the code for @missingFull.

When it detects a broken tlog chain (such as truncating the tlog), it corrects it by running a full backup to start the chain.

Here's some of the code:

-- check for missing full backup or broken transaction log chain
IF @bkpType <> 'FULL'
BEGIN
SELECT @missingFull =
CASE
WHEN last_log_backup_lsn IS NULL THEN 1
ELSE 0
END
FROM master.sys.database_recovery_status
WHERE database_id = DB_ID(@dbName)

-- Database could be in SIMPLE recovery model, so above could show a broken
-- transaction log chain
IF @missingFull = 1
SELECT @missingFull =
CASE
WHEN backup_date IS NULL THEN 1
WHEN backup_date < restore_date THEN 1
WHEN backup_date < create_date THEN 1
ELSE 0
END
FROM
(
SELECT
create_date,
restore_date =
(
SELECT MAX(restore_date) AS restore_date
FROM msdb.dbo.restorehistory
WHERE destination_database_name = @dbName
),
backup_date =
(
SELECT MAX(backup_start_date) AS backup_date
FROM msdb.dbo.backupset
WHERE database_name = @dbName AND type = 'D' --full backup
)
FROM master.sys.databases
WHERE name = @dbName
) t
END


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-07-27 : 15:18:17
That's pretty cool Tara
Go to Top of Page

TJTodd
Starting Member

10 Posts

Posted - 2010-07-27 : 15:24:24
Thanks Tara!

I wasn't aware of the last_log_backup_lsn column in
sys.database_recovery_status.

I really appreciate the help

Best regards,
Tom
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-07-27 : 15:33:18
quote:
Originally posted by Kristen

That's pretty cool Tara



Mom, wife....super DBA..."look up in the sky...."

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-27 : 15:37:57
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -