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

    « Meme Monday: Eleven Words or LessSQL Saturday Fun »
    comments

    One of the most important tasks a DBA should know how to perform, and should test regularly, is a database restore. It is absolutely necessary to back up your data so you have it if a disaster strikes, but it is equally important that you know how to retrieve that data when needed.

    The basic RESTORE syntax looks like:

    1. RESTORE DATABASE [Name] FROM [Device] WITH [Options]

    That restores an entire database to the last full backup. You can restore specific files, filegroups, and pages. You can also restore transaction log backups, if you have those for the database.

    I had become very familiar with database backups and restores. Most of my experience with restores was to restore either a full backup to a different environment, or restore a full backup and as many non-corrupt transaction logs as possible.

    One day, however, a developer came to me for help. An application change had been made which entered unwanted information in the database. We had a full backup from the previous night, and transaction logs through the current moment. The last full transaction log backup was from 11:00 AM. The user wanted QA restored to 2:00 PM, when this change was made. It was 4:00 PM.

    I was going to tell him it couldn’t be done, that I could only restore to 11:00 AM. I was quickly corrected, however, and I learned something new! Using STOPAT, you can restore only to a specific point in time in a database or log backup.

    SQL Saturday 2011 Raffle 110
    Yes, I was that excited!

    Restoring to a Point in Time

    I have a table with ID, DateAdded, and TimeAdded fields.


    I take a full backup of my database at 11:23 AM.



    I insert two more rows into the table.


    I take a transaction log backup at 11:28 AM.


    I delete a record.


    I take another transaction log backup at 11:38 AM.


    I insert two more records.


    I take another transaction log backup at 11:49 AM.



    I insert another record.


    I realize that an application change made at 11:40 was not needed. I want to restore the database to 11:40:00, essentially leaving record 7 but removing records 8 and 9. My transaction log backups were taken at 11:38 and 11:49.
    Here are the RESTORE statements I would use. Note the last one, which includes the command

    1. WITH STOPAT = 'Apr 3, 2011 11:40 AM'

    This tells SQL Server to restore only to that point in time.

    1. RESTORE DATABASE AdventureWorks
    2.    FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\AdventureWorks.bak' WITH NORECOVERY;
    3.  
    4. RESTORE LOG AdventureWorks
    5.    FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\AdventureWorks.trn' WITH NORECOVERY;
    6.    
    7. RESTORE LOG AdventureWorks
    8.    FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\AdventureWorks2.trn' WITH NORECOVERY;
    9.  
    10. RESTORE LOG AdventureWorks
    11.    FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\AdventureWorks3.trn' WITH STOPAT = 'Apr 3, 2011 11:40 AM', RECOVERY;

    When I select rows from my table, I can see that rows 1, 2, 3, 5 and 6 are included. These were part of the transaction log backup taken at 11:38 AM. From the transaction log backup taken at 11:49, only transactions entered before 11:40 AM are restored – row 7.


    RESTORE: Learn, Understand, Use

    The RESTORE command is a necessary tool in your database administration toolbox. It is up to you to learn it, understand it, and (most importantly) use it! Study the different options available, and make sure you know both how and when to use them.

    About the Author

    User bio imageJes Schultz Borland is a Consultant with Brent Ozar PLF, and a Microsoft SQL Server MVP. She holds an AAS - Programmer/Analyst degree, and has worked with SQL Server since 2007, focusing on Reporting Services and day-to-day administration. She is an active member of PASS, Director of Communication for MADPASS, founder of Tech on Tap, and a LessThanDot.com blogger. She's a frequent presenter at user groups, SQL Saturdays, and other community events. She is also an avid runner and chef.
    Social SitingsTwitterLinkedInLTD RSS Feed
    Instapaper

    4 comments

    Comment from: Kendra Little [Visitor] · http://littlekendra.com
    Kendra Little Awesome post, Jes!

    My personal habit is to write and run all my restore commands with NORECOVERY and put the DB recovery in a final command by itself, like:

    RESTORE DATABASE [DbName]

    This is totally a preference thing, of course, I just like having the command where the database is coming online be very explicit.

    Restoring to marked transactions can be very cool, too! That would make another great blog post.
    04/04/11 @ 09:23
    Comment from: Amit Banerjee [Visitor] · http://www.troubleshootingsql.com
    Amit Banerjee Combining this with Standby option of restore make this a very useful feature during disaster recovery scenarios.
    04/04/11 @ 09:24
    Comment from: SQLDenis [Member] Email
    SQLDenis STOPAT saved me once someone dropped a table, we only lost 1 minute(which was reentered) instead of a whole day of data
    04/04/11 @ 09:37
    Comment from: Dustin Mueller [Visitor] · https://twitter.com/#!/sqlcheesecake
    Dustin Mueller From now on, every time I use STOPAT, I will remember what you told me your reaction was to finding out about it!

    Thanks for giving me a reason to laugh when doing restores. :)
    02/28/12 @ 15:11

    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.)