Checking Backup Status on Multiple SQL Server Systems

Monitor backup status without having to rely on a third-party scheduler or backup tool

Sivakumar Mahalingam

September 13, 2009

6 Min Read
Checking Backup Status on Multiple SQL Server Systems

Making sure that every database on every server is backed up daily is one of the most important tasks on a DBA's to-do list. But how do you make sure every backup job has run every single day? What if someone has put the job on hold without your knowledge, especially if there are many DBAs on your team managing the environment? You want to make sure every single database has been backed up without relying on any particular backup method, whether it’s native SQL Server backup or a third-party backup tool. The task is more complicated if you have multiple SQL Server systems; multiple sources of schedulers, such as a SQL Server agent and third-party schedulers; and multiple backup methods, such as SQL Server native backup and a third-party backup tool.

That’s why you need to have BI—not business intelligence but backup intelligence. This article shows you how to monitor the backup status of multiple servers from a central server without relying on a scheduler or alternative method, as well as generate a backup report. Note that you shouldn’t rely solely on this method to verify your backup jobs. This report gives only the information that’s stored in SQL Server. You should physically verify your backups for integrity and do test restores whenever possible.

Generating the Backup Status Report

There are multiple ways to monitor and generate comprehensive backup reports. If you don’t have the budget to buy a third-party tool, you can use these scripts to generate a good report on your own. The scripts in this article work with SQL Server 2008, 2005, and 2000, as well as SQL Server 7.0. Note that if you’re managing a multiserver environment, you must designate one SQL Server system as the master server (as shown in Figure 1) where you can create a central database for storing backup information from the monitored linked servers.

The dbo.backupset table in the msdb database contains all the information about the backups. On any given SQL Server system, you can run the script in Listing 1 to find the backup status of all databases that haven’t been backed up for more than seven days. Once you’ve designated the master server, you can follow steps 1-5 to create the stored procedure that will generate the backup status report and the job that executes it.

Step 1: Create linked server connections on the master server. You have to create linked server connections on the master server for all your target servers that you want to monitor. For more information about how to create linked servers in SQL Server 2000, see "How to set up a linked server (Enterprise Manager)" in SQL Server Books Online (BOL). For more information about setting up linked servers in SQL Server 2005, see "Linking Servers" in BOL. The subsequent scripts will use the information from the sysservers table, which contains all the registered linked servers in the master database. Make sure that you can connect to all the linked servers before you proceed.

Step 2: Create the Backup_Status table. Run the script in Listing 2, create_table_backup_status.sql, which creates a table called Backup_Status on the master server. Note that in this listing and all subsequent listings, you have to substitute your database name for .

Step 3: Create the usp_mon_backup_status_of_all_servers stored procedure. Run the script in Web Listing 1 to create the usp_mon_backup_status_of_all_servers stored procedure on the master server. The script is called 02_usp_mon_backup_status_of_all_servers.sql.

Step 4: Create the usp_help_backup_status stored procedure. Use the script in Web Listing 2 to create the usp_help_backup_status stored procedure on the master server.

Step 5: Create a job that executes the usp_help_backup_status stored procedure. Create a job that executes the procedure created in Step 3 on the master server. You can also create a schedule that runs this job daily on the master server. For more information on how to create jobs, job steps, and job schedules, refer to "Implementing Jobs" in BOL.

That’s it, you’re all set. Now every day you just have to connect to the master server using Query Analyzer or SQL Server Management Studio, open a query window, and run the usp_help_backup_status stored procedure shown in Listing 3. Doing so will generate a report that looks similar to Figure 1.

You can create a more sophisticated report by using this stored procedure in Access to create a formatted report, in SQL Server Reporting Services so that others can view the report, or even make the report from the Report Server available on your SharePoint portal. The report generated using this method is equivalent to a report that’s generated by a third-party product.

Verifying Backup Status

You should have a comprehensive knowledge of all the backups on your SQL Server systems, and using this tool can make this important task easy. This method helps you monitor your backups without having to use a SQL Server agent, third-party scheduler, or third-party product.

