SQL Server Administration Guide

Comprehensive Documentation for Database Administrators

Backup and Restore

This section covers essential strategies and procedures for backing up and restoring your SQL Server databases, ensuring data integrity and business continuity.

Why Backup and Restore?

Regular backups are critical for protecting your data against various threats, including:

A well-defined restore strategy ensures you can quickly recover your databases to a consistent state with minimal data loss.

Backup Types

SQL Server supports several types of backups:

Backup Strategies

Choosing the right backup strategy depends on factors like database size, recovery point objective (RPO), and recovery time objective (RTO).

Example Strategy (Hybrid Approach)

Note: Regularly test your restore procedures to ensure they work correctly and to estimate recovery times.

Performing Backups

Backups can be performed using SQL Server Management Studio (SSMS) or Transact-SQL (T-SQL).

Using SSMS:

  1. In Object Explorer, right-click the database you want to back up.
  2. Navigate to Tasks > Back Up....
  3. Configure the backup type, destination, and options.

Using T-SQL:

Full Backup:

BACKUP DATABASE [YourDatabaseName]
TO DISK = N'C:\Backup\YourDatabaseName_Full.bak'
WITH NOFORMAT, NOINIT,  NAME = N'YourDatabaseName-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10;
GO

Differential Backup:

BACKUP DATABASE [YourDatabaseName]
TO DISK = N'C:\Backup\YourDatabaseName_Diff.bak'
WITH DIFFERENTIAL, NOFORMAT, NOINIT,  NAME = N'YourDatabaseName-Differential Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10;
GO

Transaction Log Backup:

BACKUP LOG [YourDatabaseName]
TO DISK = N'C:\Backup\YourDatabaseName_Log.trn'
WITH NOFORMAT, NOINIT,  NAME = N'YourDatabaseName-Log Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10;
GO

Important: Always store backups on a separate physical storage device or in a remote location from your SQL Server instance to protect against site-wide failures.

Restoring Databases

Restoring a database involves recovering it from a backup file.

Using SSMS:

  1. In Object Explorer, right-click the Databases node.
  2. Select Restore Database....
  3. Choose the backup source (Device or URL) and select your backup file.
  4. Configure restore options, including recovery state.

Using T-SQL:

Restoring from a Full Backup (with NORECOVERY to allow further restores):

RESTORE DATABASE [YourDatabaseName]
FROM DISK = N'C:\Backup\YourDatabaseName_Full.bak'
WITH NORECOVERY,  NOUNLOAD,  REPLACE,  STATS = 5;
GO

Restoring from a Differential Backup (with NORECOVERY):

RESTORE DATABASE [YourDatabaseName]
FROM DISK = N'C:\Backup\YourDatabaseName_Diff.bak'
WITH DIFFERENTIAL, NORECOVERY, NOUNLOAD,  STATS = 5;
GO

Restoring from a Transaction Log Backup (with NORECOVERY):

RESTORE LOG [YourDatabaseName]
FROM DISK = N'C:\Backup\YourDatabaseName_Log.trn'
WITH NORECOVERY, NOUNLOAD,  STATS = 5;
GO

Bringing the Database Online (after all restores are complete):

RESTORE DATABASE [YourDatabaseName] WITH RECOVERY;
GO

Recovery Models

The recovery model of a database significantly impacts its backup and restore capabilities:

For production databases requiring point-in-time recovery, the FULL recovery model is typically recommended.

Best Practices