fork(2) download
  1. --Backup/restore test script for SQL Server (tested with 2012)
  2.  
  3. CREATE DATABASE BackupTest
  4. GO
  5.  
  6. ALTER DATABASE BackupTest
  7. SET RECOVERY FULL WITH NO_WAIT
  8. GO
  9.  
  10. CREATE TABLE BackupTest.dbo.MsgLog (
  11. ID INT IDENTITY
  12. CONSTRAINT PK_MsgLog PRIMARY KEY,
  13. MsgText VARCHAR(MAX)
  14. )
  15. GO
  16.  
  17. --Simulate some transactions, perform trx log backups...
  18. INSERT INTO BackupTest.dbo.MsgLog (MsgText) VALUES ('Inserted before FULL backup')
  19. GO
  20.  
  21. BACKUP DATABASE BackupTest TO DISK = 'BackupTest.Full.bak' WITH INIT
  22. GO
  23.  
  24. INSERT INTO BackupTest.dbo.MsgLog (MsgText) VALUES ('Inserted before Trx Log backup #1')
  25. GO
  26.  
  27. BACKUP LOG BackupTest TO DISK = 'BackupTest.01.trn' WITH INIT
  28. GO
  29.  
  30. INSERT INTO BackupTest.dbo.MsgLog (MsgText) VALUES ('Inserted before Trx Log backup #2')
  31. GO
  32.  
  33. BACKUP LOG BackupTest TO DISK = 'BackupTest.02.trn' WITH INIT
  34. GO
  35.  
  36. INSERT INTO BackupTest.dbo.MsgLog (MsgText) VALUES ('Inserted before Trx Log backup #3')
  37. GO
  38.  
  39. BACKUP LOG BackupTest TO DISK = 'BackupTest.03.trn' WITH INIT
  40. GO
  41.  
  42. INSERT INTO BackupTest.dbo.MsgLog (MsgText) VALUES ('Inserted before Trx Log backup #4')
  43. GO
  44.  
  45. BACKUP LOG BackupTest TO DISK = 'BackupTest.04.trn' WITH INIT
  46. GO
  47.  
  48. INSERT INTO BackupTest.dbo.MsgLog (MsgText) VALUES ('Inserted before Trx Log backup #5')
  49. GO
  50.  
  51. BACKUP LOG BackupTest TO DISK = 'BackupTest.05.trn' WITH INIT
  52. GO
  53.  
  54. INSERT INTO BackupTest.dbo.MsgLog (MsgText) VALUES ('Inserted before Trx Log backup #6')
  55. GO
  56.  
  57. BACKUP LOG BackupTest TO DISK = 'BackupTest.06.trn' WITH INIT
  58. GO
  59.  
  60. INSERT INTO BackupTest.dbo.MsgLog (MsgText) VALUES ('Inserted before Trx Log backup #7')
  61. GO
  62.  
  63. BACKUP LOG BackupTest TO DISK = 'BackupTest.07.trn' WITH INIT
  64. GO
  65. --Assume the above backup completed, but RESTORE VERIFY ONLY indicates a bad backup.
  66.  
  67.  
  68. INSERT INTO BackupTest.dbo.MsgLog (MsgText) VALUES ('Inserted before DIFFERENTIAL backup.')
  69. GO
  70.  
  71. --Perform a diff backup to use as a baseline for upcoming trx log backups.
  72. BACKUP DATABASE BackupTest TO DISK = 'BackupTest.Diff.bak' WITH DIFFERENTIAL, INIT
  73. GO
  74.  
  75. --Continue to simulate some transactions, perform trx log backups...
  76. INSERT INTO BackupTest.dbo.MsgLog (MsgText) VALUES ('Inserted before Trx Log backup #8')
  77. GO
  78.  
  79. BACKUP LOG BackupTest TO DISK = 'BackupTest.08.trn' WITH INIT
  80. GO
  81.  
  82. INSERT INTO BackupTest.dbo.MsgLog (MsgText) VALUES ('Inserted before Trx Log backup #9')
  83. GO
  84.  
  85. BACKUP LOG BackupTest TO DISK = 'BackupTest.09.trn' WITH INIT
  86. GO
  87.  
  88. INSERT INTO BackupTest.dbo.MsgLog (MsgText) VALUES ('Inserted before Trx Log backup #10')
  89. GO
  90.  
  91. BACKUP LOG BackupTest TO DISK = 'BackupTest.10.trn' WITH INIT
  92. GO
  93.  
  94. --Disaster Recovery event takes place. Time to save the data.
  95.  
  96. --Restore full.
  97. RESTORE DATABASE BackupTest
  98. FROM DISK = 'BackupTest.Full.bak'
  99. WITH REPLACE, NORECOVERY
  100. GO
  101.  
  102. --Restore diff.
  103. RESTORE DATABASE BackupTest
  104. FROM DISK = 'BackupTest.Diff.bak'
  105. WITH NORECOVERY
  106. GO
  107.  
  108. --Skip all trx log backups created prior to the diff,
  109. --Restore all trx log backups created after the diff.
  110. RESTORE DATABASE BackupTest
  111. FROM DISK = 'BackupTest.08.trn'
  112. WITH NORECOVERY
  113. GO
  114.  
  115. RESTORE DATABASE BackupTest
  116. FROM DISK = 'BackupTest.09.trn'
  117. WITH NORECOVERY
  118. GO
  119.  
  120. RESTORE DATABASE BackupTest
  121. FROM DISK = 'BackupTest.10.trn'
  122. WITH NORECOVERY
  123. GO
  124.  
  125. RESTORE DATABASE BackupTest
  126. WITH RECOVERY
  127. GO
  128.  
  129. --Verify no data is lost.
  130. SELECT *
  131. FROM BackupTest.dbo.MsgLog
Runtime error #stdin #stdout #stderr 0s 2964KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: near line 1: near "DATABASE": syntax error
Error: near line 4: near "DATABASE": syntax error
Error: near line 8: near ".": syntax error
Error: near line 18: near ".": syntax error
Error: near line 21: near "BACKUP": syntax error
Error: near line 24: near ".": syntax error
Error: near line 27: near "BACKUP": syntax error
Error: near line 30: near ".": syntax error
Error: near line 33: near "BACKUP": syntax error
Error: near line 36: near ".": syntax error
Error: near line 39: near "BACKUP": syntax error
Error: near line 42: near ".": syntax error
Error: near line 45: near "BACKUP": syntax error
Error: near line 48: near ".": syntax error
Error: near line 51: near "BACKUP": syntax error
Error: near line 54: near ".": syntax error
Error: near line 57: near "BACKUP": syntax error
Error: near line 60: near ".": syntax error
Error: near line 63: near "BACKUP": syntax error
Error: near line 68: near ".": syntax error
Error: near line 72: near "BACKUP": syntax error
Error: near line 76: near ".": syntax error
Error: near line 79: near "BACKUP": syntax error
Error: near line 82: near ".": syntax error
Error: near line 85: near "BACKUP": syntax error
Error: near line 88: near ".": syntax error
Error: near line 91: near "BACKUP": syntax error
Error: near line 97: near "RESTORE": syntax error
Error: near line 103: near "RESTORE": syntax error
Error: near line 110: near "RESTORE": syntax error
Error: near line 115: near "RESTORE": syntax error
Error: near line 120: near "RESTORE": syntax error
Error: near line 125: near "RESTORE": syntax error
Error: incomplete SQL: SELECT *
FROM BackupTest.dbo.MsgLog
-- your code goes here