Restore Overview - SQL Server Database Engine

Last updated: October 26, 2023

This document provides a comprehensive overview of restoring databases in Microsoft SQL Server. Restoring a database is a critical operation for data recovery, disaster preparedness, and database maintenance.

Understanding Restore Operations

Restoring a database involves recovering it from a backup. SQL Server supports various backup and restore scenarios, including:

Key Concepts in Restore Operations

Before performing a restore, it's essential to understand these key concepts:

Common Restore Scenarios

Here are some common scenarios and how they are addressed:

Scenario 1: Restoring a Full Database Backup

This is the simplest form of restore. It involves restoring a full database backup to recover a database to the state it was in when the backup was taken.

RESTORE DATABASE MyDatabase
FROM DISK = 'C:\Backups\MyDatabase_Full.bak'
WITH RECOVERY;

Scenario 2: Restoring a Full Backup and Applying Transaction Log Backups

This scenario is used to restore a database to a point in time after the last full backup. It requires a full backup and a series of transaction log backups.

  1. Restore the full backup with NORECOVERY.
  2. Restore each transaction log backup sequentially with NORECOVERY.
  3. Restore the final transaction log backup with RECOVERY.
-- Step 1: Restore Full Backup
RESTORE DATABASE MyDatabase
FROM DISK = 'C:\Backups\MyDatabase_Full.bak'
WITH NORECOVERY;

-- Step 2: Restore Transaction Log Backups (apply in order)
RESTORE LOG MyDatabase
FROM DISK = 'C:\Backups\MyDatabase_Log1.trn'
WITH NORECOVERY;

RESTORE LOG MyDatabase
FROM DISK = 'C:\Backups\MyDatabase_Log2.trn'
WITH NORECOVERY;

-- Step 3: Restore the final log backup and bring the database online
RESTORE LOG MyDatabase
FROM DISK = 'C:\Backups\MyDatabase_Log3.trn'
WITH RECOVERY;

Scenario 3: Restoring to a Specific Point in Time

This allows you to recover your database to a specific timestamp, useful for undoing accidental data modifications.

RESTORE LOG MyDatabase
FROM DISK = 'C:\Backups\MyDatabase_Log_to_Point.trn'
WITH STOPAT = '2023-10-26 10:30:00', RECOVERY;
Note: The STOPAT clause is only applicable when restoring transaction log backups.

Restore Options and Considerations

Option Description
WITH REPLACE Overwrites an existing database with the same name. Use with caution.
WITH MOVE Specifies new locations for the database files (data and log files) during the restore operation. Essential for moving a database to a new server or changing file locations.
WITH RESTART Restarts the restore operation from the beginning if it was interrupted.
WITH STATS Displays progress messages during the restore operation.

Best Practices for Restoring

For more detailed information on specific restore commands and advanced scenarios, please refer to the related topics in the SQL Server documentation.


Related Topics: