SQL Server在msdb資料中維護了一系列表,用來儲存執行所有備份與還原的細節資訊。即使你正在使用第三方的備份應用程式,只要這個應用程式使用SQL Server的虛擬設備介面(Virtual Device Interface---VDI)來執行備份與還原執行,那麼執行細節依然被儲存在這一系列表中。
儲存細節的表包括:
- backupset
- backupfile
- backupfilegroup (SQL Server 2005 upwards)
- backupmediaset
- backupmediafamily
- restorehistory
- restorefile
- restorefilegroup
- logmarkhistory
- suspect_pages (SQL Server 2005 upwards)
你可以在Books Online裡面找到上面這些表的具體說明。
下面這個指令碼可以幫你找出每個資料庫近期的備份資訊:
SELECT b.name, a.type, MAX(a.backup_finish_date) lastbackup
FROM msdb..backupset a
INNER JOIN master..sysdatabases b ON a.database_name COLLATE DATABASE_DEFAULT = b.name COLLATE DATABASE_DEFAULT
GROUP BY b.name, a.type
ORDER BY b.name, a.type
指定資料庫最後20條交易記錄備份資訊:
SELECT TOP 20 b.physical_device_name, a.backup_start_date, a.first_lsn, a.user_name FROM msdb..backupset a
INNER JOIN msdb..backupmediafamily b ON a.media_set_id = b.media_set_id
WHERE a.type = 'L'
ORDER BY a.backup_finish_date DESC
指定時間段的交易記錄備份資訊:
SELECT b.physical_device_name, a.backup_set_id, b.family_sequence_number, a.position, a.backup_start_date, a.backup_finish_date
FROM msdb..backupset a
INNER JOIN msdb..backupmediafamily b ON a.media_set_id = b.media_set_id
WHERE a.database_name = 'AdventureWorks'
AND a.type = 'L'
AND a.backup_start_date > '10-Jan-2007'
AND a.backup_finish_date < '16-Jan-2009 3:30'
ORDER BY a.backup_start_date, b.family_sequence_number
刪除備份日誌的兩個預存程序:
EXEC msdb..sp_delete_backuphistory '1-Jan-2005'
EXEC msdb..sp_delete_database_backuphistory 'AdventureWorks'