Home > SQL Server Tips > Database Management and Administration > How to restore from a transaction log in SQL Server
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

DATABASE MANAGEMENT AND ADMINISTRATION

How to restore from a transaction log in SQL Server


Greg Robidoux, Edgewood Solutions
01.12.2006
Rating: -4.25- (out of 5)


Expert advice on database administration
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


Having a backup plan in place is half the battle in keeping your SQL Server up and running. The other half involves the restore process. You need to restore SQL Server after a failure (when you need to restore to a standby server) or when refreshing a reporting or development environment. Each of these scenarios can involve restoring transaction log backups.

The challenge that transaction log backups introduce is that they require you to restore multiple files rather than just one large full backup file. In order to successfully restore the database, you must have all of the transaction log backups that were created and they must be restored in the order they were created. If any transaction log backups are corrupt, you cannot restore any transaction log backups after the corrupt one. They need to be restored in order, and you can't skip any of the files.

Here we will take a closer look at knowing what to restore and how to restore transaction log backups.

TABLE OF CONTENTS
  [IMAGE] Step 1: Back up the active portion of the transaction log
  [IMAGE] Step 2: Identify what needs to be restored
  [IMAGE] Step 3: Validate that backups have the correct contents
  [IMAGE] Step 4: Know your restore options
  [IMAGE] Step 5: Take advantage of point-in-time recovery

[IMAGE][IMAGE]  Back up the active portion of the transaction log[IMAGE] Return to Table of Contents

In the event of a failure and you need to restore to another server, you should first back up the active portion of the transaction log to get the remaining transactio...


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED CONTENT
SQL Server Backup and Recovery
SQL Server Mailbag: Data restoration and DB property management
Achieving high availability and disaster recovery with SharePoint databases
How to 'do' SQL Server disaster recovery
The keys to database backup protection for SQL Server
Choosing a SQL Server disaster recovery solution
Licensing a standby server for SQL Server replication
Can I encrypt and restore a database backup in SQL Server 2005?
SQL Server errors, failures and other problems fixed from the trenches
Get SQL Server log shipping functionality without Enterprise Edition
SQL Server 2008 backup compression pros and cons
SQL Server Backup and Recovery Research

Database Management and Administration
Using traces in SQL Server Profiler
Meet compliance requirements with improved database security practices
Hardening the network and OS for SQL Server security
Securing the server and database in SQL Server
How SQL Server 2008 components impact SharePoint implementations
Troubleshooting Distributed Transaction Coordinator errors in SQL Server
Achieving high availability and disaster recovery with SharePoint databases
Clearing the Windows page file and its effect on server performance
Deploying a SQL Server virtual appliance for Microsoft Hyper-V
How to create SQL Server virtual appliances for Hyper-V

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
rollback  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


ns that have not yet been backed up to an existing transaction log backup. To create this last transaction log backup, use something similar to the following command:

This creates another transaction log backup that will then be used in your restore process.

[IMAGE][IMAGE]  Identify what needs to be restored[IMAGE] Return to Table of Contents

The next step is to determine which files need to be restored. Hopefully you already know which backups need to be restored. If not, you can query the SQL Server tables in the msdb; these tables will show you all the backups issued on your server, including backups created using Maintenance Plans, the wizard in Enterprise Manager, T-SQL commands and third-party tools that use the built-in SQL Server functions to issue backups.

The tables consist of the following in the msdb:

  • backupfile - Contains one row for each data or log file that is backed up
  • backupmediafamily - Contains one row for each media family
  • backupmediaset - Contains one row for each backup media set
  • backupset - Contains a row for each backup set

If you are doing a complete restore, you need to begin with a Full Backup and then include differentials and any transaction log backups after the differentials. The data in these tables shows the oldest backups first, so look for the latest Full Backup and any other backups that were issued after the Full Backup.

[IMAGE][IMAGE]  Validate that backups have the correct contents[IMAGE] Return to Table of Contents

