views:

1065

answers:

4

Folks,

Assume you receive a disconnected backup of a SQL Server database (2005 or 2008) and you restore that to your SQL Server instance.

Is there a way, is there a system catalog or something, to find out when the last write operation occured on that particular database? I'd like to be able to find out what day a particular database backup was from - unfortunately, that's not really being recorded explicitly anywhere, and checking all dozens of data table for the highest date/time stamp isn't really an option either....

Any ideas? Sure - I can look at the date/time stamp of the *.bak file - but can I find out more precisely from within SQL Server (Management Studio) ??

Thanks! Marc

A: 

Hi Marc,

as far as I know in the master database there exists a Log-table where every write is stored with detailed information. BUT I'm unsure if you need to enable the Log-mechanism - so that the default is not to log and you have to enable it. In Oracle for example it is the way around there exists a system-database table Log that you can query.

If that is not the case - you could still write yourself a trigger and apply that on every table/column needed and do the logging yourself.

Gambrinus
the poster is asking for something that has already occurred; your suggestion of a trigger won't work for that...
Mitch Wheat
exactly - trigger won't work. I'm talking about restoring a backup file form Server A on my local machine B, and then diagnosing when the last write operation on that database occured (without having access to the server it resided on)
marc_s
+1  A: 

If you have access to the SQL Server instance where the backup was originally run, you should be able to query msdb:

SELECT backup_set_id, backup_start_date, backup_finish_date
FROM  msdb.dbo.backupset  
WHERE database_name = 'MyDBname' AND type = 'D'

There are several table relating to backup sets:

  • backupfile -- contains one row for each data file or log file backed up
  • backupmediafamily -- contains one row for each media family
  • backupmediaset -- contains one row for each backup media set
  • backupset -- contains one row for each backup set

By querying these tables you can determine when the last backups occurred, what type of backups occurred and where the files were written to.

Mitch Wheat
Well, since I received a BAK file on e.g. disk, USB stick, or via FTP, I unfortunately do *not* have access to the source server :-(
marc_s
+1  A: 

You can try RESTORE HEADERONLY on your backup file, as described here

that should give you the information you're looking for.

Ralph Shillington
+1  A: 

A bit late, but should be what you want.

Each write to the database is an entry in the log file. Which has an LSN. This must be stored in the backup for log restores at least.

So, how to match LSN to a datetime?

SELECT TOP 5 [End Time] AS BringFirst, *
FROM ::fn_dblog (NULL, NULL)
WHERE [End Time] IS NOT NULL
ORDER BY BringFirst DESC

I've never used this before (just had a play for this answer). Some writes are very likely part of the backup itself, but you should be able to distinguish them with some poking around.

gbn
interesting - thanks! However, it seems now that restoring the database also counts as a "write" operation, e.g. I get the date and time of when I restored the database, and unfortunately, that "masks" the real last write time of my users.....
marc_s
Apply a date filter (second parameter? Or WHERE) to before you restored it: this should remove your local writes.
gbn