-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummit2020_demo_006.txt
360 lines (287 loc) · 7.37 KB
/
summit2020_demo_006.txt
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
#######################################################################################
# PASS Virtual Summit 2020 - SQL Server on Linux from A to Z (Randolph West) #
# Pre-conference session (2020-11-10) #
#######################################################################################
# The scripts in this document are procured from several sources, including Microsoft #
# Docs, Wikipedia, genuinecoder.com, linuxhint.com, howtogeek.com, linux.org, #
# ss64.com, and tecmint.com. #
# No copyright is claimed or intended from these code samples #
#######################################################################################
------------------------------------------
-- EXAMPLE 1: Database BackupTestEx1
------------------------------------------
USE [master];
GO
-- Hey! Yes, you! Have you turned on backup
-- compression at the server level?
SELECT name, value, value_in_use
FROM sys.configurations
WHERE name = N'backup compression default';
GO
EXEC sp_configure 'backup compression default', 1;
RECONFIGURE;
GO
-- Create a new database for demo purposes
DROP DATABASE IF EXISTS BackupTestEx1;
GO
CREATE DATABASE BackupTestEx1;
GO
USE BackupTestEx1;
GO
DROP TABLE IF EXISTS dbo.TableTest;
GO
CREATE TABLE TableTest
(
Column1 INT PRIMARY KEY IDENTITY(1, 1)
);
GO
-- Insert some data
INSERT INTO dbo.TableTest
DEFAULT VALUES;
GO 10
-- See how the data looks
SELECT
*
FROM
dbo.TableTest;
GO
-- Take a full backup
BACKUP DATABASE BackupTestEx1
TO DISK = N'/var/opt/mssql/data/BackupTestEx1.bak';
GO
-- Take a log backup to record any changes
-- and truncate (set inactive) the VLFs
BACKUP LOG BackupTestEx1
TO DISK = N'/var/opt/mssql/data/BackupTestEx1Log1.trn';
GO
-- Insert more data
INSERT INTO dbo.TableTest
DEFAULT VALUES;
GO 10
-- Take another log backup to record changes
BACKUP LOG BackupTestEx1
TO DISK = N'/var/opt/mssql/data/BackupTestEx1Log2.trn';
GO
BACKUP DATABASE BackupTestEx1
TO DISK = N'/var/opt/mssql/data/BackupTestEx1.diff'
WITH DIFFERENTIAL;
GO
-- Insert more data
INSERT INTO dbo.TableTest
DEFAULT VALUES;
GO 10
-- One more log backup to record these changes
BACKUP LOG BackupTestEx1
TO DISK = N'/var/opt/mssql/data/BackupTestEx1Log3.trn';
GO
-- Similate a disaster recovery scenario
USE [master];
GO
DROP DATABASE IF EXISTS BackupTestEx1;
GO
-- Restore full backup, with recovery
RESTORE DATABASE BackupTestEx1
FROM DISK = N'/var/opt/mssql/data/BackupTestEx1.bak';
GO
-- See how the data looks
USE BackupTestEx1;
GO
SELECT *
FROM dbo.TableTest;
GO
USE [master];
GO
DROP DATABASE IF EXISTS BackupTestEx1;
GO
-- Restore full backup, with no recovery
RESTORE DATABASE BackupTestEx1
FROM DISK = N'/var/opt/mssql/data/BackupTestEx1.bak'
WITH NORECOVERY;
GO
USE BackupTestEx1;
GO
-- Restore most recent transaction log file
RESTORE DATABASE BackupTestEx1
FROM DISK = N'/var/opt/mssql/data/BackupTestEx1Log3.trn'
WITH RECOVERY;
GO
-- Transaction log backups are incremental, so all of them are required
RESTORE DATABASE BackupTestEx1
FROM DISK = N'/var/opt/mssql/data/BackupTestEx1Log1.trn'
WITH NORECOVERY;
GO
RESTORE DATABASE BackupTestEx1
FROM DISK = N'/var/opt/mssql/data/BackupTestEx1Log2.trn'
WITH NORECOVERY;
GO
RESTORE DATABASE BackupTestEx1
FROM DISK = N'/var/opt/mssql/data/BackupTestEx1Log3.trn'
WITH NORECOVERY;
GO
-- Roll forward committed transactions and roll back uncommitted transactions
RESTORE DATABASE BackupTestEx1
WITH RECOVERY;
GO
-- See how the data looks
USE BackupTestEx1;
GO
SELECT *
FROM dbo.TableTest;
GO
USE [master];
GO
DROP DATABASE IF EXISTS BackupTestEx1;
GO
-- Restore full backup, with no recovery
RESTORE DATABASE BackupTestEx1
FROM DISK = N'/var/opt/mssql/data/BackupTestEx1.bak'
WITH NORECOVERY;
GO
-- Restore differential backup, with no recovery
RESTORE DATABASE BackupTestEx1
FROM DISK = N'/var/opt/mssql/data/BackupTestEx1.diff'
WITH NORECOVERY;
GO
-- Differential backups are not incremental...
-- so this will fail
RESTORE DATABASE BackupTestEx1
FROM DISK = N'/var/opt/mssql/data/BackupTestEx1Log1.trn'
WITH NORECOVERY;
GO
-- and this will fail too
RESTORE DATABASE BackupTestEx1
FROM DISK = N'/var/opt/mssql/data/BackupTestEx1Log2.trn'
WITH NORECOVERY;
GO
-- ... but this transaction log backup will work
-- because of the LSN
RESTORE DATABASE BackupTestEx1
FROM DISK = N'/var/opt/mssql/data/BackupTestEx1Log3.trn'
WITH NORECOVERY;
GO
-- Roll forward committed transactions and roll back uncommitted transactions
RESTORE DATABASE BackupTestEx1
WITH RECOVERY;
GO
-- See how the data looks
USE BackupTestEx1;
GO
SELECT *
FROM dbo.TableTest;
GO
------------------------------------------
-- EXAMPLE 2: Database BackupTestEx2
------------------------------------------
-- Copy only backup with transaction log backups
-- Delete the old backup files from the C: drive
USE [master];
GO
-- Create a new database for demo purposes
DROP DATABASE IF EXISTS BackupTestEx2;
GO
CREATE DATABASE BackupTestEx2;
GO
USE BackupTestEx2;
GO
DROP TABLE IF EXISTS dbo.TableTest;
GO
CREATE TABLE TableTest
(
Column1 INT PRIMARY KEY IDENTITY(1, 1)
);
GO
-- Insert some data
INSERT INTO dbo.TableTest
DEFAULT VALUES;
GO 10
-- See how it looks
SELECT *
FROM dbo.TableTest;
GO
-- Everyone needs a good full backup to seed the backup chain
BACKUP DATABASE BackupTestEx2
TO DISK = N'/var/opt/mssql/data/BackupTestEx2.bak';
GO
-- Let's do a copy-only backup now as well
BACKUP DATABASE BackupTestEx2
TO DISK = N'/var/opt/mssql/data/BackupTestEx2CopyOnly.bak'
WITH COPY_ONLY;
GO
-- Insert some data
INSERT INTO dbo.TableTest
DEFAULT VALUES;
GO 10
-- Let's do a copy-only log backup as well
-- Does not truncate the log
BACKUP LOG BackupTestEx2
TO DISK = N'/var/opt/mssql/data/BackupTestEx2CopyOnly.trn'
WITH COPY_ONLY;
GO
-- Does truncate the log
BACKUP LOG BackupTestEx2
TO DISK = N'/var/opt/mssql/data/BackupTestEx2Log1.trn';
GO
-- Let's do another full backup for fun
BACKUP DATABASE BackupTestEx2
TO DISK = N'/var/opt/mssql/data/BackupTestEx2Full2.bak';
GO
-- Insert some more data
INSERT INTO dbo.TableTest
DEFAULT VALUES;
GO 10
BACKUP LOG BackupTestEx2
TO DISK = N'/var/opt/mssql/data/BackupTestEx2Log2.trn';
GO
-- Prove that Copy-Only Backups don't
-- affect the backup chain
USE [master];
GO
DROP DATABASE IF EXISTS BackupTestEx2;
GO
-- Restore copy-only backup, with no recovery
RESTORE DATABASE BackupTestEx2
FROM DISK = N'/var/opt/mssql/data/BackupTestEx2CopyOnly.bak'
WITH NORECOVERY;
GO
RESTORE LOG BackupTestEx2
FROM DISK = N'/var/opt/mssql/data/BackupTestEx2Log1.trn'
WITH NORECOVERY;
GO
RESTORE LOG BackupTestEx2
FROM DISK = N'/var/opt/mssql/data/BackupTestEx2Log2.trn'
WITH NORECOVERY;
GO
RESTORE DATABASE BackupTestEx2
WITH RECOVERY;
GO
-- See that all the data is there
USE BackupTestEx2;
GO
SELECT *
FROM dbo.TableTest;
GO
-- That works even with the full backup that
-- was taken in between, because transaction
-- logs are incremental
-- Let's try with the second full backup
USE [master];
GO
DROP DATABASE IF EXISTS BackupTestEx2;
GO
-- Restore full backup, with log
RESTORE DATABASE BackupTestEx2
FROM DISK = N'/var/opt/mssql/data/BackupTestEx2Full2.bak'
WITH NORECOVERY;
GO
RESTORE LOG BackupTestEx2
FROM DISK = N'/var/opt/mssql/data/BackupTestEx2Log2.trn';
GO
-- See that all the data is there
USE BackupTestEx2;
GO
SELECT *
FROM dbo.TableTest;
GO
-- Always run DBCC CHECKDB after a restore
DBCC CHECKDB WITH NO_INFOMSGS, ALL_ERRORMSGS;
GO