Microsoft SQL Server 7.0 System Administration Training Kit

[Previous] [Next]

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

Estimated lesson time: 90minutes

Restoring from a Complete Database Backup

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

Specifying a Recovery Option

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

Exercise: Creating the nwcopy Database

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.

  1. Log on to your computer as Administrator or as another user that is a member of the local Administrators group
  2. Copy the C:\SQLAdmin\Exercise\Ch10\Nwc1.bak file to C:\Mssql7\Backup on your local hard disk.
  3. Open SQL Server Query Analyzer and log on to the (local) server with Microsoft Windows NT authentication. Your account is a member of the Windows NT Administrators group, which is automatically mapped to the SQL Server sysadmin role.
  4. Open and execute the C:\SQLAdmin\Exercise\Ch10\Setupnwc.sql script installed on your hard disk drive from the Supplemental Course Materials CD-ROM.
  5. This script restores the nwcopy database, which is used in the other exercises in this chapter.

Exercise: Modifying the nwcopy Database

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.

  1. Open a query window, open C:\Sqladmin\Exercise\Ch10\Addprod.sql, review its contents, and then execute it.
  2. This script adds the new product Maple Flavor Pancake Mix to the Products table.

  3. Review the results to confirm that the new row was added.

Exercise: Backing Up the nwcopy Database

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.

Exercise: Simulating Database Damage

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.

  1. Open a query window, open C:\Sqladmin\Exercise\Ch10\Dataloss.sql, review its contents, and then execute it.
  2. This script damages the database by updating all rows in the Products table.

  3. Review the result to confirm that the product Maple Flavor Pancake Mix can no longer be found in the Products table, as its name has been incorrectly changed.
  4. Close the query window.

Exercise: Using SQL Server Enterprise Manager to Restore the nwcopy Database

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.

  1. Open SQL Server Enterprise Manager.
  2. In the console tree, expand the Databases folder.
  3. In the console tree, right-click the nwcopy database icon, and then click Properties.
  4. On the Options tab, check the Single User and DBO Use Only options to restrict access to the database during the restore process.
  5. Click OK to close the dialog box and save your changes to the database options.
  6. In the console tree, right-click the nwcopy database icon, point to All Tasks, and then click Restore Database.
  7. In the Restore Database dialog box, review the automatic selections:
    • On the General tab, Database is selected for Restore. This allows a complete database backup and any associated differential or transaction log backups to be restored.
    • The First Backup To Restore option is set to the last complete backup of the nwcopy database. You can change this selection to restore from older backups when necessary. The backup history listed here reflects entries in the msdb database. If the backup set has since been overwritten, you will not be able to restore it.
    • In the list of backup sets, the backup set you made previously, called nwcopy-Complete, with a filename of C:\Mssql7\Backup\Nwc2.bak, is checked.

  8. On the Options tab, check Leave Database Operational. No Additional Transaction Logs Can Be Restored, as you have no transaction log backups to restore. This corresponds to the RECOVERY option of the RESTORE statement.
  9. Click OK to perform the restore.

Exercise: Confirming Data Recovery

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.

Restoring from a Differential Backup

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

Restoring from a Transaction Log Backup

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.

Considerations for Restoring Transaction Logs

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.

  1. The first step restores from a complete database backup without recovering the database.
  2. USE master RESTORE DATABASE northwind FROM nwindbac WITH NORECOVERY

  3. The second step restores the first transaction log without recovering the database. The progress of the restore process is displayed.
  4. USE master RESTORE LOG northwind FROM nwindbaclog WITH FILE = 1, STATS, NORECOVERY

  5. The third step restores the second transaction log. The RECOVERY option returns the Northwind database to a consistent state, rolling forward any committed transactions and rolling back any uncommitted transactions:
  6. USE master RESTORE LOG northwind FROM nwindbaclog WITH FILE = 2, RECOVERY

