Administration: Backup and Restore
Database backup and restore operations are critical components of any data management strategy. They provide a mechanism to protect your data against loss due to hardware failures, software corruption, accidental deletions, or malicious attacks.
SQL Server offers robust features for creating backups and restoring databases to a consistent state. Understanding these features, the different types of backups, and the restore process is essential for database administrators.
Backup: A copy of a database, or part of a database, that you can use to restore your database in case of data loss.
Restore: The process of recreating a database from a backup.
Recovery Model: A database property that controls the transaction log behavior, affecting backup and restore capabilities.
SQL Server supports several types of database backups, each serving a different purpose in your backup strategy.
A full database backup contains all the data and transaction log records necessary to recover the entire database. It represents a complete snapshot of the database at the time the backup was taken.
Full backups are the foundation of any backup strategy. All other backup types are based on a full backup.
A differential backup backs up only the data that has changed since the last full backup. This can significantly reduce backup time and storage space compared to full backups.
To restore a database using differential backups, you first need to restore the last full backup, followed by the last differential backup.
Transaction log backups capture the transaction log records that have been generated since the last log backup. These are only possible for databases in the Full or Bulk-Logged recovery models.
Transaction log backups are crucial for point-in-time recovery, allowing you to restore a database to a specific moment in time. They also help in truncating the transaction log, preventing it from growing indefinitely.
You can perform database backups using SQL Server Management Studio (SSMS) or Transact-SQL (T-SQL) commands.
C:\Backups\MyDatabase_Full.bak
).The general syntax for performing a backup using T-SQL is as follows:
BACKUP DATABASE [DatabaseName]
TO DISK = N'FilePath\FileName.bak'
WITH NOFORMAT, NOINIT, NAME = N'DatabaseName-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
For different backup types, modify the WITH
clause:
BACKUP DATABASE [DatabaseName]
TO DISK = N'FilePath\FileName_Diff.bak'
WITH DIFFERENTIAL, NOFORMAT, NOINIT, NAME = N'DatabaseName-Differential Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
BACKUP LOG [DatabaseName]
TO DISK = N'FilePath\FileName_Log.trn'
WITH NOFORMAT, NOINIT, NAME = N'DatabaseName-Transaction Log Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
Restoring a database is the process of retrieving it from a backup. This is a critical operation that can be performed using SSMS or T-SQL.
When restoring a database, you'll typically need to consider:
WITH RECOVERY
), ready for subsequent restores (WITH NORECOVERY
), or ready for rollback (WITH ROLLBACK ONLY
). WITH NORECOVERY
is commonly used when applying multiple backups (e.g., full, differential, and log)....
) to select your backup file.The general syntax for restoring a database using T-SQL:
RESTORE DATABASE [DatabaseName]
FROM DISK = N'FilePath\FileName.bak'
WITH FILE = 1, -- Use the first backup set in the file
MOVE N'LogicalDataFileName' TO N'PhysicalDataFilePath.mdf',
MOVE N'LogicalLogFileName' TO N'PhysicalLogFilePath.ldf',
NOUNLOAD, REPLACE, STATS = 5
GO
Important Notes for T-SQL Restore:
[DatabaseName]
with your desired database name.FilePath\FileName.bak
with the path to your backup file.MOVE
options for both data and log files, mapping the logical names from the backup to their desired physical locations. You can find logical names using RESTORE FILELISTONLY FROM DISK = 'FilePath\FileName.bak'
.REPLACE
if restoring over an existing database.NORECOVERY
in all intermediate restores and RECOVERY
in the final restore.-- Example: Restoring a Full backup WITH NORECOVERY
RESTORE DATABASE [DatabaseName]
FROM DISK = N'C:\Backups\MyDatabase_Full.bak'
WITH MOVE N'MyDatabase_Data' TO N'C:\Program Files\Microsoft SQL Server\MSSQLXX\MSSQL\DATA\MyDatabase.mdf',
MOVE N'MyDatabase_Log' TO N'C:\Program Files\Microsoft SQL Server\MSSQLXX\MSSQL\DATA\MyDatabase_log.ldf',
NORECOVERY, REPLACE, STATS = 10
GO
-- Example: Restoring a Differential backup WITH NORECOVERY
RESTORE DATABASE [DatabaseName]
FROM DISK = N'C:\Backups\MyDatabase_Diff.bak'
WITH MOVE N'MyDatabase_Data' TO N'C:\Program Files\Microsoft SQL Server\MSSQLXX\MSSQL\DATA\MyDatabase.mdf',
MOVE N'MyDatabase_Log' TO N'C:\Program Files\Microsoft SQL Server\MSSQLXX\MSSQL\DATA\MyDatabase_log.ldf',
NORECOVERY, REPLACE, STATS = 10
GO
-- Example: Restoring a Transaction Log backup WITH RECOVERY (last in sequence)
RESTORE LOG [DatabaseName]
FROM DISK = N'C:\Backups\MyDatabase_Log.trn'
WITH RECOVERY, STATS = 10
GO
The recovery model of a database determines how transaction log records are managed, which directly impacts backup and restore capabilities, especially point-in-time recovery.
In the Full recovery model, all transactions are logged. This allows for complete recovery of the database, including point-in-time recovery. Transaction log backups are essential to manage log space and enable recovery.
Use Case: Critical production databases where minimizing data loss is paramount.
The Bulk-Logged recovery model is a hybrid. It logs most operations fully but logs certain bulk operations (like bulk inserts, `SELECT INTO`, `ALTER INDEX`) minimally. This can improve performance for bulk operations.
Point-in-time recovery is possible, but the transaction log backups might not contain all the detailed information for every transaction if bulk operations occurred.
Use Case: Databases with frequent large bulk operations where performance is a concern, but point-in-time recovery is still needed.
In the Simple recovery model, transaction logs are automatically truncated (cleared) after transactions are committed and data is written to the data files. This means only full and differential backups can be performed. Point-in-time recovery is not possible.
Use Case: Development or test databases, or databases where data loss is acceptable and point-in-time recovery is not a requirement.
You can change the recovery model using SSMS (Database Properties > Options) or T-SQL:
-- For Full Recovery Model
ALTER DATABASE [DatabaseName] SET RECOVERY FULL;
-- For Simple Recovery Model
ALTER DATABASE [DatabaseName] SET RECOVERY SIMPLE;
Note: When switching from Full or Bulk-Logged to Simple, ensure you have a recent full backup, as the transaction log will be truncated.
RESTORE VERIFYONLY
command to check the integrity of a backup file without actually restoring it.RESTORE VERIFYONLY
on other backup files to see if the corruption is isolated or widespread. If a backup file is corrupt, you may need to resort to the last known good backup.