Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42eb63f

Browse files
authored
Create 04b Demo Linux Inside Transaction Log Backups
1 parent 4a7dc74 commit 42eb63f

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*============================================================
2+
// Source via Bradley Ball :: [email protected]
3+
// MIT License
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5+
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
7+
8+
==============================================================*/
9+
/*
10+
--Full Recovery and the transaction log
11+
1. perform a full backup
12+
2. perform a log backup
13+
3. look at the log using dbcc sqlperf & dbcc loginfo
14+
4. do an index rebuild
15+
5. backup the log file
16+
6. Look at the size of the log file & dbcc sqlperf
17+
7. do an index rebuild
18+
8. look at the used space in the using sqlperf
19+
9. do a checkpoint
20+
10. look at the used space using sql perf
21+
11. do a full backup
22+
12. look at the used space using sql perf
23+
13. do a log backup
24+
14. start a transaction to insert some rows
25+
15. look at the log space using sql perf
26+
16. do a log backup
27+
17. look at sql perf again
28+
18. commit the tran
29+
19. look at sql perf
30+
20. do a log backup
31+
--Piecemeal recovery
32+
1. do a full backup
33+
2. restore fgs primary, 1, 2,3, 4
34+
3. do a query that hits fg1, fg2, fg3, fg4,& fg5
35+
4. finish the restore
36+
*/
37+
38+
/*
39+
Let's Take a full backup of
40+
our Database in Full Recovery
41+
and then take a log backup
42+
*/
43+
BACKUP DATABASE demoInternals_Partition TO DISK=N'/var/opt/mssql/data/demoInternals_Partition.bak' WITH INIT, stats=10
44+
GO
45+
BACKUP LOG demoInternals_Partition TO DISK=N'/var/opt/mssql/data/demoInternals_Partition1.trn' WITH INIT, stats=10
46+
GO
47+
/*
48+
Look at the log
49+
*/
50+
DBCC SQLPERF(LOGSPACE)
51+
GO
52+
USE demoInternals_Partition
53+
GO
54+
DBCC LOGINFO
55+
/*
56+
Let's rebuild an index and create some logged records
57+
*/
58+
USE demoInternals_Partition
59+
GO
60+
DBCC SQLPERF(LOGSPACE)
61+
GO
62+
ALTER INDEX PK_myTable1_myID ON dbo.myTable1 REBUILD
63+
GO
64+
DBCC SQLPERF(LOGSPACE)
65+
GO
66+
/*
67+
Backup the Log and check the sqlperf stats
68+
*/
69+
checkpoint
70+
71+
BACKUP LOG demoInternals_Partition TO DISK=N'/var/opt/mssql/data/demoInternals_Partition2.trn' WITH INIT, stats=10
72+
GO
73+
DBCC SQLPERF(LOGSPACE)
74+
GO
75+
/*
76+
Rebuild the index and try a checkpoint
77+
*/
78+
ALTER INDEX PK_myTable1_myID ON dbo.myTable1 REBUILD
79+
GO
80+
DBCC SQLPERF(LOGSPACE)
81+
GO
82+
/*
83+
let's see if a checkpoint will clear the log
84+
*/
85+
CHECKPOINT
86+
GO
87+
DBCC SQLPERF(LOGSPACE)
88+
GO
89+
/*
90+
do I really need to to transaction log
91+
backups can't I just do a full backup on the DB in full
92+
recovery model and that will clear the log?
93+
*/
94+
BACKUP DATABASE demoInternals_Partition TO DISK=N'/var/opt/mssql/data/demoInternals_Partition.bak' WITH INIT, stats=10
95+
GO
96+
DBCC SQLPERF(LOGSPACE)
97+
GO
98+
/*
99+
Backup our Log
100+
*/
101+
BACKUP LOG demoInternals_Partition TO DISK=N'/var/opt/mssql/data/demoInternals_Partition3.trn' WITH INIT, stats=10
102+
GO
103+
DBCC SQLPERF(LOGSPACE)
104+
GO
105+
/*
106+
--EXECUTE IN ANOTHER WINDOW
107+
--Imitate inserts or long running index rebuilds
108+
DECLARE @i INT
109+
SET @i=0
110+
111+
BEGIN TRAN
112+
WHILE (@i<20000)
113+
BEGIN
114+
INSERT INTO myTable1 DEFAULT VALUES;
115+
SET @i = @i +1
116+
117+
END
118+
--COMMIT TRAN
119+
*/
120+
DBCC SQLPERF(LOGSPACE)
121+
GO
122+
BACKUP LOG demoInternals_Partition TO DISK=N'/var/opt/mssql/data/demoInternals_Partition3.trn' WITH INIT, stats=10
123+
GO
124+
DBCC SQLPERF(LOGSPACE)
125+
GO
126+
dbcc loginfo
127+
/*
128+
Commit the transaction on the
129+
Other window
130+
*/
131+
DBCC SQLPERF(LOGSPACE)
132+
GO
133+
BACKUP LOG demoInternals_Partition TO DISK=N'/var/opt/mssql/data/demoInternals_Partition3.trn' WITH INIT, stats=10
134+
GO
135+
DBCC SQLPERF(LOGSPACE)
136+
GO

0 commit comments

Comments
 (0)