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.
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 taken2. Has the recovery model set to Simple3. Has the recovery model set to FullAt 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.aspxThe 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 KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-07-27 : 15:18:17
|
That's pretty cool Tara |
 |
|
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 insys.database_recovery_status. I really appreciate the helpBest regards,Tom |
 |
|
X002548
Not Just a Number
15586 Posts |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|
|