-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestoreDatabase.sql
More file actions
278 lines (250 loc) · 9.32 KB
/
RestoreDatabase.sql
File metadata and controls
278 lines (250 loc) · 9.32 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
USE MASTER;
IF (exists (SELECT name from master.dbo.sysdatabases
WHERE ('[' + name + ']' = '$(DatabaseName)'
or name = '$(DatabaseName)')))
BEGIN
ALTER DATABASE [$(DatabaseName)] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
END
DECLARE
@BackupFile nvarchar(260) = '$(BakFile)',
@NewDatabaseName sysname = '$(DatabaseName)',
@DataFolder nvarchar(260),
@LogFolder nvarchar(260),
@LogicalName nvarchar(128),
@PhysicalName nvarchar(260),
@PhysicalFolderName nvarchar(260),
@PhysicalFileName nvarchar(260),
@NewPhysicalName nvarchar(260),
@NewLogicalName nvarchar(128),
@OldDatabaseName nvarchar(128),
@RestoreStatement nvarchar(MAX),
@Command nvarchar(MAX),
@ReturnCode int,
@FileType char(1),
@ServerName nvarchar(128),
@BackupFinishDate datetime,
@Message nvarchar(4000),
@ChangeLogicalNamesSql nvarchar(MAX),
@Error int,
@ProductVersion nvarchar(128),
@ProductVersionNumber tinyint,
@HeaderSql nvarchar(MAX),
@FileListSql nvarchar(MAX)
/*
The reason for all the extra logic in this file is to compensate for varying default sql file paths and for varying versions of sql.
We get the file names using HEADERONLY and FILELISTONLY and then build new paths and RESTORE using MOVE to save them to the new locations.
*/
--get default paths (Only works in SQL 2012 and onward.)
select @DataFolder = convert(varchar(260), serverproperty('InstanceDefaultDataPath')),
@LogFolder = convert(varchar(260), serverproperty('InstanceDefaultLogPath'))
SET NOCOUNT ON;
--add trailing backslash to folder names if not already specified
IF LEFT(REVERSE(@DataFolder), 1) <> '\' SET @DataFolder = @DataFolder + '\';
IF LEFT(REVERSE(@LogFolder), 1) <> '\' SET @LogFolder = @LogFolder + '\';
--get SQL version
SET @ProductVersion = CONVERT(NVARCHAR(128),SERVERPROPERTY('ProductVersion'))
SET @ProductVersionNumber = SUBSTRING(@ProductVersion, 1, (CHARINDEX('.', @ProductVersion) - 1));
IF object_id('dbo.tblBakHeader') IS NOT NULL DROP TABLE dbo.tblBakHeader
IF object_id('dbo.tblBakFileList') IS NOT NULL DROP TABLE dbo.tblBakFileList
--Common return values for HeaderOnly
SET @HeaderSql = 'create table dbo.tblBakHeader
(BackupName nvarchar(128),
BackupDescription nvarchar(255),
BackupType smallint,
ExpirationDate datetime,
Compressed tinyint,
Position smallint,
DeviceType tinyint,
UserName nvarchar(128),
ServerName nvarchar(128),
DatabaseName nvarchar(128),
DatabaseVersion int,
DatabaseCreationDate datetime,
BackupSize numeric(20,0),
FirstLSN numeric(25,0),
LastLSN numeric(25,0),
CheckpointLSN numeric(25,0),
DatabaseBackupLSN numeric(25,0),
BackupStartDate datetime,
BackupFinishDate datetime,
SortOrder smallint,
CodePage smallint,
UnicodeLocaleId int,
UnicodeComparisonStyle int,
CompatibilityLevel tinyint,
SoftwareVendorId int,
SoftwareVersionMajor int,
SoftwareVersionMinor int,
SoftwareVersionBuild int,
MachineName nvarchar(128),
Flags int,
BindingID uniqueidentifier,
RecoveryForkID uniqueidentifier,
Collation nvarchar(128),
FamilyGUID uniqueidentifier,
HasBulkLoggedData bit,
IsSnapshot bit,
IsReadOnly bit,
IsSingleUser bit,
HasBackupChecksums bit,
IsDamaged bit,
BeginsLogChain bit,
HasIncompleteMetaData bit,
IsForceOffline bit,
IsCopyOnly bit,
FirstRecoveryForkID uniqueidentifier,
ForkPointLSN decimal(25, 0) NULL,
RecoveryModel nvarchar(60),
DifferentialBaseLSN decimal(25, 0) NULL,
DifferentialBaseGUID uniqueidentifier,
BackupTypeDescription nvarchar(60),
BackupSetGUID uniqueidentifier NULL,
CompressedBackupSize binary(8),
Containment tinyint NOT NULL'
--Common return values for FileListOnly
SET @FileListSql = 'create table dbo.tblBakFileList
(
LogicalName nvarchar(128),
PhysicalName nvarchar(260),
Type char(1),
FileGroupName nvarchar(120),
Size numeric(20, 0),
MaxSize numeric(20, 0),
FileID bigint,
CreateLSN numeric(25,0),
DropLSN numeric(25,0) NULL,
UniqueID uniqueidentifier,
ReadOnlyLSN numeric(25,0) NULL,
ReadWriteLSN numeric(25,0),
BackupSizeInBytes bigint,
SourceBlockSize int,
FileGroupID int,
LogGroupGUID uniqueidentifier,
DifferentialBaseLSN numeric(25,0) NULL,
DifferentialBaseGUID uniqueidentifier,
IsReadOnly bit,
IsPresent bit,
TDEThumbprint varbinary(32)'
-- Values specific to SQL Sever 2014 and 2016
IF @ProductVersionNumber in(12, 13)
SET @HeaderSql = @HeaderSql +'
,KeyAlgorithm nvarchar(32)
,EncryptorThumbprint varbinary(20)
,EncryptorType nvarchar(32)'
--Values specific to SQL Server 2016
IF @ProductVersionNumber in(13)
SET @FileListSql = @FileListSql + '
,SnapshotURL nvarchar(360)'
--All versions - close statement
SET @HeaderSql = @HeaderSql + ');'
SET @FileListSql = @FileListSql + ');'
--Create Header and FileList tables
EXEC(@HeaderSql)
EXEC(@FileListSql)
SET @Error = 0;
-- get backup header info and display
SET @RestoreStatement = N'RESTORE HEADERONLY
FROM DISK=N''' + @BackupFile + ''' WITH FILE=1';
INSERT INTO dbo.tblBakHeader
EXEC('RESTORE HEADERONLY FROM DISK=N''' + @BackupFile + ''' WITH FILE = 1');
SET @Error = @@ERROR;
IF @Error <> 0 GOTO Done;
IF NOT EXISTS(SELECT * FROM dbo.tblBakHeader) GOTO Done;
SELECT
@OldDatabaseName = DatabaseName,
@ServerName = ServerName,
@BackupFinishDate = BackupFinishDate
FROM dbo.tblBakHeader;
IF @NewDatabaseName IS NULL SET @NewDatabaseName = @OldDatabaseName;
SET @Message = N'--Backup source: ServerName=%s, DatabaseName=%s, BackupFinishDate=' +
CONVERT(nvarchar(23), @BackupFinishDate, 121);
RAISERROR(@Message, 0, 1, @ServerName, @OldDatabaseName) WITH NOWAIT;
-- get filelist info
SET @RestoreStatement = N'RESTORE FILELISTONLY
FROM DISK=N''' + @BackupFile + ''' WITH FILE= 1';
INSERT INTO dbo.tblBakFileList
EXEC(@RestoreStatement);
SET @Error = @@ERROR;
IF @Error <> 0 GOTO Done;
IF NOT EXISTS(SELECT * FROM dbo.tblBakFileList) GOTO Done;
-- generate RESTORE DATABASE statement and ALTER DATABASE statements
SET @ChangeLogicalNamesSql = '';
SET @RestoreStatement =
N'RESTORE DATABASE ' +
QUOTENAME(@NewDatabaseName) +
N'
FROM DISK=N''' +
@BackupFile + '''' +
N'
WITH REPLACE,
FILE=1'
DECLARE FileList CURSOR LOCAL STATIC READ_ONLY FOR
SELECT
TYPE AS FileType,
LogicalName,
--extract folder name from full path
LEFT(PhysicalName,
LEN(LTRIM(RTRIM(PhysicalName))) -
CHARINDEX('\',
REVERSE(LTRIM(RTRIM(PhysicalName)))) + 1)
AS PhysicalFolderName,
--extract file name from full path
LTRIM(RTRIM(RIGHT(PhysicalName,
CHARINDEX('\',
REVERSE(PhysicalName)) - 1))) AS PhysicalFileName
FROM dbo.tblBakFileList;
OPEN FileList;
WHILE 1 = 1
BEGIN
FETCH NEXT FROM FileList INTO
@FileType, @LogicalName, @PhysicalFolderName, @PhysicalFileName;
IF @@FETCH_STATUS = -1 BREAK;
-- build new physical name
SET @NewPhysicalName =
CASE @FileType
WHEN 'D' THEN --database
@DataFolder + @NewDatabaseName + RIGHT(@PhysicalFileName, LEN(@PhysicalFileName) - LEN(@OldDatabaseName)) --gets suffix
WHEN 'L' THEN --logs
@LogFolder + @NewDatabaseName + RIGHT(@PhysicalFileName, LEN(@PhysicalFileName) - LEN(@OldDatabaseName)) --gets suffix
END;
-- build new logical name
SET @NewLogicalName = @NewDatabaseName + RIGHT(@LogicalName, LEN(@LogicalName) - LEN(@OldDatabaseName)) --gets suffix
-- generate ALTER DATABASE...MODIFY FILE statement if logical file name is different
IF @NewLogicalName <> @LogicalName
SET @ChangeLogicalNamesSql = @ChangeLogicalNamesSql + N'ALTER DATABASE ' + QUOTENAME(@NewDatabaseName) + N'
MODIFY FILE (NAME=''' + @LogicalName + N''', NEWNAME=''' + @NewLogicalName + N''');
'
-- add MOVE option as needed if folder and/or file names are changed
IF @PhysicalFolderName + @PhysicalFileName <> @NewPhysicalName
BEGIN
SET @RestoreStatement = @RestoreStatement +
N',
MOVE ''' +
@LogicalName +
N''' TO ''' +
@NewPhysicalName +
N'''';
END;
END;
CLOSE FileList;
DEALLOCATE FileList;
--execute RESTORE statement
RAISERROR(N'Executing:
%s', 0, 1, @RestoreStatement) WITH NOWAIT
EXEC (@RestoreStatement);
SET @Error = @@ERROR;
IF @Error <> 0 GOTO Done;
--execute ALTER DATABASE statement(s)
IF @ChangeLogicalNamesSql <> ''
BEGIN
RAISERROR(N'Executing:
%s', 0, 1, @ChangeLogicalNamesSql) WITH NOWAIT
EXEC (@ChangeLogicalNamesSql);
SET @Error = @@ERROR;
IF @Error <> 0 GOTO Done;
END
Done:
GO
ALTER DATABASE [$(DatabaseName)] SET MULTI_USER, ENABLE_BROKER WITH NO_WAIT;
IF object_id('dbo.tblBakHeader') IS NOT NULL DROP TABLE dbo.tblBakHeader
IF object_id('dbo.tblBakFileList') IS NOT NULL DROP TABLE dbo.tblBakFileList