Get Last Backup Times for all Databases in SQL Server

Ensuring your databases are regularly backed up is critical for data recovery in case of corruption, accidental deletions, or other failures. If you don’t have a backup, recovering lost data may be impossible. As a Database Administrator, it’s your number one Priority along-side ensuring your SQL Service is online and healthy.

In this article, I’ll demonstrate how to query the backup history for all databases in a SQL Server instance, helping you verify your backup schedule and detect potential gaps.

In SQL Server, backups are categorized into three main types:
> Full Backups: Capture the entire database at a specific point in time.
> Differential Backups: Include only changes made since the last full backup.
> Transaction Log (TLog) Backups: Record all transaction log activity since the last log backup.

If a backup of a specific type hasn’t been performed, the output for that type will show as NULL when using the SQL queries below.

If you discover when running this you have no Full Backup, you should resolve that by backing up all of your databases.

1. Show Most Recent Backups for all Databases

This first script retrieves the most recent Full, Differential, and Transaction Log backup times for all databases in your SQL Server instance. Use this to confirm if all databases have been backed up recently.

Get Most Recent Last Backup SQL Server

We can see from the above screenshot all databases on this SQL Server Instance have been backed up in the last 30 days. Only the demoBlog has had recent Differential and TLog Backups.

The type column within the backupset system table gives us a way of checking whether the backup was a Full, Differential or Transaction Log.

2. Get Database Backup History

The second script provides a detailed history of all backups, including their type, size, and associated metadata for the last two months. This is particularly useful for auditing backup schedules or troubleshooting issues like broken backup chains.

show database backup history sql server

You can simply remove the WHERE Clause to bring back the full history if necessary.

3. Tips for Optimizing Backup Strategies

Test Restores:
Regularly perform restore tests to ensure backups are usable.

Automate Backups:
Use SQL Server Agent jobs or third-party tools for consistent scheduling.

Monitor Regularly:
Run these scripts periodically to monitor backup status.

Enable Alerts:
Configure alerts for missed or failed backups.


Comments

Leave a Reply

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