-
Notifications
You must be signed in to change notification settings - Fork 41
/
sp_Snapshot.sql
162 lines (128 loc) · 4.76 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
154
155
156
157
158
159
160
161
162
/******************************************************************
Author: David Fowler
Revision date: 26 January 2024
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.
******************************************************************/
CREATE OR ALTER PROC sp_snapshot (
@DatabaseList NVARCHAR(4000)
,@Suffix NVARCHAR(100) = 'snapshot'
,@FilePath NVARCHAR(255) = ''
,@Timestamp BIT = 0
,@DateFormat INT = 126
,@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
IF @Timestamp = 1
BEGIN
SET @Suffix += '_' + CONVERT(VARCHAR, GETDATE(), @DateFormat)
END
--we need to strip out certain characters from the file name that tend to occur in dates6
SET @Suffix = REPLACE(@Suffix, ':', '')
SET @Suffix = REPLACE(@Suffix, '\', '')
SET @Suffix = REPLACE(@Suffix, '/', '')
--select the database list into a temp table so that we can work with it
INSERT INTO #DatabaseList
SELECT value
FROM STRING_SPLIT(@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
DECLARE @SQL VARCHAR(MAX)
SET @SQL = 'USE ' + @Databases + 'DECLARE @DatabaseName VARCHAR(128)
DECLARE @SnapshotName VARCHAR(128)
SET @DatabaseName = DB_NAME()
SET @SnapshotName = DB_NAME() + ''_' + @Suffix +
'''
--table variable to hold file list
DECLARE @DatabaseFiles TABLE (id INT identity(1,1),name VARCHAR(128), physical_name VARCHAR(400), ss_file_name VARCHAR(4000))
--populate table variable with file information
INSERT INTO @DatabaseFiles (name, physical_name, ss_file_name)
SELECT name, physical_name, REVERSE(SUBSTRING(REVERSE(Physical_name),CHARINDEX(''\'', REVERSE(physical_name),0),LEN(physical_name)))
FROM sys.master_files
WHERE type != 1 AND database_id = DB_ID()
--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
IF ''' + @FilePath +
''' = ''''
BEGIN
SELECT @SnapshotScript = @SnapshotScript + ''(NAME = '' + QUOTENAME(name) + '', FILENAME = '''''' + ss_file_name + @SnapshotName + ''.ss''''),''
FROM @DatabaseFiles
WHERE id = @LoopCounter
END
ELSE
BEGIN
SELECT @SnapshotScript = @SnapshotScript + ''(NAME = '' + QUOTENAME(name) + '', FILENAME = ''''' + @FilePath + ''' + @SnapshotName + ''.ss''''),''
FROM @DatabaseFiles
WHERE id = @LoopCounter
END
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 + '''
--PRINT @SnapshotScript
EXEC (@SnapshotScript)'
EXEC (@SQL)
FETCH NEXT
FROM databases_curr
INTO @Databases
END
CLOSE databases_curr
DEALLOCATE databases_curr
END
END