Restore Overview

This document provides a comprehensive overview of restoring SQL Server databases. Restoring a database involves retrieving a saved copy of the database and applying it to a server instance. This is a critical operation for data recovery, disaster preparedness, and migrating databases.

What is Database Restore?

Restoring a database is the process of re-creating a database from a backup. When a database is backed up, its data and transaction log are saved to a backup file or set of files. To restore the database, SQL Server reads these backup files and applies the data to create a new instance of the database, which can then be updated to the point in time represented by the backup.

Key Concepts

  • Backup File: The file that contains the backed-up data and/or transaction log.
  • Restore Operation: The process of applying a backup to a database.
  • Point-in-Time Restore: The ability to restore a database to a specific point in time, usually by applying transaction log backups after a full or differential backup.
  • Recovery Model: Determines how transactions are logged, which affects the available restore options (Simple, Full, Bulk-Logged).
  • RESTORE Statement: The Transact-SQL command used to perform restore operations.

When to Restore a Database

  • Data Loss: Recovering from accidental data deletion or corruption.
  • Disaster Recovery: Rebuilding a database after a hardware failure or site disaster.
  • Database Migration: Moving a database to a different server or environment.
  • Testing: Creating a copy of a production database for testing purposes.
  • Point-in-Time Recovery: Reverting specific transactions or recovering from a particular error.
Important: Ensure you have appropriate backup files (full, differential, and transaction log backups, as needed) before attempting a restore operation. The recovery model of the database greatly influences the types of backups you can take and use for restores.

Common Restore Scenarios

Restoring a Full Backup

This is the most basic restore operation, where you restore a full database backup. This brings the database to the state it was in when the full backup was taken.

RESTORE DATABASE YourDatabaseName
FROM DISK = 'C:\Backups\YourDatabaseName_Full.bak';

Restoring with Transaction Log Backups (Point-in-Time Restore)

To perform a point-in-time restore, you first restore a full backup, then a differential backup (if available), and then one or more transaction log backups in sequence. The database is left in a RESTORE-PENDING state until the final log backup is applied with the RECOVERY option.

-- Restore Full Backup
RESTORE DATABASE YourDatabaseName
FROM DISK = 'C:\Backups\YourDatabaseName_Full.bak'
WITH NORECOVERY;

-- Restore Differential Backup (if applicable)
RESTORE DATABASE YourDatabaseName
FROM DISK = 'C:\Backups\YourDatabaseName_Diff.bak'
WITH NORECOVERY;

-- Restore Transaction Log Backups up to the desired point
RESTORE LOG YourDatabaseName
FROM DISK = 'C:\Backups\YourDatabaseName_Log1.trn'
WITH NORECOVERY;

RESTORE LOG YourDatabaseName
FROM DISK = 'C:\Backups\YourDatabaseName_Log2.trn'
WITH RECOVERY; -- Apply the last log backup and bring the database online

Restoring to a Different Server or as a Different Database Name

You can restore a backup to a different server instance or give the restored database a new name using the WITH MOVE option to specify new file locations.

RESTORE DATABASE NewDatabaseName
FROM DISK = 'C:\Backups\YourDatabaseName_Full.bak'
WITH MOVE 'LogicalDataFileName' TO 'D:\Data\NewDatabaseName.mdf',
     MOVE 'LogicalLogFileName' TO 'E:\Logs\NewDatabaseName_log.ldf',
     NORECOVERY;

Further Reading