Solved

date when the database was restored?

Posted on 2016-07-19
6
61 Views
Last Modified: 2016-07-20
other than
msdb..restorehistory
is it possible to know when was the current database restored? (from the database itself, if possible..)
0
Comment
Question by:25112
6 Comments
 
LVL 23

Assisted Solution

by:Ioannis Paraskevopoulos
Ioannis Paraskevopoulos earned 250 total points
ID: 41720245
Hi,

You may try this:

WITH LastRestores AS
(
SELECT
    DatabaseName = [d].[name] ,
    [d].[create_date] ,
    [d].[compatibility_level] ,
    [d].[collation_name] ,
    r.*,
    RowNum = ROW_NUMBER() OVER (PARTITION BY d.Name ORDER BY r.[restore_date] DESC)
FROM master.sys.databases d
LEFT OUTER JOIN msdb.dbo.[restorehistory] r ON r.[destination_database_name] = d.Name
)
SELECT *
FROM [LastRestores]
WHERE [RowNum] = 1

Open in new window


I will not claim the answer as my own. I found it through a Google Search and it is hosted here.

By the way, this does not seem to work on AzureSQL.

Giannis
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41721127
Try this:
SELECT 
    [rs].[destination_database_name], 
    [rs].[restore_date], 
    [bs].[backup_start_date], 
    [bs].[backup_finish_date], 
    [bs].[database_name] as [source_database_name], 
    [bmf].[physical_device_name] as [backup_file_used_for_restore]
FROM 
    msdb..restorehistory rs
    INNER JOIN msdb..backupset bs
	   ON [rs].[backup_set_id] = [bs].[backup_set_id]
    INNER JOIN msdb..backupmediafamily bmf 
	   ON [bs].[media_set_id] = [bmf].[media_set_id] 
WHERE
    [rs].[destination_database_name]='<db_name_here>' -- if ignored all database restore info will be returned
ORDER BY 
    [rs].[restore_date] DESC

Open in new window

0
 
LVL 5

Author Comment

by:25112
ID: 41721361
thank you experts..
both solution refers to msdb database..

the confirmation I need is that this information is not available on the user database, at all?
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 26

Accepted Solution

by:
Zberteoc earned 250 total points
ID: 41721421
I already stated that the query will return the restore history info for ALL the databases if there is no filter to it. The reason you see the msdb database is that all the backup and restore info for ALL the databases on a SQL instance is stored in tables that reside in the msdb database. That is by design.  But be aware that the restore and backup history is purged periodically so you may miss it for some databases. If you have multiple entries just take the last one in chronological order. In my query I ordered by [restore_date] DESC(ending) so the last restore would be the first backup row for any database. Actually I would restore by database name first:
SELECT 
    [rs].[destination_database_name], 
    [rs].[restore_date], 
    [bs].[backup_start_date], 
    [bs].[backup_finish_date], 
    [bs].[database_name] as [source_database_name], 
    [bmf].[physical_device_name] as [backup_file_used_for_restore]
FROM 
    msdb..restorehistory rs
    INNER JOIN msdb..backupset bs
	   ON [rs].[backup_set_id] = [bs].[backup_set_id]
    INNER JOIN msdb..backupmediafamily bmf 
	   ON [bs].[media_set_id] = [bmf].[media_set_id] 
--WHERE [rs].[destination_database_name]='<db_name_here>' -- if ignored all database restore info will be returned
ORDER BY
    [rs].[destination_database_name], 
    [rs].[restore_date] DESC

Open in new window

This should return the info for ALL database restored that exist in the history.
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41721535
One more thing. I just realize it is possible that the restore process has nothing to do with the backup process if the restore was done from an outside backup file or if the database has no backup info. That means the query should use left join with the backup set but add the [msdb].[dbo].[restorefile] table with INNER JOIN to have the accurate restore info:
SELECT --rs.*,
     [destination_database_name]
    ,[restore_date]
    ,rs.[user_name]
    ,[restore_type]
    ,[replace]
    ,[recovery]
    ,[file_number]
    ,[destination_phys_drive]
    ,[destination_phys_name]
    ,[bs].[backup_start_date]
    ,[bs].[backup_finish_date]
    ,[bs].[database_name] as [source_database_name]
    ,[bmf].[physical_device_name] as [backup_file_used_for_restore]
FROM 
    msdb..restorehistory rs
    inner join [msdb].[dbo].[restorefile] rf
	   on rf.restore_history_id=rs.restore_history_id    
    LEFT JOIN msdb..backupset bs
	   ON [rs].[backup_set_id] = [bs].[backup_set_id]
    LEFT JOIN msdb..backupmediafamily bmf 
	   ON [bs].[media_set_id] = [bmf].[media_set_id] 
--WHERE [rs].[destination_database_name]='<db_name_here>' -- if ignored all database restore info will be returned
ORDER BY
    [rs].[destination_database_name], 
    [rs].[restore_date] DESC

Open in new window

0
 
LVL 25

Expert Comment

by:Mohammed Khawaja
ID: 41721599
That is a nice query.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question