Listing 1: The Script to Find Databases Not Backed Up in the Past 7 Days

SELECT database_name = sd.name, backup_finish_date, typeFROM master.dbo.sysdatabases sd LEFT OUTER JOIN (SELECT bs.database_name, backup_finish_date, type = case type when 'D' then 'Database'when 'I' then 'Database Differential'when 'L' then 'Log'when 'F' then 'File or Filegroup'end, backup_sizeFROM msdb.dbo.backupset bs,   (select database_name, max_backup_finish_date = max(backup_finish_date) from msdb.dbo.backupsetgroup by database_name) bs1where bs.database_name = bs1.database_nameand bs.backup_finish_date = bs1.max_backup_finish_date) bs3ON bs3.database_name = sd.namewhere sd.name not in ('tempdb')and(backup_finish_date 

Listing 2

-- Replace your database name for yourdbUse Gocreate table Backup_Status(server_name sysname not null,database_name sysname not null,backup_finish_date datetime null,type varchar(50) null)Go

Web Listing 1: The Script to Create the usp_mon_backup_status_of_all_servers Stored Procedure

-- Replace your database name for yourdbUse GoSET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GOcreate  procedure usp_mon_backup_status_of_all_serversasbegindeclare @sql nvarchar(4000)declare @return_code intdeclare @last_backup_date datetimedeclare @server_name sysnamedeclare servers_cursor cursor forselect srvname from master.dbo.sysserversorder by srvnamedelete from backup_statusopen servers_cursorfetch servers_cursor into @server_namewhile @@fetch_status = 0beginset @sql = ''set @sql = 'insert into backup_status SELECT server_name = '''  + @server_name + ''', database_name = convert(varchar, sd.name), backup_finish_date, type ' +'FROM [' + @server_name + '].master.dbo.sysdatabases sd LEFT OUTER JOIN (SELECT bs.database_name, backup_finish_date, type = case type when ''D'' then ''Database''when ''I'' then ''Database Differential''when ''L'' then ''Log''when ''F'' then ''File or Filegroup''end, backup_sizeFROM [' + @server_name + '].msdb.dbo.backupset bs,   (select database_name, max_backup_finish_date = max(backup_finish_date) from [' + @server_name + '].msdb.dbo.backupsetgroup by database_name) bs1where bs.database_name = bs1.database_nameand bs.backup_finish_date = bs1.max_backup_finish_date) bs3ON bs3.database_name = sd.namewhere sd.name not in (''tempdb'')and(backup_finish_date < getdate() - 7or backup_finish_date is null)ORDER BY sd.name ASC, backup_finish_date DESC'--print @sqlexec sp_executesql @sqlfetch servers_cursor into @server_nameendclose servers_cursordeallocate servers_cursorendGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO

Web Listing 2: The Script to Create usp_help_backup_status Stored Procedure

-- Replace your database name for yourdbUse GoSET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GOcreate  proc  usp_help_backup_statusasbegindeclare @sql nvarchar(4000)declare @return_code intdeclare @last_backup_date datetimedeclare @server_name sysnamedeclare servers_cursor cursor forselect distinct server_name from .dbo.backup_statusorder by server_nameopen servers_cursorfetch servers_cursor into @server_nameprint ''print '---------------------------------------------------------------------------'Print 'Databases not backed up in the last seven days'print '---------------------------------------------------------------------------'print ''while @@fetch_status = 0beginprint '---------------------------------------------------------------------------'print 'Server name: ' + @server_nameprint '---------------------------------------------------------------------------'print ''select database_name = convert(varchar, database_name),backup_finish_date = convert(varchar(30), backup_finish_date, 121), type = convert(varchar, type)from backup_statuswhere server_name = @server_namefetch servers_cursor into @server_nameendclose servers_cursordeallocate servers_cursorendGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO

Listing 3: The usp_help_backup_status Stored Procedure

 -- Replace your database name for yourdbUse GoExec usp_help_backup_status

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like