Skip to main content
Our Tech Ideas

Retrieving SQL Server Restore History Using T-SQL Queries

Introduction

Explain the importance of maintaining restore history in a SQL Server environment. Introduce the problem statement: How can database administrators track restore operations effectively?

Understanding the Data Model

Briefly discuss the relevant tables in the msdb database:

  • restorehistory: Stores information about database restores.
  • backupset: Contains details about backup sets, including start and finish times.
  • backupmediafamily: Holds information about backup devices.

Crafting the SQL Query

Explain each part of the SQL query step by step:

SELECT
  [rs].[destination_database_name],
  [rs].[restore_date],
  [bs].[backup_start_date],
  [bs].[backup_finish_date],
  [bs].[database_name] as [source_database_name],
  [bmf].[physical_device_name] as [source_device_name]
FROM msdb..restorehistory rs
INNER JOIN msdb..backupset bs ON rs.backup_set_id = bs.backup_set_id
INNER JOIN msdb..backupmediafamily bmf ON bs.media_set_id = bmf.media_set_id
ORDER BY [rs].[restore_date] DESC;
SQL
  • SELECT: List of columns selected from the query.
  • FROM: Tables involved in the query.
  • INNER JOIN: How tables are joined using common keys (backup_set_id and media_set_id).
  • ORDER BY: Sorting results by restore_date in descending order.

Explanation of Columns

Break down each column selected:

  • destination_database_name: Name of the database being restored.
  • restore_date: Date and time of the restore operation.
  • backup_start_date, backup_finish_date: Start and end times of the backup operation.
  • source_database_name: Name of the source database being restored from.
  • source_device_name: Physical device (backup media) from which the restore was performed.

Practical Use Cases

Discuss practical scenarios where this query can be useful:

  • Auditing restore operations.
  • Investigating restore failures or delays.
  • Monitoring historical trends in restore activities.

Conclusion

Summarize the importance of tracking restore history and how the provided SQL query simplifies this task. Encourage readers to customize and integrate the query into their monitoring or reporting systems.