Best Practices Backup System Databases in SQL Server

Problem

The role of system databases for the functioning of SQL Server cannot be underestimated due to the significant amount of information which is stored within these databases. System databases which are available in SQL Server 2005 and later versions are Master, Resource, MSDB, MODEL, TempDB, Distribution, ReportServer and ReportServerTempDB.

It’s a best practice to create daily backups of all the system databases once all the user databases on the server are backed up successfully.  If not daily, the DBA should at a minimum backup all the system databases each time a server or database configuration is added or modified.  These include Service Packs, Hot Fixes, Cumulative Update, login changes, job changes, operator changes, database configuration changes, SSIS package changes, replication changes, etc…

Solution

The following shows the system databases and how they are used.  Since this data is only stored in these databases this is why it is key to back up these databases.

Primary System Databases

  • master – this holds information about logins and also information about all other databases
  • msdb – this stores jobs, operators, alerts, backup and restore history, database mail information ,etc…
  • model – this is used as a model for all new databases.  If you want certain objects to be in all new databases this is where you configure this information.
  • tempdb – this database is created each time SQL Server starts so there is not a need to back this up

Resource Database

In SQL Server 2005, Microsoft introduced a new system database called the Resource database. The Resource database is a read-only hidden database that contains all the system objects which are included within SQL Server. The DBA needs to perform a file-based copy of mssqlsystemresource.mdf and mssqlsystemresource.ldf files of the Resource database as SQL Server doesn’t support backing up the Resource database.

In SQL Server 2005 the Resource database is available in “<drive>:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\” location and in SQL Server 2008 the Resource database is available in “<drive>:\Program Files\Microsoft SQL Server\MSSQL10.<instance_name>\MSSQL\Binn\” location.

Reporting Services Databases

  • ReportServer – will be available if you have installed Reporting Services
  • ReportServerTempDB – will be available if you have installed Reporting Services

Replication System Database

  • distribution – this database will be available when you have configured Replication

Enabling XP_CMDSHELL

As we need to perform file-based backups for the Resource database files, you need to enable xp_cmdshell feature using the sp_configure system stored procedure. The below T-SQL code can be used to enable this feature.

USE master
GO
sp_configure 'show advanced options'
GO
/* 0 = Disabled , 1 = Enabled */
sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE WITH OVERRIDE
GO

T-SQL code to backup System Databases

Database Administrators can use the below T-SQL code to backup all of the system databases to SystemDatabaseBackups folder on the E drive.  You will need to change this path for your systems.

This is a simple script that includes code for each database to be backed up.

USE master
GO
SELECT GETDATE() AS 'System Database Backup Start Time'
GO
/* Backup Distribution Database */ 
BACKUP DATABASE Distribution 
TO DISK = 'E:\SystemDatabaseBackups\Distribution.BAK' 
WITH INIT
GO
/* Backup ReportServer Database */ 
BACKUP DATABASE ReportServer 
TO DISK = 'E:\SystemDatabaseBackups\ReportServer.BAK' 
WITH INIT
GO
/* Backup ReportServerTempDB Database */ 
BACKUP DATABASE ReportServerTempDB 
TO DISK = 'E:\SystemDatabaseBackups\ReportServerTempDB.BAK' 
WITH INIT
GO
/* Backup Master Model */ 
BACKUP DATABASE Model 
TO DISK = 'E:\SystemDatabaseBackups\Model.BAK' 
WITH INIT
GO
/* Backup Master Database */ 
BACKUP DATABASE Master 
TO DISK = 'E:\SystemDatabaseBackups\Master.BAK' 
WITH INIT
GO
/* Backup Master MSDB */ 
BACKUP DATABASE MSDB 
TO DISK = 'E:\SystemDatabaseBackups\MSDB.BAK' 
WITH INIT
GO
/* Copy Resource Database Files Using XP_CMDSHELL */ 
EXEC xp_cmdshell 'COPY /Y "D:\Program Files\Microsoft SQL Server\MSSQL10.
SQL2008\MSSQL\Binn\mssqlsystemresource.mdf" "E:\SystemDatabaseBackups"' 
GO
EXEC xp_cmdshell 'COPY /Y "D:\Program Files\Microsoft SQL Server\MSSQL10.
SQL2008\MSSQL\Binn\mssqlsystemresource.ldf" "E:\SystemDatabaseBackups"'
GO
SELECT GETDATE() AS 'System Database Backup End Time'
GO

Here is a listing of these databases and files after they have been backed up.

system database backup files

If you have proper backups of all the system and user databases then you can restore and recover the SQL Server system in the event of a system failure, such as a hard disk corruption.

Next Steps

Leave a Reply

Your email address will not be published. Required fields are marked *