-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathListLastDBRestores.sql
More file actions
67 lines (61 loc) · 3.35 KB
/
ListLastDBRestores.sql
File metadata and controls
67 lines (61 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*******************************************************************************************************************
================================================================================
DESCRIPTION:
--------------------------------------------------------------------------------
List the database restore history captured within msdb
There are two variables, @dbname and @days
(@dbname) = the name of the database you are searching for and would need to be enclosed in single quotation marks.
NULL = all databases
(@days) = negative integer (i.e., -7) which represents how many days previously you want to search.
NULL = default to searching for only the previous thirty days
HISTORY:
--------------------------------------------------------------------------------
Date: Developer: Description:
--------------------------------------------------------------------------------
* * Created
--------------------------------------------------------------------------------
NOTES:
--------------------------------------------------------------------------------
THIS SCRIPT/CODE ARE PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED.
LIMITED TESTING HAS BEEN PERFORMED ON THIS SCRIPT/CODE AND THEREFORE THE AUTHOR DOES NOT WARRANT
THAT ANY SCRIPT/CODE IS BUG OR ERROR-FREE. IT IS EXPECTED THAT ANY SCRIPT/CODE UNDERGO YOUR OWN
TESTING AND/OR VALIDATION BEFORE USAGE ON ANY CRITICAL SQL SERVER PLATFORM.
THIS SCRIPT MAY BE A COLLECTION OF MY OWN CODE COLLATED OVER MANY YEARS, OR OTHER CODE I HAVE
LOCATED ON THE WEB WITH AN UNKNOWN ORIGIN. WHERE CODE HAS BEEN IDENTIFIED IT WILL BE CITED.
================================================================================
*******************************************************************************************************************/
DECLARE
@dbname SYSNAME,
@days INT;
SET @dbname = NULL; --substitute for whatever database name you want
SET @days = -30; --previous number of days, script will default to 30
SELECT [rsh].[destination_database_name] AS [database],
[rsh].user_name AS [restored by],
CASE
WHEN [rsh].[restore_type] = 'D'
THEN 'Database'
WHEN [rsh].[restore_type] = 'F'
THEN 'File'
WHEN [rsh].[restore_type] = 'G'
THEN 'Filegroup'
WHEN [rsh].[restore_type] = 'I'
THEN 'Differential'
WHEN [rsh].[restore_type] = 'L'
THEN 'Log'
WHEN [rsh].[restore_type] = 'V'
THEN 'Verifyonly'
WHEN [rsh].[restore_type] = 'R'
THEN 'Revert'
ELSE [rsh].[restore_type]
END AS [restore type],
[rsh].[restore_date] AS [restore started],
[bmf].[physical_device_name] AS [restored from],
[rf].[destination_phys_name] AS [restored to]
FROM [msdb].[dbo].[restorehistory] AS [rsh]
INNER JOIN [msdb].[dbo].[backupset] AS [bs] ON [rsh].[backup_set_id] = [bs].[backup_set_id]
INNER JOIN [msdb].[dbo].[restorefile] AS [rf] ON [rsh].[restore_history_id] = [rf].[restore_history_id]
INNER JOIN [msdb].[dbo].[backupmediafamily] AS [bmf] ON [bmf].[media_set_id] = [bs].[media_set_id]
WHERE [rsh].[restore_date] >= DATEADD([dd], ISNULL(@days, -30), GETDATE()) --want to search for previous days
AND [destination_database_name] = ISNULL(@dbname, [destination_database_name]) --if no dbname, then return all
ORDER BY [rsh].[restore_history_id] DESC;
GO