Click here to Skip to main content
Click here to Skip to main content
Go to top

Backup System Databases in SQL Server

, 9 Oct 2009
Rate this:
Please Sign up or sign in to vote.
Backup System Databases in SQL Server

Table of Contents

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, 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, we 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:

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.

Points of Interest

  • Building your knowledge about the system databases is important to have a better understanding of how SQL Server works internally.
  • Although system databases are normal databases, use caution when working with them due to the potential impact a change could have across the instance.

History

  • 09-October-2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
GeneralMy vote of 5 Pinmemberthatraja6-Oct-10 21:10 
GeneralGood one Pinmemberdesibeats12-Oct-09 0:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 9 Oct 2009
Article Copyright 2009 by Sujit Mandal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid