In SQL Server 2022, a new improved backup metadata feature has been introduced, providing enhanced information about backups. One significant addition to the backup metadata is the inclusion of the last valid restore time. This valuable information allows users to identify a safe point in time to restore their databases, even if the backup start and end times differ from the timestamps in the transaction log files. In this blog post, we will explore the importance of this new feature and demonstrate its usage with a code example. While you are at it, do check out Comprehensive Database Performance Health Check.
Importance of Last Valid Restore Time
The last valid restore time provided by the improved backup metadata feature is crucial for ensuring data integrity and recovery. While backup start and end times may vary, the last valid restore time serves as a reference point to safely restore the database to a specific time. This information is especially valuable for point-in-time recovery scenarios, allowing users to restore their databases accurately and reliably.
Script
Here is the script I have used to generate this scenario.
-- Create or recreate the SQLAuthority database USE master; IF EXISTS (SELECT 1 FROM sys.databases WHERE name = 'SQLAuthority') DROP DATABASE SQLAuthority; GO CREATE DATABASE SQLAuthority; GO -- Switch to the SQLAuthority database USE SQLAuthority; GO -- Create the Products table CREATE TABLE Products ( ID INT, Name NVARCHAR(100), CurrentDateTime DATETIME ); -- Insert initial data into the Products table INSERT INTO Products (ID, Name, CurrentDateTime) VALUES (1, 'Action Figure', GETDATE()), (2, 'Plush Teddy Bear', GETDATE()), (3, 'Remote Control Car', GETDATE()); GO -- Perform a full database backup of SQLAuthority BACKUP DATABASE SQLAuthority TO DISK = 'D:\Backup\SQLAuthority_full_backup.bak'; GO -- Retrieve data from the Products table SELECT * FROM Products; GO -- Insert additional data into the Products table INSERT INTO Products (ID, Name, CurrentDateTime) VALUES (4, 'Dollhouse', GETDATE()), (5, 'Building Blocks', GETDATE()), (6, 'RC Drone', GETDATE()); GO 100 -- Take a transaction log backup of SQLAuthority BACKUP LOG SQLAuthority TO DISK = 'D:\Backup\SQLAuthority_log_backup.trn'; GO -- Query backup metadata from the backupset system table SELECT bs.database_name, BackupStartDate = bs.backup_start_date, LastValidRestoreTime = bs.last_valid_restore_time, BackupFinishDate = bs.backup_finish_date, backuptype = CASE WHEN bs.type = 'D' AND bs.is_copy_only = 0 THEN 'Full Database' WHEN bs.type = 'D' AND bs.is_copy_only = 1 THEN 'Full Copy-Only Database' WHEN bs.type = 'I' THEN 'Differential database backup' WHEN bs.type = 'L' THEN 'Transaction Log' WHEN bs.type = 'F' THEN 'File or filegroup' WHEN bs.type = 'G' THEN 'Differential file' WHEN bs.type = 'P' THEN 'Partial' WHEN bs.type = 'Q' THEN 'Differential partial' END + ' Backup', CASE bf.device_type WHEN 2 THEN 'Disk' WHEN 5 THEN 'Tape' WHEN 7 THEN 'Virtual device' WHEN 9 THEN 'Azure Storage' WHEN 105 THEN 'A permanent backup device' ELSE 'Other Device' END AS DeviceType, bs.recovery_model, bs.compatibility_level, LatestBackupLocation = bf.physical_device_name, backup_size_mb = CONVERT(DECIMAL(10, 2), bs.backup_size / 1024. / 1024.), compressed_backup_size_mb = CONVERT(DECIMAL(10, 2), bs.compressed_backup_size / 1024. / 1024.), bms.is_password_protected FROM msdb.dbo.backupset bs LEFT JOIN msdb.dbo.backupmediafamily bf ON bs.[media_set_id] = bf.[media_set_id] INNER JOIN msdb.dbo.backupmediaset bms ON bs.[media_set_id] = bms.[media_set_id] ORDER BY bs.database_name ASC, bs.Backup_Start_Date DESC;
Here is the result which we get.
In the result, you can clearly see that the last valid restore datetime is very much different for the backup start date and backup finish date.
Conclusion
The introduction of improved backup metadata in SQL Server 2022 has enhanced the backup and restore process. The inclusion of the last valid restore time empowers users to restore their databases to a precise point in time, irrespective of differences between backup and log file timestamps. This feature is particularly useful for point-in-time recovery, ensuring data integrity and minimizing potential data loss. By leveraging the code example provided in this blog post, SQL Server users can take advantage of this new feature and enhance their data protection strategies. Understanding these concepts will help you utilize the improved backup metadata feature effectively and ensure the resilience of your SQL Server databases.
Reference: Pinal Dave (http://www.SQLAuthority.com)
First appeared on SQL SERVER 2022 – Last Valid Restore Time – Improved Backup Metadata