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, type FROM 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_size FROM msdb.dbo.backupset bs, (select database_name, max_backup_finish_date = max(backup_finish_date) from msdb.dbo.backupset group by database_name) bs1 where bs.database_name = bs1.database_name and bs.backup_finish_date = bs1.max_backup_finish_date) bs3 ON bs3.database_name = sd.name where sd.name not in ('tempdb') and (backup_finish_date
Listing 2
-- Replace your database name for yourdb Use Go create 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 yourdb UseGo SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO create procedure usp_mon_backup_status_of_all_servers as begin declare @sql nvarchar(4000) declare @return_code int declare @last_backup_date datetime declare @server_name sysname declare servers_cursor cursor for select srvname from master.dbo.sysservers order by srvname delete from backup_status open servers_cursor fetch servers_cursor into @server_name while @@fetch_status = 0 begin set @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_size FROM [' + @server_name + '].msdb.dbo.backupset bs, (select database_name, max_backup_finish_date = max(backup_finish_date) from [' + @server_name + '].msdb.dbo.backupset group by database_name) bs1 where bs.database_name = bs1.database_name and bs.backup_finish_date = bs1.max_backup_finish_date) bs3 ON bs3.database_name = sd.name where sd.name not in (''tempdb'') and (backup_finish_date < getdate() - 7 or backup_finish_date is null) ORDER BY sd.name ASC, backup_finish_date DESC' -- print @sql exec sp_executesql @sql fetch servers_cursor into @server_name end close servers_cursor deallocate servers_cursor end GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO
Web Listing 2: The Script to Create usp_help_backup_status Stored Procedure
-- Replace your database name for yourdb UseGo SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO create proc usp_help_backup_status as begin declare @sql nvarchar(4000) declare @return_code int declare @last_backup_date datetime declare @server_name sysname declare servers_cursor cursor for select distinct server_name from .dbo.backup_status order by server_name open servers_cursor fetch servers_cursor into @server_name print '' print '---------------------------------------------------------------------------' Print 'Databases not backed up in the last seven days' print '---------------------------------------------------------------------------' print '' while @@fetch_status = 0 begin print '---------------------------------------------------------------------------' print 'Server name: ' + @server_name print '---------------------------------------------------------------------------' print '' select database_name = convert(varchar, database_name), backup_finish_date = convert(varchar(30), backup_finish_date, 121), type = convert(varchar, type) from backup_status where server_name = @server_name fetch servers_cursor into @server_name end close servers_cursor deallocate servers_cursor end GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO
Listing 3: The usp_help_backup_status Stored Procedure
-- Replace your database name for yourdb Use Go Exec usp_help_backup_status