以下のT-SQLクエリを使用して、最後の完全データベースバックアップの日付を決定し、バックアップファイルのサイズと場所も返します。私の問題は、バックアップがなかった、またはバックアップ履歴がないデータベースのデータをまったく返さないことです。理想的には、バックアップ履歴があるかどうかに関係なく、すべてのデータベースが返されるようにクエリを変更したいと思います。以下のクエリを変更してこれに対応する方法を誰かにアドバイスできますか?
WITH LastBackUp AS
(
SELECT bs.database_name,
bs.backup_size,
bs.backup_start_date,
bmf.physical_device_name,
Position = ROW_NUMBER() OVER( PARTITION BY bs.database_name ORDER BY bs.backup_start_date DESC )
FROM msdb.dbo.backupmediafamily bmf
JOIN msdb.dbo.backupmediaset bms ON bmf.media_set_id = bms.media_set_id
JOIN msdb.dbo.backupset bs ON bms.media_set_id = bs.media_set_id
WHERE bs.[type] = 'D'
AND bs.is_copy_only = 0
)
SELECT
database_name AS [Database],
CAST(backup_size / 1048576 AS DECIMAL(10, 2) ) AS [BackupSizeMB],
backup_start_date AS [Last Full DB Backup Date],
physical_device_name AS [Backup File Location]
FROM LastBackUp
WHERE Position = 1
ORDER BY [Database];
最初にデータベースのリストをクエリする必要があります(sys.databases
にあります)。次にそれをクエリに結合します。
WITH LastBackUp AS
(
SELECT bs.database_name,
bs.backup_size,
bs.backup_start_date,
bmf.physical_device_name,
Position = ROW_NUMBER() OVER( PARTITION BY bs.database_name ORDER BY bs.backup_start_date DESC )
FROM msdb.dbo.backupmediafamily bmf
JOIN msdb.dbo.backupmediaset bms ON bmf.media_set_id = bms.media_set_id
JOIN msdb.dbo.backupset bs ON bms.media_set_id = bs.media_set_id
WHERE bs.[type] = 'D'
AND bs.is_copy_only = 0
)
SELECT
sd.name AS [Database],
CAST(backup_size / 1048576 AS DECIMAL(10, 2) ) AS [BackupSizeMB],
backup_start_date AS [Last Full DB Backup Date],
physical_device_name AS [Backup File Location]
FROM sys.databases AS sd
LEFT JOIN LastBackUp AS lb
ON sd.name = lb.database_name
AND Position = 1
ORDER BY [Database];
Spaghettidbaによる美しい答えに追加するだけです :
複数のファイルにバックアップするとどうなりますか?このスクリプトはそれをすべて表示します。
declare @backup_type CHAR(1) = 'D' --'D' full, 'L' log
;with Radhe as (
SELECT @@Servername as [Server_Name],
B.name as Database_Name,
ISNULL(STR(ABS(DATEDIFF(day, GetDate(), MAX(Backup_finish_date)))), 'NEVER') as DaysSinceLastBackup,
ISNULL(Convert(char(11), MAX(backup_finish_date), 113)+ ' ' + CONVERT(VARCHAR(8),MAX(backup_finish_date),108), 'NEVER') as LastBackupDate
,BackupSize_GB=CAST(COALESCE(MAX(A.BACKUP_SIZE),0)/1024.00/1024.00/1024.00 AS NUMERIC(18,2))
,BackupSize_MB=CAST(COALESCE(MAX(A.BACKUP_SIZE),0)/1024.00/1024.00 AS NUMERIC(18,2))
,media_set_id = MAX(A.media_set_id)
,[AVG Backup Duration]= AVG(CAST(DATEDIFF(s, A.backup_start_date, A.backup_finish_date) AS int))
,[Longest Backup Duration]= MAX(CAST(DATEDIFF(s, A.backup_start_date, A.backup_finish_date) AS int))
,A.type
FROM sys.databases B
LEFT OUTER JOIN msdb.dbo.backupset A
ON A.database_name = B.name
AND A.is_copy_only = 0
AND (@backup_type IS NULL OR A.type = @backup_type )
GROUP BY B.Name, A.type
)
SELECT r.[Server_Name]
,r.Database_Name
,[Backup Type] = r.type
,r.DaysSinceLastBackup
,r.LastBackupDate
,r.BackupSize_GB
,r.BackupSize_MB
,F.physical_device_name
,r.[AVG Backup Duration]
,r.[Longest Backup Duration]
FROM Radhe r
LEFT OUTER JOIN msdb.dbo.backupmediafamily F
ON R.media_set_id = F.media_set_id
ORDER BY r.Server_Name, r.Database_Name