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.
SQL Server relies on several built-in system databases:
master DatabaseThe 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 DatabaseThe 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 DatabaseThe 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 DatabaseThe 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.
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.
master).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 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.
masterRestoring 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.
modelRestoring 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
msdbRestoring 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.
master and msdb are typically in Full recovery mode, while model can be in Simple or Full.master) become corrupt and backups are unavailable, you may need to rebuild the instance, which involves losing all data and configuration.VERIFYONLY option after a backup to ensure the backup file is intact.