You are on page 1of 3

BACKUP using NO_TRUNCATE and RESTORE

NOTE: When a database's data files are unavailable, perhaps due to a hardware failure, you can still back up the transaction log if the media it is on is still available. However, you need to issue the BACKUPLOG command with the NO_TRUNCATE option. This will allow you to take a backup of the transaction log up to the point just before the hardware failure, and is usually referred to as backing up the tail of the transaction log. However, if your database is using the bulk-logged recovery model, and the transaction log contains minimally logged transactions, the data files which contain the modified pages must also be available. If those data files are unavailable, you will not be able to back up the tail of the transaction log. This is another point to consider when using the bulk-logged recovery model Overview The BACKUP LOG command has an option named NO_TRUNCATE. This is used when you have an inaccessible database suspect! "o# instance$ and %ou &ant to 'ac(up the o#phaned t#ansaction )og "o# that data'ase. Because o" the name o" the pa#amete#! ho&e*e#! a common misunde#standing is that the pu#pose o" this option is that %ou can do se*e#a) )og 'ac(ups using NO_TRUNCATE and only apply the most recent when you do RESTORE. That is not the case see *e#sion note 'e)o&! though$. Acknowledgements Than(s to Pete# +eoh at %oh, So"t&a#e! &&&.%oh,.com. -.*e ne*e# #ecommended using NO_TRUNCATE e/cept &hen %ou ha*e an inaccessi')e data'ase. But Pete# opened m% e%es "o# the "act that S0L Se#*e# on)% inc)ude )og #eco#ds since )ast )og 'ac(up! #ega#d)ess o" &hethe# that one &as p#oduced using NO_TRUNCATE o# not. Versions This a#tic)e &as o#igina))% &#itten "o# S0L Se#*e# 1222. S0L Se#*e# 1223 has a ne& option "o# the 'ac(up command! ca))ed COP+_ONL+ &hich a))o& %ou to do a 'ac(up "o# pu#pose o" cop%ing a data'ase &ithout a""ecting the 'ac(up se4uence the option app)ies "o# "u)) and )og 'ac(ups$. A)so! a 4uic( test on 1223 suggest that %ou can s(ip inte#mediate (o5i se na)a,i u s#edini )og 'ac(ups done using NO_TRUNCATE. This seems to ma(e NO_TRUNCATE p#ett% much the same as COP+_ONL+ "o# )og 'ac(ups. Pe#sona))%! sti)) p#e"e# to thin( o" NO_TRUNCATE as 6a))o& me to 'ac(up the )og o" a damaged data'ase6. Be)o& is ho& S0L Se#*e# 1223 Boo(s On)ine e/p#esses it7 6The NO_TRUNCATE option o" BACKUP LOG is e4ui*a)ent to speci"%ing 'oth COP+_ONL+ and CONT-NUE_A8TER_ERROR.6 Scenario Sa% %ou ha*e the 'e)o& 'ac(up se4uence7 9. BACKUP :ATABASE 1. BACKUP LOG ... ;-T< NO_TRUNCATE =. BACKUP LOG ... ;-T< NO_TRUNCATE >. BACKUP LOG ... ;-T< NO_TRUNCATE ;e &i)) )oo( at the e""ect o" a'o*e "o# the acti*e t#ansaction )og "i)e s$! the )d" "i)e s$. ;e &i)) a)so )oo( at the si,e o" the t#ansaction )og 'ac(ups p#oduced. And o" cou#se the RESTORE options &e ha*e. Size of the active transaction log A side e""ect o" NO_TRUNCATE option is that S0L Se#*e# does not empt% the acti*e t#ansaction )og. ?% guess is that this is 'ecause the data'ase might 'e inaccessi')e! so S0L se#*e# cannot pe#"o#m a C<ECKPO-NT in the data'ase. End #esu)t is that the )d" "i)e s$ &i)) (eep g#o&ing unti) %ou hand)e this someho&. Size of the log backups This might come as a su#p#ise! 'ut on S0L Se#*e# 1222 each )og 'ac(up &i)) on)% contain the modi"ications p#oduced since the p#io# )og 'ac(up. Se 'e)o& "o# ho& this a""ects the a'i)it% to #esto#e. Restore options As a)&a%s! %ou ha*e to sta#t '% #esto#ing a data'ase 'ac(up. One might thin( that %ou then on)% ha*e to the most #ecent )og 'ac(up since &e used the NO_TRUNCATE option$! i.e.7

RESTORE :ATABASE "#om step RESTORE LOG "#om step !

<o&e*e#! that is not possi')e on S0L Se#*e# 1222. This is 'ecause S0L Se#*e# did on)% inc)ude the modi"ications p#oduced since the )ast )og 'ac(up! e*en &hen using the NO_TRUNCATE option. A p#ope# #esto#e scena#io "o# S0L Se#*e# 1222 &ou)d 'e7 RESTORE :ATABASE "#om step 9 RESTORE LOG "#om step 1 RESTORE LOG "#om step = RESTORE LOG "#om step > A'o*e &i)) o" cou#se not 'e possi')e i" %ou didn@t (eep the 'ac(ups p#oduced "#om step 1 and =A "s #icrosoft misleading us for S$% Server &'''( - &ou)d sa% 'oth 6%es6 and 6no6 to this. 8i#st! - &ant to st#ess that the documentation does not state that NO_TRUNCATE can 'e used to #esto#e "#om on)% 9 and > a'o*e. The documentation states that NO_TRUNCATE is used &hen %ou ha*e an inaccessi')e data'ase. <o&e*e#! the name o" this option suggests that %ou don@t ha*e to app)% a)) )og 'ac(ups! that on)% the most #ecent &i)) do. - ha*e suggested to ?S to change the name o" this option so it #e")ects &hat the pu#pose o" the option is! instead o" the technica) imp)ementation. - ha*e a "ee)ing that this &i)) not happen! though. Script proving above points) The sc#ipt 'e)o& pe#"o#ms a data'ase 'ac(up and th#ee )og 'ac(ups. A)) )og 'ac(ups a#e p#oduced using the NO_TRUNCATE option. Be"o#e each )og 'ac(up is p#oduced a num'e# o" modi"ications a#e pe#"o#med in the data'ase. - suggest that %ou monito# the si,e o" 'oth the )d" "i)e as &e)) as the )og 'ac(up "i)es p#oduced. +ou &i)) see on 1222 that each )og 'ac(up is a'out the same si,e! &hi)e on 1223 the si,e &i)) acumu)ate. ;a#ning7 Be)o& &i)) de)ete a data'ase named @test@! i" such e/istsA
SET NOCOUNT ON

USE master GO IF EXISTS (SELECT * FROM sysdatabases WHERE name = 'test') DROP DATABASE test GO CREATE DATABASE test GO USE test CREATE TABLE t(c1 INT IDENTITY, c2 CHAR(5000) DEFAULT 'hello') BACKUP DATABASE test TO DISK = 'c:\db.bak' WITH INIT GO DECLARE @a INT SET @a = 1 WHILE @a <= 1000 BEGIN INSERT INTO t DEFAULT VALUES SET @a = @a + 1 END BACKUP LOG test TO DISK = 'C:\1.bak' WITH NO_TRUNCATE, INIT GO DECLARE @a INT SET @a = 1 WHILE @a <= 1000 BEGIN INSERT INTO t DEFAULT VALUES SET @a = @a + 1 END BACKUP LOG test TO DISK = 'C:\2.bak' WITH NO_TRUNCATE, INIT GO DECLARE @a INT SET @a = 1 WHILE @a <= 1000 BEGIN INSERT INTO t DEFAULT VALUES SET @a = @a + 1 END BACKUP LOG test TO DISK = 'C:\3.bak' WITH NO_TRUNCATE, INIT USE master RESTORE DATABASE test FROM DISK = 'C:\db.bak' WITH NORECOVERY, REPLACE

--Below fails on 2000 RESTORE LOG test FROM DISK = 'C:\3.bak' WITH RECOVERY --You have to restore RESTORE DATABASE test RESTORE LOG test FROM RESTORE LOG test FROM RESTORE LOG test FROM --Clean up DROP DATABASE test the transaction log backups in sequence: FROM DISK = 'C:\db.bak' WITH NORECOVERY, REPLACE DISK = 'C:\1.bak' WITH NORECOVERY DISK = 'C:\2.bak' WITH NORECOVERY DISK = 'C:\3.bak' WITH RECOVERY

You might also like