forked from SQLUndercover/UndercoverToolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsp_Snapshot.sql
153 lines (114 loc) · 4.65 KB
/
sp_Snapshot.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/******************************************************************
Author: David Fowler
Revision date: 15 August 2019
Version: 2
© 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.
******************************************************************/
USE master
GO
ALTER PROCEDURE sp_Snapshot
(@DatabaseList NVARCHAR(4000),
@ListOnly BIT = 0)
AS
BEGIN
SET NOCOUNT ON
IF OBJECT_ID('tempdb..#DatabaseList') IS NOT NULL
DROP TABLE #DatabaseList
CREATE TABLE #DatabaseList (name NVARCHAR(4000))
IF OBJECT_ID('tempdb..#DatabasesFinal') IS NOT NULL
DROP TABLE #DatabasesFinal
--set compatibility mode
DECLARE @compatibility BIT
--set compatibility to 1 if server version includes STRING_SPLIT
SELECT @compatibility = CASE
WHEN SERVERPROPERTY ('productversion') >= '13.0.4001.0' AND Compatibility_Level >= 130 THEN 1
ELSE 0
END
FROM sys.databases
WHERE name = DB_NAME()
--select the database list into a temp table so that we can work with it
IF @compatibility = 1 --if compatibility = 1 then use STRING_SPLIT otherwise use fn_SplitString
INSERT INTO #DatabaseList
SELECT value
FROM STRING_SPLIT(@DatabaseList,',')
ELSE
INSERT INTO #DatabaseList
SELECT StringElement AS name
FROM master..fn_SplitString(@DatabaseList,',')
--get list of databases, including those covered by any wildcards
SELECT QUOTENAME(name) AS name
INTO #DatabasesFinal
FROM sys.databases databases
WHERE EXISTS
(SELECT name
FROM #DatabaseList
WHERE databases.name LIKE #DatabaseList.name)
IF @ListOnly = 1 --if @listonly set then only print the affected databases
SELECT name
FROM #DatabasesFinal
ELSE
BEGIN
DECLARE @Databases VARCHAR(128)
------------------------------------------------------------------------------------------------------
--Loop through each database creating snapshots
DECLARE databases_curr CURSOR
FOR SELECT name
FROM #DatabasesFinal
OPEN databases_curr
FETCH NEXT FROM databases_curr
INTO @Databases
WHILE @@FETCH_STATUS = 0
BEGIN
--create snapshots
EXEC ('USE ' + @Databases +
'DECLARE @DatabaseName VARCHAR(128)
DECLARE @SnapshotName VARCHAR(128)
SET @DatabaseName = DB_NAME()
SET @SnapshotName = DB_NAME() + ''_snapshot''
--table variable to hold file list
DECLARE @DatabaseFiles TABLE (id INT identity(1,1),name VARCHAR(128), physical_name VARCHAR(400))
--populate table variable with file information
INSERT INTO @DatabaseFiles (name, physical_name)
SELECT name, physical_name
FROM sys.database_files
WHERE type != 1
--begin building snapshot script
DECLARE @SnapshotScript VARCHAR(1000)
SET @SnapshotScript = ''CREATE DATABASE '' + QUOTENAME(@SnapshotName) + '' ON ''
--loop through datafile table variable
DECLARE @LoopCounter INT = 0
DECLARE @FileCount INT
SELECT @FileCount = COUNT(*)
FROM @DatabaseFiles
WHILE @LoopCounter < @FileCount
BEGIN
SET @LoopCounter = @LoopCounter + 1
SELECT @SnapshotScript = @SnapshotScript + ''(NAME = '' + QUOTENAME(name) + '', FILENAME = '''''' + physical_name + ''.ss''''),''
FROM @DatabaseFiles
WHERE id = @LoopCounter
END
--loop will have added an unwanted comma at the end of the script, delete this comma
SET @SnapshotScript = LEFT(@SnapshotScript, LEN(@SnapshotScript) -1)
--add AS SNAPSHOT to script
SET @SnapshotScript = @SnapshotScript + '' AS SNAPSHOT OF ['' + @DatabaseName + '']''
--Generate the snapshot
PRINT ''Creating Snapshot for ' + @Databases + '''
EXEC (@SnapshotScript)')
FETCH NEXT FROM databases_curr
INTO @Databases
END
CLOSE databases_curr
DEALLOCATE databases_curr
END
END