This lesson explains how to restore backups of various types. Before undertaking any type of restore operation, you should make sure that you have a valid backup set and that you have all devices that contain the backup set.
After this lesson, you will be able to
- Restore backups from different backup types
Estimated lesson time: 90minutes
When you restore a database from a complete database backup, SQL Server re-creates the database and all of its associated files and then places them in their original locations. All database objects are re-created automatically. You do not need to rebuild the database schema before you restore the database.
You will typically restore from a complete database backup when
The RECOVERY option initiates the recovery process so that your database is returned to a consistent state. Use the following guidelines in choosing the RECOVERY or NORECOVERY option:
The following example assumes that a complete backup exists on the nwindbac named backup device and that two backup sets are appended to that device. The Northwind database is completely replaced by the second backup set on the nwindbac named backup device. Finally, the recovery process returns the database to a consistent state (rolls forward committed changes and rolls back uncommitted activities).
USE master RESTORE DATABASE northwind FROM nwindbac WITH FILE = 2, RECOVERY |
In this exercise, you will restore the nwcopy database from a backup provided on the Supplemental Course Materials CD-ROM. You will use this database in the other exercises in this chapter to practice restoring databases.
This script restores the nwcopy database, which is used in the other exercises in this chapter.
In this exercise, you will execute a script that adds a row to the Products table. You will then write and execute a query that returns the new row.
This script adds the new product Maple Flavor Pancake Mix to the Products table.
In this exercise, you will execute a script that backs up the nwcopy database to a single backup device.
Open C:\Sqladmin\Exercise\Ch10\Makeback.sql, review its contents, and then execute it.
This script backs up the nwcopy database to a single backup device. This backup device has a logical name of nwc2 and a physical name of C:\Mssql7 \Backup\Nwc2.bak.
In this exercise, you will execute a script that damages the database by updating all rows in the Products table. You will then write and execute a query to confirm that, due to the erroneous update, the product Maple Flavor Pancake Mix no longer appears in the Products table.
This script damages the database by updating all rows in the Products table.
In this exercise, you will use SQL Server Enterprise Manager to restrict access to the nwcopy database, restore from a complete database backup, and then allow access to the database after the restore process is complete.
IMPORTANT
You must close the query window or select another database in the query window in order to complete this exercise. The restore operation requires that no users use the database during the restore. Check that no other query windows are using the nwcopy database.
In this exercise, you will write and execute a query that returns the Maple Flavor Pancake Mix product and another that lists all of the products in the Products table.
Open a query window, open C:\Sqladmin\Exercise\Ch10\ChkRest.sql, review its contents, and then execute it.
When you restore a database from a differential database backup, the following occur:
The restore often takes less time than it does to apply a series of transaction logs representing the same database activity.
When you restore from a differential backup, consider the following facts and guidelines:
The following example restores a differential backup without recovering the database. The nwindbacdiff device contains a differential backup. Specifying the NORECOVERY option allows you to restore transaction logs. The RECOVERY option will be specified for the last transaction log restore.
USE master RESTORE DATABASE northwind FROM nwindbacdiff WITH NORECOVERY |
When you restore from a transaction log backup, SQL Server reapplies changes to the database that are recorded in the transaction log.
You will typically restore transaction logs as a means of applying changes made to the database since the last complete database or differential backup. In addition, you can restore transaction logs to recover a database up to a specific point in time.
Although restoring a differential backup may speed up the restore process, you may have to restore additional transaction log backups that were created after a differential backup, to ensure data consistency.
Before you restore any transaction logs, you first must restore the complete database backup, specifying the NORECOVERY option. When you have multiple transaction logs to apply, specify the NORECOVERY option for all transaction logs except the last one. This causes SQL Server to suspend the recovery process until the last transaction log is restored.
The syntax for the RESTORE LOGstatement is as follows:
RESTORE LOG {database_name | @database_name_var} [FROM <backup_device> [, ...n]] [WITH [[,] {NORECOVERY | RECOVERY | STANDBY = undo_file_name}] [[,] STOPAT = {date_time | @date_time_var}] |
NOTE
The RESTORE LOG statement replaces the LOAD TRANsaction statement found in previous versions of SQL Server. LOAD TRANsaction is still supported for backward compatibility only. You should use the RESTORE LOG statement.
The following example assumes that a complete database backup exists on the nwindbac named backup device and that two transaction log backups exist on the nwindbaclog named backup device. Three separate restore operations are performed to ensure database consistency.
USE master RESTORE DATABASE northwind FROM nwindbac WITH NORECOVERY |
USE master RESTORE LOG northwind FROM nwindbaclog WITH FILE = 1, STATS, NORECOVERY |
USE master RESTORE LOG northwind FROM nwindbaclog WITH FILE = 2, RECOVERY |
When you restore transaction logs, you can restore to a specific point in time by using the STOPAT option. The following are some guidelines for using this option:
For example, if you know that a malicious update to a database occurred at 11:00 a.m., you can restore the changes in the transaction log through 10:59 a.m. and not apply any changes that occurred after that point.
NOTE
The STOPAT option can be specified only when restoring transaction log backups; it cannot be used with complete or differential database backups, which are taken as a snapshot of a database at a particular time. Transaction logs record individual changes over time; transaction log backups can therefore be used to restore changes up to a particular time.
The following example assumes that a complete database backup was made to the nwindbac named backup device at 8:00 p.m. on January 2, 1998. In addition, two transaction log backups were made at 10:00 a.m. and 1:00 p.m. on January 3, 1998, to the nwindbaclog named backup device. Only changes that occurred before 11:00 a.m. on January 3, 1998, must be restored. Three separate restore operations are performed to ensure database consistency:
USE master RESTORE DATABASE northwind FROM nwindbac WITH NORECOVERY |
USE master RESTORE LOG northwind FROM nwindbaclog WITH FILE = 1, NORECOVERY |
USE master RESTORE LOG northwind FROM nwindbaclog WITH FILE = 2, RECOVERY, STOPAT = 'January 3, 1998 11:00 AM' |
You can restore from a file or filegroup backup to reduce the time required to restore part of a very large database. Restore from a file or filegroup when a particular file was accidentally deleted or damaged.
When you restore from a file or filegroup, consider the following:
The syntax for the RESTORE DATABASE statement is as follows:
RESTORE DATABASE {database_name | @database_name_var} <file_or_filegroup> [, ...m] [FROM <backup_device> [, ...n]] |
|
The following example assumes that a database exists in three files: Nwind1, Nwind2, and Nwind3. The Nwind2 database file contains a single table and its related indexes. The Nwind2 database file was backed up onto the Nwind2bac backup device. One transaction log backup was performed after the Nwind2 file was last backed up. Nwind2 must be restored because the physical medium is damaged. The restore consists of two steps to ensure database consistency:
USE master RESTORE DATABASE northwind FILE = Nwind2 FROM Nwind2bac WITH NORECOVERY |
USE master RESTORE LOG northwind FROM nwindbaclog WITH RECOVERY |
In the following series of exercises, you will first perform a number of data modifications and backups of the nwcopy database. Then you will simulate a media failure and restore the database from your backups.
In this exercise, you will execute a script that makes a complete database backup of the nwcopy database to the nwc3 named backup device. This backup is the baseline for the restore operation later.
Open a query window, open C:\Sqladmin\Exercise\Ch10\Compback.sql, review its contents, and then execute it. This script backs up the nwcopy database to the nwc3 named backup device.
In this exercise, you will execute a script that adds a customer to the Customers table and confirms that the customer was added. Then you will execute another script that backs up the transaction log to the nwchange named backup device.
This script adds the Health Food Store as a customer to the Customers table and queries the table to return the new customer.
This script backs up the transaction log of the nwcopy database to the nwchange named backup device.
In this exercise, you will execute a script that adds another customer to the Customers table and returns that customer to confirm that the customer was added. You will then execute another script that performs a differential backup and appends it to the nwchange named backup device.
This script adds the Volcano Coffee Company to the Customers table and queries the table to return the new customer.
This script performs a differential backup to capture all changes since the last complete database backup. The differential backup is appended to the nwchange named backup device.
In this exercise, you will execute a script that adds a third customer to the nwcopy database and confirms that the customer was added.
This script adds The Wine Cellar as a customer to the Customers table and queries the table to return the new customer.
NOTE
The remaining exercises simulate a media failure and recovery from the failure. Note that at this stage you have backups of all modifications except the addition of the third customer. You will simulate the media failure before backing up that modification, demonstrating SQL Server's ability to back up transactions after a media failure.
In this exercise, you will simulate damage to the medium that stores the nwcopy database.
You should find an information message stating that there was a device activation error for the C:\Mssql7\Data\Nwcopy_data2.ndf file.
What should you do to restore and recover the nwcopy database?
Answer
In this exercise, you will execute a script that performs a transaction log backup after the simulated failure of the nwcopy database. The backup is appended to the nwchange named backup device.
NOTE
Backup of the transaction log after failure is possible only if the primary data and the transaction log files are intact.
This script uses the NO_TRUNCATE option to back up the transaction log of the nwcopy database when the database is not available.
In this exercise, you will use SQL Server Enterprise Manager to examine the contents and creation date of all nwcopy database backups.
What does the nwc3 device contain?
Answer
What does the nwchange device contain?
Answer
In this exercise, you will review the restore strategy suggested by SQL Server Enterprise Manager and determine whether it is appropriate.
Notice that four backup sets are listed. SQL Server automatically selects the most recent complete database backup and the corresponding differential and/or transaction log backup sets that should be restored to return the database to a consistent state. Three out of four backups are selected (full database, differential, and one transaction log).
Do you agree that the selected backups should be restored?
Answer
Why is the first transaction log backup not selected?
Answer
In Exercises 9, 10, and 11, instead of simply restoring the full database as suggested by SQL Server Enterprise Manager, you will restore the different backup sets individually. This is done so that you can see and understand the effect of each restore.
In this exercise, you will use SQL Server Enterprise Manager to restore the complete database and differential backups and allow access to the database after the restore process is complete.
In this exercise you will execute a script that lists the new customers in the Customers table in order to evaluate the restore process.
This script determines whether the three new customers that were previously added to the Customers table were recovered.
Have all three new customers been recovered?
Answer
IMPORTANT
You must close the query window or select another database in this and any other open query windows in order to complete the Exercise 11 in this Lesson. The restore operation requires that no users be using the database.
In this exercise, you will use SQL Server Enterprise Manager to restore the transaction log and then allow access to the database after the restore process is complete.
When restoring databases, you should obtain information about the backups that you plan to restore. Make sure that the files are valid and contain all of the backups that are required to restore the database to a consistent state. Use the NORECOVERY option if you have additional backups that must be restored. Use the RECOVERY option on the last backup to return the database to a consistent state.