/
ch27.sql
417 lines (360 loc) · 9.18 KB
/
ch27.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* 27-1. Restoring a Database from a Full Backup */
--create a backup
USE master;
GO
Declare @BackupDate Char(8) = Convert(Varchar,GetDate(),112)
,@BackupPath Varchar(50);
Set @BackupPath= 'C:\Apress\TestDB_'+ @BackupDate + '.BAK';
BACKUP DATABASE TestDB
TO DISK = @BackupPath;
GO
-- Time passes, we make another backup to the same device
USE master;
GO
Declare @BackupDate Char(8) = Convert(Varchar,GetDate(),112)
,@BackupPath Varchar(50);
Set @BackupPath= 'C:\Apress\TestDB_'+ @BackupDate + '.BAK';
BACKUP DATABASE TestDB
TO DISK = @BackupPath;
GO
--restore
Declare @DeviceName Varchar(50);
Select @DeviceName = b.physical_device_name
From msdb.dbo.backupset a
INNER JOIN msdb.dbo.backupmediafamily b
ON a.media_set_id = b.media_set_id
Where a.database_name = 'TestDB'
And a.type = 'D'
And Convert(Varchar,a.backup_start_date,112) = Convert(Varchar,GetDate(),112);
RESTORE DATABASE TestDB
FROM DISK = @DeviceName
WITH FILE = 2, REPLACE;
GO
--example two
Declare @DeviceName Varchar(50);
Select @DeviceName = b.physical_device_name
From msdb.dbo.backupset a
INNER JOIN msdb.dbo.backupmediafamily b
ON a.media_set_id = b.media_set_id
Where a.database_name = 'TestDB'
And a.type = 'D'
And Convert(Varchar,a.backup_start_date,112) = Convert(Varchar,GetDate(),112);
RESTORE DATABASE TrainingDB
FROM DISK = @DeviceName
WITH FILE = 2,
MOVE 'TestDB' TO 'C:\Apress\TrainingDB.mdf',
MOVE 'TestDB_log' TO 'C:\Apress\TrainingDB_log.LDF';
GO
--example three
USE master;
GO
/* The path for each file should be changed to a path matching one
That exists on your system. */
BACKUP DATABASE TestDB
TO DISK = 'C:\Apress\Recipes\TestDB_Stripe1.bak'
, DISK = 'D:\Apress\Recipes\TestDB_Stripe2.bak'
, DISK = 'E:\Apress\Recipes\TestDB_Stripe3.bak'
WITH NOFORMAT, NOINIT,
NAME = N'AdventureWorks2012-Stripe Database Backup',
SKIP, STATS = 20;
GO
USE master;
GO
/* You should use the same file path for each file as specified
in the backup statement. */
RESTORE DATABASE TestDB
FROM DISK = 'C:\Apress\Recipes\TestDB_Stripe1.bak'
, DISK = 'D:\Apress\Recipes\TestDB_Stripe2.bak'
, DISK = 'E:\Apress\Recipes\TestDB_Stripe3.bak'
WITH FILE = 1, REPLACE;
GO
/* 27-2. Restoring a Database from a Transaction Log Backup */
USE master;
GO
IF NOT EXISTS (SELECT name FROM sys.databases
WHERE name = 'TrainingDB')
BEGIN
CREATE DATABASE TrainingDB;
END
GO
-- Add a table and some data to it
USE TrainingDB
GO
SELECT *
INTO dbo.SalesOrderDetail
FROM AdventureWorks2012.Sales.SalesOrderDetail;
GO
--full backup and t-log backups
USE master;
GO
Declare @BackupDate Char(8) = Convert(Varchar,GetDate(),112)
,@BackupPath Varchar(50);
Set @BackupPath= 'C:\Apress\TrainingDB_'+ @BackupDate + '.BAK';
BACKUP DATABASE TrainingDB
TO DISK = @BackupPath;
GO
BACKUP LOG TrainingDB
TO DISK = 'C:\Apress\TrainingDB_20120430_8AM.trn';
GO
-- Two hours pass, another transaction log backup is made
BACKUP LOG TrainingDB
TO DISK = 'C:\Apress\TrainingDB_20120430_10AM.trn';
GO
--kick out connections
USE master;
GO
-- Kicking out all other connections
ALTER DATABASE TrainingDB
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
--perform the restore
USE master;
GO
Declare @DeviceName Varchar(50);
Select @DeviceName = b.physical_device_name
From msdb.dbo.backupset a
INNER JOIN msdb.dbo.backupmediafamily b
ON a.media_set_id = b.media_set_id
Where a.database_name = 'TrainingDB'
And a.type = 'D'
And Convert(Varchar,a.backup_start_date,112) = Convert(Varchar,GetDate(),112);
RESTORE DATABASE TrainingDB
FROM DISK = @DeviceName
WITH NORECOVERY, REPLACE;
RESTORE LOG TrainingDB
FROM DISK = 'C:\Apress\ TrainingDB_20120430_8AM.trn'
WITH NORECOVERY, REPLACE
RESTORE LOG TrainingDB
FROM DISK = 'C:\Apress\ TrainingDB_20120430_10AM.trn'
WITH RECOVERY, REPLACE
--example two
USE master;
GO
BACKUP DATABASE TrainingDB
TO DISK = 'C:\Apress\TrainingDB_StopAt.bak';
GO
--delete some rows
USE TrainingDB;
GO
DELETE dbo.SalesOrderDetail
WHERE ProductID = 776;
GO
SELECT GETDATE();
GO
--backup log
BACKUP LOG TrainingDB
TO DISK = 'C:\Apress\TrainingDB_20120430_2022.trn';
GO
--restore database
USE master;
GO
RESTORE DATABASE TrainingDB
FROM DISK = 'C:\Apress\TrainingDB_StopAt.bak'
WITH FILE = 1, NORECOVERY,
STOPAT = '2012-04-30 22:17:10.563';
GO
--restore log
RESTORE LOG TrainingDB
FROM DISK = 'C:\Apress\TrainingDB_20120430_2022.trn'
WITH RECOVERY,
STOPAT = '2012-04-30 22:17:10.563';
GO
--confirm the restore
USE TrainingDB;
GO
SELECT COUNT(*)
FROM dbo.SalesOrderDetail
WHERE ProductID = 776;
GO
/* 27-3. Restoring a Database from a Differential Backup */
--setup the example
USE master;
GO
BACKUP DATABASE TrainingDB
TO DISK = 'C:\Apress\TrainingDB_DiffExample.bak';
GO
-- Time passes
BACKUP DATABASE TrainingDB
TO DISK = 'C:\Apress\TrainingDB_DiffExample.diff'
WITH DIFFERENTIAL;
GO
-- More time passes
BACKUP LOG TrainingDB
TO DISK = 'C:\Apress\TrainingDB_DiffExample_tlog.trn';
GO
--perform the restore
USE master;
GO
-- Full database restore
RESTORE DATABASE TrainingDB
FROM DISK = 'C:\Apress\TrainingDB_DiffExample.bak'
WITH NORECOVERY, REPLACE;
GO
-- Differential
RESTORE DATABASE TrainingDB
FROM DISK = 'C:\Apress\TrainingDB_DiffExample.diff'
WITH NORECOVERY;
GO
-- Transaction log
RESTORE LOG TrainingDB
FROM DISK = 'C:\Apress\TrainingDB_DiffExample_tlog.trn'
WITH RECOVERY;
GO
/* 27-4. Restoring a File or Filegroup */
USE master;
GO
If Not Exists (Select name from sys.databases where name = 'VLTestDB')
Begin
CREATE DATABASE VLTestDB
ON PRIMARY
( NAME = N'VLTestDB',FILENAME =N'c:\Apress\VLTestDB.mdf'
,SIZE = 4072KB , FILEGROWTH = 0 ),
FILEGROUP FG2
( NAME = N'VLTestDB2', FILENAME =N'c:\Apress\VLTestDB2.ndf'
, SIZE = 3048KB , FILEGROWTH = 1024KB )
,( NAME = N'VLTestDB3', FILENAME =N'c:\Apress\VLTestDB3.ndf'
, SIZE = 3048KB , FILEGROWTH = 1024KB )
LOG ON
( NAME = N'VLTestDBLog', FILENAME =N'c:\Apress\VLTestDB_log.ldf'
, SIZE = 1024KB , FILEGROWTH = 10%);
Alter DATABASE VLTestDB
Modify FILEGROUP FG2 Default;
END
GO
USE master;
GO
BACKUP DATABASE VLTestDB
FILEGROUP = 'FG2'
TO DISK = 'C:\Apress\VLTestDB_FG2.bak'
WITH NAME = N'VLTestDB-Full Filegroup Backup',
SKIP, STATS = 20;
GO
--tlog backup after a bit of time
BACKUP LOG VLTestDB
TO DISK = 'C:\Apress\VLTestDB_FG_Example.trn';
GO
--restore FG2
USE master;
GO
RESTORE DATABASE VLTestDB
FILEGROUP = 'FG2'
FROM DISK = 'C:\Apress\VLTestDB_FG2.bak'
WITH FILE = 1, NORECOVERY, REPLACE;
RESTORE LOG VLTestDB
FROM DISK = 'C:\Apress\VLTestDB_FG_Example.trn'
WITH FILE = 1, RECOVERY;
GO
/* 27-5. Performing a Piecemeal (PARTIAL) Restore */
--backup primary and FG2 filegroup
USE master;
GO
BACKUP DATABASE VLTestDB
FILEGROUP = 'PRIMARY'
TO DISK = 'C:\Apress\VLTestDB_Primary_PieceExmp.bak';
GO
BACKUP DATABASE VLTestDB
FILEGROUP = 'FG2'
TO DISK = 'C:\Apress\VLTestDB_FG2_PieceExmp.bak';
GO
--t-log backup after some time
BACKUP LOG VLTestDB
TO DISK = 'C:\Apress\VLTestDB_PieceExmp.trn';
GO
--piecemeal restore
USE master;
GO
RESTORE DATABASE VLTestDB
FILEGROUP = 'PRIMARY'
FROM DISK = 'C:\Apress\VLTestDB_Primary_PieceExmp.bak'
WITH PARTIAL, NORECOVERY, REPLACE;
RESTORE LOG VLTestDB
FROM DISK = 'C:\Apress\VLTestDB_PieceExmp.trn'
WITH RECOVERY;
GO
--view status
USE VLTestDB;
GO
SELECT name,state_desc
FROM sys.database_files;
GO
/* 27-6. Restoring a Page */
--Example setup
USE master;
GO
BACKUP DATABASE TestDB
TO DISK = 'C:\Apress\TestDB_PageExample.bak'
GO
--perform a restore
USE master;
GO
RESTORE DATABASE TestDB
PAGE='1:8'
FROM DISK = 'C:\Apress\TestDB_PageExample.bak'
WITH NORECOVERY, REPLACE;
GO
--perform tlog backup
BACKUP LOG TestDB
TO DISK = 'C:\Apress\TestDB_PageExample_tlog.trn';
GO
--final restore
RESTORE LOG TestDB
FROM DISK = 'C:\Apress\TestDB_PageExample_tlog.trn'
WITH RECOVERY;
/* 27-7. Identifying Databases with Multiple Recovery Paths */
--setup example
USE master;
GO
IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = 'RomanHistory')
BEGIN
CREATE DATABASE RomanHistory;
END
GO
BACKUP DATABASE RomanHistory
TO DISK = 'C:\Apress\RomanHistory_A.bak';
GO
USE RomanHistory;
GO
CREATE TABLE EmperorTitle
(EmperorTitleID int NOT NULL PRIMARY KEY IDENTITY(1,1), TitleNM varchar(255));
GO
INSERT Into EmperorTitle (TitleNM)
VALUES ('Aulus'), ('Imperator'), ('Pius Felix'), ('Ouintus')
BACKUP LOG RomanHistory
TO DISK = 'C:\Apress\RomanHistory_A.trn';
GO
--get database info
USE msdb;
GO
SELECT LastLSN = last_log_backup_lsn ,Rec_Fork = recovery_fork_guid
,Frst_Fork = first_recovery_fork_guid ,Fork_LSN = fork_point_lsn
FROM sys.database_recovery_status
WHERE database_id = DB_ID('RomanHistory');
GO
--data modifications and tlog backup
USE RomanHistory;
GO
INSERT Into EmperorTitle (TitleNM)
VALUES ('Germanicus'), ('Lucius'), ('Maximus'), ('Titus');
GO
BACKUP LOG RomanHistory
TO DISK = 'C:\Apress\RomanHistory_B.trn';
GO
--restore db to a prior state
USE master;
GO
RESTORE DATABASE RomanHistory
FROM DISK = 'C:\Apress\RomanHistory_A.bak'
WITH NORECOVERY, REPLACE;
RESTORE DATABASE RomanHistory
FROM DISK = 'C:\Apress\RomanHistory_A.trn'
WITH RECOVERY, REPLACE;
GO
--reissue the database info query
USE msdb;
GO
SELECT LastLSN = last_log_backup_lsn ,Rec_Fork = recovery_fork_guid
,Frst_Fork = first_recovery_fork_guid ,Fork_LSN = fork_point_lsn
FROM sys.database_recovery_status
WHERE database_id = DB_ID('RomanHistory');
GO