-
Notifications
You must be signed in to change notification settings - Fork 41
/
SnapshotRestoreHistory.sql
90 lines (63 loc) · 2.29 KB
/
SnapshotRestoreHistory.sql
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/******************************************************************
Author: David Fowler
Revision date: 14/08/2023
Version: 1
Audit restores from snapshot
© www.sqlundercover.com
This script is for personal, educational, and internal
corporate purposes, provided that this header is preserved. Redistribution or sale
of this script,in whole or in part, is prohibited without the author's express
written consent.
The software is provided "as is", without warranty of any kind, express or
implied, including but not limited to the warranties of merchantability,
fitness for a particular purpose and noninfringement. in no event shall the
authors or copyright holders be liable for any claim, damages or other
liability, whether in an action of contract, tort or otherwise, arising from,
out of or in connection with the software or the use or other dealings in the
software.
******************************************************************/
CREATE TABLE SnapshotRestoreHistory
(LogDate DATETIME,
ProcessInfo VARCHAR(10),
[Text] VARCHAR(500) NOT NULL)
GO
CREATE PROC PopulateSnapshotRestoreHistory
AS
BEGIN
--create temp holding table for log entries
IF OBJECT_ID('tempdb.dbo.#SnapshotLogs') IS NOT NULL
DROP TABLE #SnapshotLogs
CREATE TABLE #SnapshotLogs
(LogDate DATETIME,
ProcessInfo VARCHAR(10),
[Text] VARCHAR(500) NOT NULL)
--temp table to be userd by Log sursor
IF OBJECT_ID('tempdb.dbo.#LogFiles') IS NOT NULL
DROP TABLE #LogFiles
CREATE TABLE #LogFiles (LogNumber INT, StartDate DATETIME, SizeInBytes INT)
INSERT INTO #LogFiles
EXEC xp_enumerrorlogs
DECLARE @LogNumber INT
DECLARE LogCur CURSOR LOCAL FAST_FORWARD FOR
SELECT LogNumber
FROM #LogFiles
OPEN LogCur
FETCH NEXT FROM LogCur INTO @LogNumber
WHILE @@FETCH_STATUS = 0
BEGIN
--get entries from log file
INSERT INTO #SnapshotLogs
EXEC [sys].[sp_readerrorlog] @LogNumber,1,'Reverting database'
--merge log entries in to history table
MERGE SnapshotRestoreHistory AS Target
USING #SnapshotLogs
ON Target.LogDate = #SnapshotLogs.LogDate
AND Target.[Text] = #SnapshotLogs.[Text]
WHEN NOT MATCHED BY Target THEN
INSERT (LogDate,ProcessInfo,[Text])
VALUES (#SnapshotLogs.LogDate,#SnapshotLogs.ProcessInfo,#SnapshotLogs.[Text]);
FETCH NEXT FROM LogCur INTO @LogNumber
END
CLOSE LogCur
DEALLOCATE LogCur
END