Backup & Restore Overview
This guide provides a comprehensive reference for backing up and restoring Microsoft SQL Server databases. It covers backup types, best practices, T‑SQL commands, PowerShell scripts, and common troubleshooting scenarios.
Key concepts:
- Full backup – captures the entire database at a point in time.
- Differential backup – records changes since the last full backup.
- Transaction log backup – preserves log records to enable point‑in‑time recovery.
- Copy‑only backup – creates a backup without affecting the backup chain.
Backup Types
Backup Type | Description | Typical Use‑Case |
---|---|---|
Full | Complete copy of a database. | Weekly baseline, nightly for critical DBs. |
Differential | Changes since last full backup. | Mid‑day backups to reduce restore time. |
Log | Active transaction log records. | Point‑in‑time recovery, every 15‑30 mins. |
Copy‑only | Independent backup; does not truncate logs. | Ad‑hoc backups for testing. |
File/Filegroup | Selective file or filegroup backup. | Large databases with rarely‑used filegroups. |
Partial | Backup of primary filegroup + read‑only groups. | Large read‑only data warehouses. |
T‑SQL Backup Examples
Full Backup
BACKUP DATABASE [AdventureWorks]
TO DISK = N'C:\Backups\AdventureWorks_Full_2025_09_13.bak'
WITH INIT,
NAME = N'AdventureWorks-Full Backup',
DESCRIPTION = N'Full backup of AdventureWorks on 2025‑09‑13',
COMPRESSION,
STATS = 10;
Differential Backup
BACKUP DATABASE [AdventureWorks]
TO DISK = N'C:\Backups\AdventureWorks_Diff_2025_09_13.bak'
WITH DIFFERENTIAL,
NAME = N'AdventureWorks-Diff Backup',
DESCRIPTION = N'Differential backup after full backup',
STATS = 10;
Transaction Log Backup
BACKUP LOG [AdventureWorks]
TO DISK = N'C:\Backups\AdventureWorks_Log_2025_09_13.trn'
WITH NORECOVERY,
NAME = N'AdventureWorks-Log Backup',
DESCRIPTION = N'Log backup for point‑in‑time restore',
STATS = 10;
PowerShell Backup Example
# Requires SqlServer module
Import-Module SqlServer
$server = 'SQL01\MAIN'
$database = 'AdventureWorks'
$path = "C:\Backups\$($database)_Full_$(Get-Date -Format 'yyyyMMdd_HHmm').bak"
Backup-SqlDatabase -ServerInstance $server `
-Database $database `
-BackupFile $path `
-CompressionOption On `
-Initialize `
-BackupAction Database `
-Verbose
Write-Host "Backup completed: $path"
Restoring a Database
Restore steps typically follow this order:
- Restore the most recent full backup with
WITH NORECOVERY
. - Apply one or more differential backups (if any) also with
WITH NORECOVERY
. - Apply all required log backups in chronological order, ending with
WITH RECOVERY
orWITH STANDBY
for a read‑only state.
Full Restore Example
RESTORE DATABASE [AdventureWorks]
FROM DISK = N'C:\Backups\AdventureWorks_Full_2025_09_13.bak'
WITH MOVE N'AdventureWorks_Data' TO N'D:\Data\AdventureWorks.mdf',
MOVE N'AdventureWorks_Log' TO N'D:\Logs\AdventureWorks_log.ldf',
NORECOVERY,
REPLACE,
STATS = 10;
Applying Differential Backup
RESTORE DATABASE [AdventureWorks]
FROM DISK = N'C:\Backups\AdventureWorks_Diff_2025_09_13.bak'
WITH NORECOVERY,
STATS = 10;
Applying Log Backups
RESTORE LOG [AdventureWorks]
FROM DISK = N'C:\Backups\AdventureWorks_Log_2025_09_13.trn'
WITH RECOVERY,
STATS = 10;
Best Practices
- Store backups on a separate storage volume or remote location.
- Validate backups regularly with
RESTORE VERIFYONLY
. - Keep at least three generations of full backups and corresponding differential/log chains.
- Encrypt backups for compliance using
ENCRYPTION
options. - Document backup schedules, retention policies, and recovery objectives (RPO/RTO).
Troubleshooting
Common Errors
Error | Cause | Resolution |
---|---|---|
2618 – Backup file already exists | Backup file already present without WITH INIT | Use WITH INIT or specify a new filename. |
3013 – Restore stopped because of error | Incorrect file paths or insufficient permissions | Verify paths, file existence, and service account rights. |
4326 – Log chain broken | Missing log backup after a full backup | Ensure regular log backups; re‑initialize chain with a new full backup. |