Restoring Databases
This document provides comprehensive guidance on restoring databases in SQL Server, covering various scenarios, options, and best practices to ensure data integrity and minimal downtime.
On This Page
Introduction
Restoring a database is a critical operation for disaster recovery, point-in-time recovery, and migrating data. SQL Server offers robust tools and options to manage database restores effectively.
Common Restore Scenarios
- Full Database Restore: Restoring the entire database from a full backup.
- Point-in-Time Restore: Restoring a database to a specific point in time using a full backup, followed by differential and transaction log backups.
- Restoring to a Different Server: Migrating or recovering a database on a new SQL Server instance.
- Restoring System Databases: Special procedures for restoring
master,model,msdb, andtempdb. - Restoring from a Corrupted Database: Techniques to recover data when the primary database is inaccessible.
Key Restore Options
Understanding these options is crucial for successful restores:
WITH NORECOVERY: Leaves the database in a restoring state, allowing subsequent log backups to be applied. This is essential for point-in-time restores.WITH RECOVERY: Rolls back any uncommitted transactions and makes the database available for use. This is the default when restoring a single full backup.WITH REPLACE: Overwrites an existing database with the same name. Use with extreme caution.WITH RESTART: Resumes a previous restore operation.MOVE 'logical_file_name' TO 'physical_file_path': Allows you to specify new file locations for data and log files during restore.
Using Transact-SQL
The primary command for restoring databases is RESTORE DATABASE. Here's a basic example of a full restore:
RESTORE DATABASE YourDatabaseName
FROM DISK = 'C:\Backups\YourDatabaseName_Full.bak'
WITH RECOVERY, REPLACE;
GO
For a point-in-time restore, you would apply the full, differential, and then transaction log backups sequentially:
-- Restore Full Backup (No Recovery)
RESTORE DATABASE YourDatabaseName
FROM DISK = 'C:\Backups\YourDatabaseName_Full.bak'
WITH NORECOVERY;
GO
-- Restore Differential Backup (No Recovery)
RESTORE DATABASE YourDatabaseName
FROM DISK = 'C:\Backups\YourDatabaseName_Diff.bak'
WITH NORECOVERY;
GO
-- Restore Transaction Log Backup to a specific point
RESTORE LOG YourDatabaseName
FROM DISK = 'C:\Backups\YourDatabaseName_Log1.trn'
WITH STOPAT = '2023-10-27 10:30:00', NORECOVERY;
GO
-- Final Restore (Recovery)
RESTORE DATABASE YourDatabaseName
WITH RECOVERY;
GO
Using SQL Server Management Studio (SSMS)
SSMS provides a graphical interface for performing restores:
- In SSMS, connect to your SQL Server instance.
- Right-click on "Databases" and select "Restore Database...".
- In the "Restore Database" dialog, choose "Device" and select the backup file(s).
- Select the target database. If it doesn't exist, you'll need to specify a new name. If it exists, you'll likely want to check "Overwrite the existing database (WITH REPLACE)".
- Navigate to the "Files" page to review and adjust file locations using the "Relocate all files to folder" option or by moving individual files using the `MOVE` option.
- Go to the "Options" page to select recovery states (
RECOVERY,NORECOVERY,STANDBY) and other settings. - Click "OK" to start the restore process.
WITH REPLACE or restoring system databases.
Advanced Topics
- Restoring to a different location: Use the
MOVEoption in T-SQL or the "Files" page in SSMS to specify new physical paths for the database's data and log files. - Restoring Over a Running Database: Requires the
WITH REPLACEoption. Be extremely careful with this. - Partial Restores: Restoring only specific filegroups of a database.
STANDBYMode: Keeps the database available for read-only access while allowing transaction log restores to continue.
Troubleshooting Common Issues
- "The tail of the log for the database has already been backed up..." This error occurs when you try to restore a full backup without
WITH NORECOVERY, or when the log has changed since the last backup. Ensure you've taken a final log backup if the database was active. - "RESTORE DATABASE is terminating abnormally because of an error." Check the SQL Server error logs for more specific details. Common causes include disk space issues, incorrect backup file paths, or permission problems.
- File path errors: Ensure the specified file paths for data and log files exist on the destination server and that the SQL Server service account has the necessary permissions.
For detailed information on specific backup and restore commands, refer to the official SQL Server documentation for your version.