SHARE
Facebook X Pinterest WhatsApp

Has Your Database Been Backed up in the Last 24 Hours?

Written By
thumbnail
Gregory Larsen
Gregory Larsen
Jan 2, 2018

As a DBA your #1 responsibility is to make sure that each of your SQL Server databases are backed up.  Without recent backups you will not be able to recover your database without potentially losing many days of database updates.  Therefore, you need to make sure on a daily basis that your backups are running and running successfully.  In this tip, I provide a couple of TSQL scripts that will identify the last backup of each database.  By reviewing the output from these scripts, you will be able to identify the last backup for the databases on your instance.

Script #1:  Return the LastBackupDateTime for each database on your Instance

SELECT db.Name AS DatabaseName,
COALESCE(CONVERT(VARCHAR(19), MAX(bs.backup_finish_date), 120),'None') AS LastBackUpDateTime
FROM sys.sysdatabases db
        LEFT OUTER JOIN msdb.dbo.backupset bs 
     ON bs.database_name = db.name
GROUP BY db.Name; 

Script #2: Show the databases that have never had a backup, or the current backup is over 24 hours old

SELECT db.Name AS DatabaseName,
COALESCE(CONVERT(VARCHAR(19), MAX(bs.backup_finish_date), 120),'None') AS LastBackUpTime
FROM sys.sysdatabases db
        LEFT OUTER JOIN msdb.dbo.backupset bs 
     ON bs.database_name = db.name
GROUP BY db.Name
HAVING max(bs.backup_finish_date) < dateadd(dd,-1,getdate())
    or max(bs.backup_finish_date) is NULL;

See all articles by Greg Larsen

Recommended for you...

Best Online Courses to Learn SQL
Ronnie Payne
Sep 23, 2022
Best Courses for Database Administrators
Ronnie Payne
Jul 22, 2022
Tip 74 – Changing Cost Threshold for Parallelism
Gregory Larsen
Feb 24, 2021
How Many Databases Can You Name?
Brad Jones
May 11, 2020
Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.