Restoring to a Specific Point in Time

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:

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:

  1. The first step restores a database from a complete database backup without recovering the database.
  2. USE master RESTORE DATABASE northwind FROM nwindbac WITH NORECOVERY

  3. The second step restores the first transaction log without recovering the database.
  4. USE master RESTORE LOG northwind FROM nwindbaclog WITH FILE = 1, NORECOVERY

  5. The third step restores the second transaction log, applies changes that occurred before 11:00 a.m. on January 3, 1998, and recovers the database.
  6. USE master RESTORE LOG northwind FROM nwindbaclog WITH FILE = 2, RECOVERY, STOPAT = 'January 3, 1998 11:00 AM'

Restoring from a File or Filegroup Backup

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]]

where <file_or_filegroup> is

{FILE = logical_file_name | FILEGROUP = logical_filegroup_name}

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:

  1. The first step restores the backup of the Nwind2 database file without rolling forward any committed transactions or rolling back any uncommitted transactions.
  2. USE master RESTORE DATABASE northwind FILE = Nwind2 FROM Nwind2bac WITH NORECOVERY

  3. The second step restores the transaction log backup. The RECOVERY option returns the Northwind database to a consistent state, rolling forward any committed transactions and rolling back any uncommitted transactions:
  4. USE master RESTORE LOG northwind FROM nwindbaclog WITH RECOVERY

A Practical Example

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.

Exercise 1: Backing Up the nwcopy Database

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.

Exercise 2: Modifying the nwcopy Database and Backing Up the Transaction Log

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.

  1. Open a query window, open C:\SQLAdmin\Exercise\Ch10\Addcust1.sql, review its contents, and then execute it.
  2. This script adds the Health Food Store as a customer to the Customers table and queries the table to return the new customer.

  3. Open a query window, open C:\SQLAdmin\Exercise\Ch10\Logback1.sql, review its contents, and then execute it.
  4. This script backs up the transaction log of the nwcopy database to the nwchange named backup device.

Exercise 3: Modifying the nwcopy Database and Performing a Differential Backup

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.

  1. Open a query window, open C:\SQLAdmin\Exercise\Ch10\Addcust2.sql, review its contents, and then execute it.
  2. This script adds the Volcano Coffee Company to the Customers table and queries the table to return the new customer.

  3. Open a query window, open C:\SQLAdmin\Exercise\Ch10\Diffback.sql, review its contents, and then execute it.
  4. 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.

Exercise 4: Modifying the nwcopy Database

In this exercise, you will execute a script that adds a third customer to the nwcopy database and confirms that the customer was added.

  1. Open a query window, open C:\SQLAdmin\Exercise\Ch10\Addcust3.sql, review its contents, and then execute it.
  2. 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.

Exercise 5: Simulating Database Damage

In this exercise, you will simulate damage to the medium that stores the nwcopy database.

  1. Switch to SQL Server Enterprise Manager, and then exit.
  2. Open SQL Server Service Manager, and then stop the SQL Server service.
  3. Use Windows NT Explorer to rename the secondary data file for the nwcopy database from C:\Mssql7\Data\Nwcopy_data2.ndf to Nwcopy_data2.bad. Be careful not to change the primary data file, C:\Mssql7\Data\Nwcopy_data.mdf, or the transaction log file, C:\Mssql7\Data\Nwcopy_log.ldf.
  4. Restart the SQL Server service.
  5. Open SQL Server Enterprise Manager.
  6. In the console tree, expand the Databases folder, and then click the nwcopy database icon.
  7. If the Details pane shows database object icons, right-click the nwcopy database icon, point to View, and then click Taskpad.
  8. SQL Server displays an error message stating that an error occurred while trying to access the database information.
  9. Open Windows NT Event Viewer and examine the contents of the Application Log.
  10. 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

Exercise 6: Performing a Transaction Log Backup of the nwcopy Database

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.

  1. Open a query window.
  2. Open C:\Sqladmin\Exercise\Ch10\Logback2.sql, review its contents, and then execute it.
  3. This script uses the NO_TRUNCATE option to back up the transaction log of the nwcopy database when the database is not available.

Exercise 7: Examining the nwcopy Backups

