• subscribe
September 14, 2009 12:00 AM

Checking Backup Status on Multiple SQL Server Systems

Monitor backup status without having to rely on a third-party scheduler or backup tool
SQL Server Pro
InstantDoc ID #102606

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 2.

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.



ARTICLE TOOLS

Comments
  • Tim
    2 years ago
    Mar 10, 2010

    It's pretty bad when you post an article in SQL Server Mag and the code is bad. Great concept, but not much help when your code doesn't work. It would be helpful if you post updated and correct code to your article.

  • Edward
    3 years ago
    Dec 01, 2009

    I had the same problem as Marcos when trying to create usp_mon_bacukp_status_of_all_servers. Received Msg 105, Level 15, State 1, Procedure usp_mon_backup_status_of_all_servers, Line 15
    Unclosed quotation mark after the character string ' error. I tried everything I could think of. I even compared with the corrected code below, but it still didn't work. I emailed the author just a few minutes ago. I wonder if something is getting munged for some of us when we try to copy/paste from a browser. Just a theory.

  • John
    3 years ago
    Nov 23, 2009

    -- Replace your database name for yourdb

    Use
    Go

    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

  • John
    3 years ago
    Nov 23, 2009

    I emailed the author and he send me updated code(see below). Works like a charm. Thanks!

    -John

    ============================================

    -- Replace your database name for yourdb

    Use
    Go

    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

  • John
    3 years ago
    Nov 18, 2009

    This is a grea idea. However, the scripts in Web Listing #1 and Web Listing #2 seem to both have errors. It isn't very useful if the code doesn't work.

You must log on before posting a comment.

Are you a new visitor? Register Here