J'utilise la requête T-SQL ci-dessous pour déterminer la date de la dernière sauvegarde complète de la base de données et également retourner la taille et l'emplacement du fichier de sauvegarde. Mon problème est qu'il ne renverra aucune donnée pour les bases de données qui n'ont eu aucune sauvegarde ou pour lesquelles il n'y a pas d'historique de sauvegarde. Idéalement, je voudrais modifier la requête afin que toutes les bases de données soient retournées, qu'il y ait ou non un historique de sauvegarde pour elles. Quelqu'un peut-il indiquer comment la requête ci-dessous pourrait être modifiée pour s'adapter à cela?
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];
Vous devez d'abord interroger la liste des bases de données (elle se trouve dans sys.databases
), puis joignez-le à votre requête:
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];
Juste pour ajouter à la belle réponse de Spaghettidba :
que faire si vous sauvegardez dans plusieurs fichiers? ce script vous montrera tout.
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