In addition to the RESTORE command to restore backups, there are other RESTORE commands that let you see the contents of the backup files. These commands are RESTORE HEADERONLY and RESTORE FILELISTONLY.

RESTORE HEADERONLY

This command allows you to see the backup header information for all backup sets on a particular backup device. This command is useful when you have multiple files to work with or if you get a backup file from a server that you do not manage. To determine what is stored on the backup files, you can run this command in Query Analyzer.

RESTORE FILELISTONLY

This command allows you to see a list of the database and log files contained in the backup set, which filegroup they are in and also the size of the data and log files. The LogicalName and PhysicalName are key data elements that are used during the restore process.

RESTORE FILELISTONLY FROM DISK = 'C:\SQL\Backup\North.bak'

[IMAGE][IMAGE]  Know your restore options[IMAGE] Return to Table of Contents

There are several options to use when restoring backups, including transaction log backups. The following are some of those options:

NORECOVERY

This option allows you to restore additional backup files. You can use it when restoring Full, Differential or Transaction Log backups. RECOVERY

This is the default if no option is specified. This option would be used on the last restore issued. Once used, you cannot restore additional backups; to do so you must start the restore process all over again. This option can be used when restoring Full, Differential or Transaction Log backups. STANDBY

This option allows you to put the database in a read-only mode, but it still allows additional transaction logs to be restored. It can be used when restoring Full, Differential or Transaction Log backups. MOVE

When restoring databases to a different server, you will probably need to use the MOVE option unless the servers are set up the same way. Above I mentioned using the LogicalName and PhysicalName from the RESTORE FILELISTONLY command. The MOVE option lets you move the physical files to a different location on your server. You will need to use the MOVE option for all of your restores including Full, Differential and Transaction Log.

[IMAGE][IMAGE]  Take advantage of point-in-time recovery[IMAGE] Return to Table of Contents

In addition to doing a complete restore of the transaction log, SQL Server also has options to stop at a particular point in time or transaction mark. This way if you know when or where the database problem occurred, you can recover your database transactions to that particular point prior to the database problem you are trying to avoid. For example if someone deleted all records from a table, you might want to recover your database to the point in time prior to that operation.

STOPAT

This option allows you to recover all committed transactions that occurred up to Dec. 31, 2005; 12:00 AM.

The next two commands allow you to restore your transactions using transaction marks, which must be named transactions used within your application. If you are not using named transactions, this option will not work.

STOPATMARK

With this option, you can recover all committed transactions that occurred up to transaction mark Invoice1024.

STOPBEFOREMARK

This option allows you to recover all committed transactions that occurred prior to transaction mark Invoice1024.

As you can see, several restore options may be used for all of your backup files and certain options are only available for transaction log backups. Take the time to learn about the different options and when you might use one over another. Also, make sure you know where your backups are being stored and try to use a naming convention that makes it easy to determine what the contents are in the backup files. The RESTORE HEADERONLY and RESTORE FILELISTONLY are great tools to help you figure this out, but using a good naming convention makes this step a lot faster and more reliable.

About the author: Greg Robidoux is the president and founder of Edgewood Solutions LLC, a technology services company delivering professional services and product solutions for Microsoft SQL Server. He has authored numerous articles and has delivered presentations at regional SQL Server users' groups and national SQL Server events. Robidoux, who also serves as the SearchSQLServer.com Backup and Recovery expert, welcomes your questions.


More information from SearchSQLServer.com

  • Tip: Restoring a database from another SQL Server
  • Step-by-Step Guide: How to properly back up a SQL Server
  • Learning Center: Top 10 SQL Server tips of 2005


  • Rate this Tip
    To rate tips, you must be a member of SearchSQLServer.com.
    Register now to start rating these tips. Log in if you are already a member.




    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



    SQL Server Development - .NET, C#, T-SQL, Visual Basic
    HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts