Backup and Restore of System Databases

On this page

Introduction

System databases contain essential information for SQL Server to operate. Backing up and restoring these databases is crucial for maintaining the integrity and availability of your SQL Server instance. This document outlines the procedures and considerations for managing system database backups.

System Databases

SQL Server relies on several built-in system databases:

master Database

The master database records all system-level information for an instance of SQL Server. This includes metadata about other databases, login accounts, and system configuration settings.

model Database

The model database is used as a template for all new databases created on the SQL Server instance. Any changes made to the model database, such as adding files or setting database options, will be inherited by new databases.

msdb Database

The msdb database stores information about scheduled jobs, alerts, operators, backups, and the history of these items. It is essential for managing and monitoring SQL Server Agent activities.

tempdb Database

The tempdb database is used to store temporary objects, such as temporary tables, table variables, cursors, and worktables for operations like sorting and hashing. tempdb is recreated every time SQL Server starts, so it does not require regular backups.

Note: tempdb is a special case. It is not backed up and restored like other user databases because it is rebuilt every time the SQL Server instance starts.

Backing Up System Databases

Regular backups of the master, model, and msdb databases are essential. We recommend performing full backups regularly and transaction log backups for msdb if it's in full recovery mode.

Using SQL Server Management Studio (SSMS)

  1. Connect to your SQL Server instance using SSMS.
  2. In Object Explorer, expand "Databases".
  3. Right-click on the system database you want to back up (e.g., master).
  4. Navigate to "Tasks" > "Back Up...".
  5. In the "Back Up Database" dialog:
    • Ensure "Database" is set to the correct system database.
    • Select "Backup type" (e.g., "Full").
    • Specify a "Destination" for the backup file (.bak).
    • Click "OK" to start the backup.

Using Transact-SQL

You can also use Transact-SQL commands to perform backups:

-- Backup the master database
BACKUP DATABASE master
TO DISK = 'C:\Backup\master_full_backup.bak'
WITH FORMAT, COMPRESSION, STATS = 10;
GO

-- Backup the model database
BACKUP DATABASE model
TO DISK = 'C:\Backup\model_full_backup.bak'
WITH FORMAT, COMPRESSION, STATS = 10;
GO

-- Backup the msdb database (assuming full recovery mode)
BACKUP DATABASE msdb
TO DISK = 'C:\Backup\msdb_full_backup.bak'
WITH FORMAT, COMPRESSION, STATS = 10;
GO

-- Transaction Log backup for msdb (if in full recovery mode)
BACKUP LOG msdb
TO DISK = 'C:\Backup\msdb_log_backup.trn'
WITH COMPRESSION, STATS = 10;
GO

Restoring System Databases

Restoring system databases should be done with extreme caution, as it can affect the entire SQL Server instance. Always ensure you have a reliable, recent backup before proceeding.

Important: You cannot restore system databases if the SQL Server instance is not running. You may need to start SQL Server in single-user mode or minimal configuration mode to perform restores.

Restoring master

Restoring the master database is a critical operation. If master is corrupted, you might need to rebuild it.

-- Restore the master database (requires SQL Server to be in single-user mode)
RESTORE DATABASE master
FROM DISK = 'C:\Backup\master_full_backup.bak'
WITH REPLACE, STATS = 10;
GO

After restoring master, you will need to restart the SQL Server service.

Restoring model

Restoring the model database is generally safer than restoring master.

RESTORE DATABASE model
FROM DISK = 'C:\Backup\model_full_backup.bak'
WITH REPLACE, STATS = 10;
GO

Restoring msdb

Restoring msdb is important to recover SQL Server Agent jobs and history.

RESTORE DATABASE msdb
FROM DISK = 'C:\Backup\msdb_full_backup.bak'
WITH REPLACE, STATS = 10;
GO

-- If you have transaction log backups for msdb:
-- RESTORE LOG msdb
-- FROM DISK = 'C:\Backup\msdb_log_backup.trn'
-- WITH NORECOVERY, STATS = 10;
-- GO
-- -- Then a final log backup or restore to bring it online if needed
-- RESTORE LOG msdb WITH RECOVERY;
-- GO

After restoring msdb, you may need to restart SQL Server Agent service.

Important Considerations

Best Practices