Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Setting a standard DateFormat for SQL ServerCumulative update package 2 for SQL Server 2008 is available »
    comments

    You're the perfect DBA. You have a small environement but critical none the less. You've setup your disaster recovery plans based on a backup schedule and solid plan. You've gone as far as to copy backups to external disk along with the tape backups the server administrators take nightly. Logs are backed up every 15 minutes just to make sure you can recover from anything including corrupt data and the largest of all fall outs. This morning however you saw your 70GB database all the sudden has a log size of 11GB and you're running out of space on the 36 drive you allocated for the logs. What's the first thing you do? Google regain disk space log files sql server. Sweet, I have a billion hits and scripts to truncate my log and all my space will be recovered. In particular you find something like this, "BACKUP LOG db_name WITH truncate_only". Happy DBA!!!

    An hour later lightning hits the building completely taking everything out because the server administrators UPS system was fried along with everything else. The company will survive though as long as you can get power and the primary db server isn't fried also or grab that spare DL380 you have just for this case. See, you've even thought of that! Good DBA. So you install the OS and slam the drives into the spare server. You connect the external disk you made those copies of the backups on. Got it all going and feeling good about yourself. Smiling happy boss is all comfortable knowing they hired you for just this event. You restore your full backup and everything is good. Restore the differential the night before and then go to the logs. Funny thing is you notice there isn't a normal log that was backed up at around the time right before the disaster that is typically over 3GB. Then you remember this is the mission critical data import that takes place daily and is unrecoverable for more loads than the single one that takes place. No worries, it must be a smaller load than normal. You have a solid backup plan and they ahve to be in there. You restore the log right before you ran that disk saving DBCC command and then go to the next one.......................right about now you're heart is pumping so hard it feels like it will your chest is going to explode. What happened??? I don't have the data I thought and my logs are nothing more than 2K after the DBCC along with my job isn't all that safe right now.

    Let's talk about what happen. There is this thing backups use called the LSN (Log Sequence Number). Very cirtical to the manner in which you can restore backups. In short when you truncated the log you essentially killed the LSN and broken the chain between the Full which is required for any differential or log backup to restore correctly. If you had access to your SQL Agent history for the job you would probably see an error for the log backup job after you truncated the logs stating something like, "BACKUP LOG cannot be performed because there is no current database backup".

    So lets watch it happen

    create a db

    1. CREATE DATABASE [dr_db] ON PRIMARY
    2. ( NAME = N'dr_db', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.LKF00TKSQL08\MSSQL\DATA\dr_db.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
    3. LOG ON
    4. ( NAME = N'dr_db_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.LKF00TKSQL08\MSSQL\DATA\dr_db_log.ldf' , SIZE = 1024KB , MAXSIZE = 102400KB , FILEGROWTH = 1%)
    5. GO

    Here is what we'll do then. Run these line for line

    1. BACKUP DATABASE dr_db TO DISK='D:\dr_db_full.bak' WITH INIT
    2. BACKUP LOG dr_db TO DISK='D:\db_dr_log1.trn'
    3. CREATE TABLE dr_db.dbo.tbl (col1 int)
    4. INSERT INTO dr_db.dbo.tbl VALUES (1)
    5. BACKUP DATABASE dr_db TO DISK='D:\dr_db_diff1.bak' WITH INIT,DIFFERENTIAL
    6. INSERT INTO dr_db.dbo.tbl VALUES (2)
    7. INSERT INTO dr_db.dbo.tbl VALUES (3)
    8. BACKUP LOG dr_db TO DISK='D:\db_dr_log2.trn'
    9. INSERT INTO dr_db.dbo.tbl VALUES (4)
    10. INSERT INTO dr_db.dbo.tbl VALUES (5)
    11. INSERT INTO dr_db.dbo.tbl VALUES (6)
    12. INSERT INTO dr_db.dbo.tbl VALUES (7)
    13. BACKUP LOG dr_db WITH truncate_only
    14. BACKUP DATABASE dr_db TO DISK='D:\dr_db_diff2.bak' WITH INIT,DIFFERENTIAL
    15. BACKUP LOG dr_db TO DISK='D:\db_dr_log4.trn'
    16. INSERT INTO dr_db.dbo.tbl VALUES (8)
    17. INSERT INTO dr_db.dbo.tbl VALUES (9)
    18. INSERT INTO dr_db.dbo.tbl VALUES (10)
    19. INSERT INTO dr_db.dbo.tbl VALUES (11)
    20. BACKUP LOG dr_db TO DISK='D:\db_dr_log5.trn'

    Pretty straight forward. You see the error come up right after the truncate_only. What you've done is basically kill your recovery to any point in time after the log backup just prior to the truncate_only. Luckily this is gone in SQL Server 2008 because you have no idea how many DBAs did this.

    There is an option to get some disk back

    If you are in need of regaining space run the SHRINKFILE on the log. If it does not shrink DO NOT TRUNC it unless you have no recovery plan. Then you're in more trouble than anyone can help so go ahead and trunc away ;)

    You can see a shrinkfile in the same situation below work out as such

    Same database and we'll create another named dr_db2

    1. BACKUP DATABASE dr_db TO DISK='D:\dr_db_full.bak' WITH INIT
    2. BACKUP LOG dr_db TO DISK='D:\db_dr_log1.trn'
    3. CREATE TABLE dr_db.dbo.tbl (col1 int)
    4. INSERT INTO dr_db.dbo.tbl VALUES (1)
    5. BACKUP DATABASE dr_db TO DISK='D:\dr_db_diff1.bak' WITH INIT,DIFFERENTIAL
    6. INSERT INTO dr_db.dbo.tbl VALUES (2)
    7. INSERT INTO dr_db.dbo.tbl VALUES (3)
    8. BACKUP LOG dr_db TO DISK='D:\db_dr_log2.trn'
    9. INSERT INTO dr_db.dbo.tbl VALUES (4)
    10. INSERT INTO dr_db.dbo.tbl VALUES (5)
    11. INSERT INTO dr_db.dbo.tbl VALUES (6)
    12. INSERT INTO dr_db.dbo.tbl VALUES (7)
    13. DBCC SHRINKFILE ('dr_db_log',TRUNCATEONLY)
    14. BACKUP LOG dr_db TO DISK='D:\db_dr_log4.trn'
    15. INSERT INTO dr_db.dbo.tbl VALUES (8)
    16. INSERT INTO dr_db.dbo.tbl VALUES (9)
    17. INSERT INTO dr_db.dbo.tbl VALUES (10)
    18. INSERT INTO dr_db.dbo.tbl VALUES (11)
    19. BACKUP LOG dr_db TO DISK='D:\db_dr_log5.trn'

    Now let's see a RESTORE

    1. RESTORE DATABASE [dr_db2] FROM DISK = N'D:\dr_db_full.bak' WITH FILE = 1, MOVE N'dr_db' TO N'D:\SQLDATA\SQLSYSDATA\MSSQL.1\MSSQL\DATA\dr_db2.mdf', MOVE N'dr_db_log' TO N'D:\SQLDATA\SQLSYSDATA\MSSQL.1\MSSQL\DATA\dr_db2_log.ldf', NORECOVERY, NOUNLOAD, REPLACE, STATS = 10
    2. RESTORE DATABASE [dr_db2] FROM DISK = N'D:\dr_db_diff1.bak' WITH NORECOVERY
    3. RESTORE LOG [dr_db2] FROM DISK = N'D:\db_dr_log2.trn' WITH NORECOVERY
    4.  
    5. RESTORE LOG [dr_db2] FROM DISK = N'D:\db_dr_log4.trn' WITH NORECOVERY
    6. RESTORE LOG [dr_db2] FROM DISK = N'D:\db_dr_log5.trn' WITH NORECOVERY
    7. RESTORE DATABASE dr_db2 WITH RECOVERY

    Let's test it to see if my "tbl" table has data from 1 to 11

    1. SELECT * FROM tbl

    And it does :) Now you're a happy DBA

    Also if you have log shipping setup and you trunc that gets fun. Next entry I want to go into is using the COPY_ONLY which is based on another LSN topic while having log shipping and remote/local recovery points. This landscape is fun to setup and it may give you ideas on how you can quickly recover data and or from a disaster.

    Rememeber, truncate very BAD!!!

    Here is a resource for you to keep going on LSN and how backups work. This is important and you should know it if you ever typed the BACKUP command. Rmemeber, just because you can do something doesn't mean you should be doing it.

    Backupset (viewing backups and LSN data) http://technet.microsoft.com/en-us/library/ms186299.aspx

    About the Author

    Ted Krueger is a SQL Server MVP and has been working in development and database administration for 13+ years. Specialties range from High Availability and Disaster / Recovery setup and testing methods down to custom assembly development for SQL Server Reporting Services. Ted blogs and is also one of the founders of LessThanDot.com technology community. Some of the articles focused on are Backup / Recovery, Security, SSIS and working on SQL Server and using all of the SQL Server features available to create stable and scalable database services. @onpnt
    Social SitingsTwitterLinkedInLTD RSS Feed
    14464 views
    Instapaper

    9 comments

    Comment from: Jerry Hung [Visitor] · http://www.SQLServerNewbie.com
    *****
    Jerry Hung Excellent article

    That's why people always say
    Take a FULL backup before truncate
    Take a FULL backup right after truncate!!!!
    11/20/08 @ 09:42
    Comment from: Ted Krueger (onpnt) [Member]
    Ted Krueger (onpnt) Thanks Jerry. That is a good point although in some cases it's not an easy task to backup that many times if the db exceeds into the TB range. even a 100GB backup can range around 30 minu with LiteSpeed attached.

    11/20/08 @ 11:11
    Comment from: SQLDenis [Member] Email
    *****
    SQLDenis At least we have native compression in SQL Server 2008 (for those shops who do not have Quest Litespeed)
    11/20/08 @ 11:25
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky Can you a bit elaborate on last topic (native compression) - though I can google myself, of course.
    07/12/09 @ 06:36
    Comment from: Ted Krueger (onpnt) [Member]
    Ted Krueger (onpnt) Hi Naomi. I'll write a blog up once the short vacation is done on SQL Server 2008 compression. Hope it will answer a few questions and such.
    07/13/09 @ 19:00
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky I think Denis already wrote one! :)
    11/25/09 @ 11:16
    Comment from: SQLDenis [Member] Email
    SQLDenis Yes I did the compression post and is shows a nice improvement in backup time as well as restore time and of course the backup itself is only about 25% of the original uncompressed one....And going forward standard editions will also be able to create a compressed backup not just do the restore
    12/05/09 @ 08:09
    Comment from: Engrish? [Visitor]
    Engrish? Not to nitpick at free (and helpful) information here, but can you work on the grammar? Especially your constant use of the word 'rate' instead of 'right'. It's hard to focus on the content when you have to keep re-reading the sentences to figure out what you're trying to say.
    02/11/10 @ 10:06
    Comment from: Ted Krueger (onpnt) [Member]
    Ted Krueger (onpnt) That's not nitpicking at all. It was a complaint I received often and have been working on with peer reviews prior to publishing. I will review older blogs like this one and make corrections.

    Thanks for the comment and suggestions. They all help greatly in my efforts to share what I know on SQL Server.
    02/11/10 @ 10:30

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)