SQL Server Documentation

Database Engine - Backup and Restore

Restoring Your Database

This document provides a comprehensive guide to restoring your SQL Server databases. Proper backup and restore procedures are critical for data protection and disaster recovery.

Understanding Restore Scenarios

Restoring a database can be performed in several scenarios:

Methods for Restoring Databases

You can restore databases using SQL Server Management Studio (SSMS) or Transact-SQL (T-SQL) commands.

Using SQL Server Management Studio (SSMS)

SSMS offers a user-friendly interface for performing restore operations.

  1. Connect to your SQL Server instance in SSMS.
  2. Right-click on the Databases node and select Restore Database....
  3. In the Restore Database dialog box:
    • Select the Source: Choose Device and browse to your backup file, or select URL for Azure Blob Storage.
    • Choose the Backup set to restore.
    • Under Destination, select the Database to restore to. You can restore to a new database or overwrite an existing one.
    • Navigate to the Files page to verify or change the physical file locations if restoring to a different server or configuration.
    • Review the Options page for settings like overwriting existing databases and recovery states.
  4. Click OK to start the restore process.

Using Transact-SQL (T-SQL)

T-SQL provides granular control over restore operations.

Restoring a Full Database Backup

This is the basic command to restore a full backup. Ensure the database does not exist or use the WITH REPLACE option to overwrite it.

RESTORE DATABASE YourDatabaseName
FROM DISK = 'C:\Backups\YourDatabaseName_Full.bak'
WITH NORECOVERY; -- Or RECOVERY if this is the only backup
GO
Restoring a Transaction Log Backup

This command restores a transaction log backup after a full backup.

RESTORE LOG YourDatabaseName
FROM DISK = 'C:\Backups\YourDatabaseName_Log.trn'
WITH NORECOVERY;
GO
Restoring to a Specific Point in Time

Use the STOPAT clause to restore to a precise moment.

RESTORE DATABASE YourDatabaseName
FROM DISK = 'C:\Backups\YourDatabaseName_Full.bak'
WITH NORECOVERY,
     STOPAT = '2023-10-27 10:30:00';
GO

RESTORE LOG YourDatabaseName
FROM DISK = 'C:\Backups\YourDatabaseName_Log.trn'
WITH RECOVERY; -- Use RECOVERY when the last log backup is applied
GO
Tip: Always ensure your backup files are accessible and intact before initiating a restore. Test your restore process regularly.

Recovery States

Understanding recovery states is crucial for a successful restore, especially when applying multiple backups.

Note: If you are restoring a database that contains the objects that are used by the database, such as system databases, you must restart SQL Server after the restore is complete.

Common Restore Options

Troubleshooting Restore Issues