Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

date when the database was restored?

Posted on 2016-07-19
6
Medium Priority
?
79 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 23

Assisted Solution

by:Ioannis Paraskevopoulos
Ioannis Paraskevopoulos earned 1000 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 27

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 27

Accepted Solution

by:
Zberteoc earned 1000 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 27

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

618 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