In this exercise, you will use SQL Server Enterprise Manager to examine the contents and creation date of all nwcopy database backups.

  1. Switch to SQL Server Enterprise Manager
  2. In the console tree, expand the Management folder, and then click Backup.
  3. In the details pane, right-click the nwc3 device, and then click Properties.
  4. Click View Contents to examine the contents of the nwc3 device. Notice the type, description, and date and time of the backup set on the device.
  5. What does the nwc3 device contain?

    Answer

  6. Click Close to close the Backup Media Contents dialog box. Click Cancel to close the Backup Device Properties dialog box.
  7. Repeat steps 3, 4, and 5 to examine the contents of the nwchange device. Notice the type, description, and date and time of each backup set on the device.
  8. What does the nwchange device contain?

    Answer

Exercise 8: Reviewing the Restore Strategy

In this exercise, you will review the restore strategy suggested by SQL Server Enterprise Manager and determine whether it is appropriate.

  1. In the console tree, right-click the nwcopy database icon, point to All Tasks, and then click Restore Database.
  2. The Restore Database dialog box appears. Verify that the nwcopy database is selected in the Restore As Database list.
  3. 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

  4. Click Cancel to close the Restore Database dialog box.

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.

Exercise 9: Restoring the Complete and Differential Backups

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.

  1. Open SQL Server Enterprise Manager.
  2. In the console tree, expand the Databases folder.
  3. In the console tree, right-click the nwcopy database icon, point to All Tasks, and then click Restore Database.
  4. In the Restore Database dialog box, review the automatic selections:
    • On the General tab, Database is selected for Restore. This allows a complete database backup and any associated differential or transaction log backups to be restored.
    • In the Restore list, you should see the four backups you made. The first (complete), third (differential), and fourth (transaction log) backups are checked. To restore only the complete and differential database backup sets, uncheck the third checked backup set (nwcopy-Log2) to deselect it.

  5. On the Options tab, click Leave Database Read-Only And Able To Restore Additional Transaction Logs, as you will restore the transaction log backups later. This corresponds to the RESTORE statement STANDBY option.
  6. Check both Prompt Before Restoring Each Backup and Force Restore Over Existing Database.
  7. Click OK to restore the complete database backup.
  8. Click OK to restore the differential database backup.

Exercise 10: Examining the Contents of the Database

In this exercise you will execute a script that lists the new customers in the Customers table in order to evaluate the restore process.

  1. Open a query window, open C:\Sqladmin\Exercise\Ch10\Listcust.sql, review its contents, and then execute it.
  2. 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

  3. Close the query window.

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.

Exercise 11: Restoring the Transaction Log Backup

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.

  1. Switch to SQL Server Enterprise Manager.
  2. Expand the Databases folder.
  3. In the console tree, right-click the nwcopy database icon, point to All Tasks, and then click Restore Database.
  4. In the Restore Database dialog box, on the General tab, click the From Device option in the Restore section.
  5. Click the Select Devices button.
  6. In the Choose Restore Devices dialog box, click Disk for the Restore From option if it is not selected.
  7. Click Add to add the backup device to the Restore From list.
  8. In the Choose Restore Destination dialog box, click Backup Device.
  9. In the Backup Device drop-down list, select nwchange. Click OK to accept the nwchange device.
  10. If there are any devices other than nwchange in the Restore From list, highlight them and click Remove to remove them.
  11. Click OK to accept nwchange as the device from which to restore.
  12. Click the View Contents button to select a backup set from the nwchange device.
  13. Check the check box next to the nwcopy-Log2 backup set. This is the transaction log backup taken after the simulated database failure. Click OK to accept the selected backup set. Note that the Backup number is now set to 3; this corresponds to selecting the third backup set with the FILE option of the RESTORE statement.
  14. Click the Restore Backup Set option and the Transaction Log option if they are not selected.
  15. On the Options tab, click Leave Database Operational. No Additional Transaction Logs Can Be Restored, as you have no more transaction log backups to restore. This corresponds to the RECOVERY option of the RESTORE statement.
  16. Click OK to perform the restore.
  17. Switch to SQL Server Query Analyzer. Open a query window, open C:\SQLAdmin\Exercise\Ch10\Listcust.sql, and execute it. All three new customer records are now present, indicating a successful restore of transactions backed up after a simulated database failure.

Lesson Summary

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.

Категории