TSQL #66 – Monitoring Backup Solution – Backup History

Backup Button

From multiple occasion I also want to write something on the subject of backup history, first of all because Catherine Wilhelmsen has hosted the TSQL Tuesday # 66 and secondly Thomas Larock has introduced a similar solution just today.

We DBAs knowing the vexing issue of backup monitoring … did it run or did not run, can I keep my customer SLA yet?

We have agreed to our customers contractually to check the sql server backup on a daily base, it has to be ran successfully within the last 24 hours, at least one full backup. If this is not the case our reporting tool will created an incident ticket “Please check Backup” automatically.
Therefore my statement has also some limitations which can of course be individually adjusted depending on your needs.
Ideally, you write the query result into a table to collect historical data of the backup, so you can identify a trend of the respective backup sizes.

/*
Get all Backup History Data - used at Customer XYZ for Reporting 
*/

SELECT 
  s.name as [Database], 
  (select bmf.physical_device_name from msdb..backupmediafamily bmf where bmf.media_set_id = bs.media_set_id) as [Backup_Path_File],
  bs.backup_start_date as DATE_BEGIN, 
  bs.backup_finish_date as DATE_END, 
  CASE WHEN bs.backup_start_date > DATEADD(dd,-7,getdate())
    THEN 0
    ELSE 1
  END as [Status],
  CASE WHEN bs.backup_start_date > DATEADD(dd,-1,getdate())
    THEN 'Backup is current within a day'
     WHEN bs.backup_start_date > DATEADD(dd,-7,getdate())
    THEN 'Backup is current within a week'
    ELSE '*****CHECK BACKUP!!!*****'
  END as [Message],
  convert(varchar,cast(bs.backup_size/1024/1024 as money),10) as [Backup_Volume],
  GETDATE() as [Date_Checked]
from 	master..sysdatabases s LEFT OUTER JOIN	msdb..backupset bs ON s.name = bs.database_name
  AND bs.backup_start_date = (SELECT MAX(backup_start_date) FROM msdb..backupset WHERE database_name = bs.database_name AND type = 'D')
WHERE	s.name <> 'tempdb'
ORDER BY s.name
GO

With the result of the query, you now can do a lot of things, for example based on the column “Status” mark the appropriate line colored to make the faulty backups more visible or to change the subject line of a status-mail.
We have a combination of SQL Server alerts with the above query script which then informs us where we check/restart the backups.

Backup History Monitoring

In addition I recommend to establish the SQL Alert “SQL Server Alert System: ‘Severity 016’ occurred on SERVER_XYZ” with mail alerting (see also this blog post for SQL Server Alerts).

addendum:
Last week I stumbled over a storage problem where this script is not really worked because the backup was running but indeed it was unfortunately very slow, so that it had not been finished within 40 minutes (as usual) but unfortunately had run around 24 hours… the result was not telling the whole truth but in this case we knew about the problem…

Backup

Björn works as a database administrator and Head of Competence for MS SQL and mySQL in Hamburg (Germany). He regularly participates in the PASS regional group meetings, events of the PASS such as SQLSaturday and SQLGrillen and he organizes the Azure Meetup group in Hamburg. He is interested in topics like SQL Server, Powershell and Azure for science fiction, snowboarding, baking and cycling.

One thought on “TSQL #66 – Monitoring Backup Solution – Backup History

Leave a Reply

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