Информация по резервированию для указанной базы за последние n-дней
-- Имя базы данных
DECLARE @database_name char(100) ='NameDatabase'
-- Сколько дней от текущей даты нужно анализировать
DECLARE @day int = 1
SELECT
CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server,
msdb.dbo.backupset.database_name as 'Database name',
CASE msdb..backupset.type
WHEN 'D' THEN 'Database'
WHEN 'L' THEN 'Log'
END AS backup_type,
msdb.dbo.backupset.backup_size,
convert(varchar, dateadd(s, datediff (s, msdb.dbo.backupset.backup_start_date, msdb.dbo.backupset.backup_finish_date), convert(datetime2, '0001-01-01')), 108) as backup_duration,
msdb.dbo.backupset.backup_start_date AS Start_backup,
msdb.dbo.backupset.backup_finish_date AS Finish_backup,
msdb.dbo.backupmediafamily.physical_device_name,
msdb.dbo.backupmediafamily.logical_device_name,
msdb.dbo.backupset.name AS backupset_name,
msdb.dbo.backupset.description,
msdb.dbo.backupset.expiration_date AS Expiration_backup
FROM msdb.dbo.backupmediafamily
INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
WHERE (CONVERT(datetime, msdb.dbo.backupset.backup_start_date, 102) >= GETDATE() - @day) and msdb.dbo.backupset.database_name = @database_name
ORDER BY
msdb.dbo.backupset.database_name,
msdb.dbo.backupset.backup_finish_date desc
Информация о последней сделанной резервной копии для всех баз данных на SQL-сервере
SELECT
CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server,
msdb.dbo.backupset.database_name,
MAX(msdb.dbo.backupset.backup_finish_date) AS last_db_backup_date
FROM msdb.dbo.backupmediafamily
INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
INNER JOIN sys.sysdatabases ON sys.sysdatabases.name = msdb.dbo.backupset.database_name
WHERE msdb..backupset.type = 'D'
GROUP BY
msdb.dbo.backupset.database_name
ORDER BY
last_db_backup_date
Список баз данных с количеством часов, которое прошло с последней сделанной резервной копии (базы, которые резервировались за последние 24 часа, в список не попадают)
SELECT
CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server,
msdb.dbo.backupset.database_name,
MAX(msdb.dbo.backupset.backup_finish_date) AS last_db_backup_date,
DATEDIFF(hh, MAX(msdb.dbo.backupset.backup_finish_date), GETDATE()) AS [Backup Age (Hours)]
FROM master.dbo.sysdatabases AS M LEFT JOIN msdb.dbo.backupset ON M.Name = msdb.dbo.backupset.database_name
WHERE msdb.dbo.backupset.type = 'D'
GROUP BY msdb.dbo.backupset.database_name
HAVING (MAX(msdb.dbo.backupset.backup_finish_date) < DATEADD(hh, - 24, GETDATE()))
UNION
SELECT
CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server,
M.NAME AS database_name,
NULL AS [Last Data Backup Date],
9999 AS [Backup Age (Hours)]
FROM
master.dbo.sysdatabases AS M LEFT JOIN msdb.dbo.backupset
ON M.name = msdb.dbo.backupset.database_name
WHERE msdb.dbo.backupset.database_name IS NULL AND M.name <> 'tempdb'
ORDER BY
msdb.dbo.backupset.database_name
Источник: http://www.mssqltips.com/sqlservertip/1601/script-to-retrieve-sql-server-database-backup-history-and-no-backups/
-- Имя базы данных
DECLARE @database_name char(100) ='NameDatabase'
-- Сколько дней от текущей даты нужно анализировать
DECLARE @day int = 1
SELECT
CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server,
msdb.dbo.backupset.database_name as 'Database name',
CASE msdb..backupset.type
WHEN 'D' THEN 'Database'
WHEN 'L' THEN 'Log'
END AS backup_type,
msdb.dbo.backupset.backup_size,
convert(varchar, dateadd(s, datediff (s, msdb.dbo.backupset.backup_start_date, msdb.dbo.backupset.backup_finish_date), convert(datetime2, '0001-01-01')), 108) as backup_duration,
msdb.dbo.backupset.backup_start_date AS Start_backup,
msdb.dbo.backupset.backup_finish_date AS Finish_backup,
msdb.dbo.backupmediafamily.physical_device_name,
msdb.dbo.backupmediafamily.logical_device_name,
msdb.dbo.backupset.name AS backupset_name,
msdb.dbo.backupset.description,
msdb.dbo.backupset.expiration_date AS Expiration_backup
FROM msdb.dbo.backupmediafamily
INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
WHERE (CONVERT(datetime, msdb.dbo.backupset.backup_start_date, 102) >= GETDATE() - @day) and msdb.dbo.backupset.database_name = @database_name
ORDER BY
msdb.dbo.backupset.database_name,
msdb.dbo.backupset.backup_finish_date desc
Информация о последней сделанной резервной копии для всех баз данных на SQL-сервере
SELECT
CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server,
msdb.dbo.backupset.database_name,
MAX(msdb.dbo.backupset.backup_finish_date) AS last_db_backup_date
FROM msdb.dbo.backupmediafamily
INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
INNER JOIN sys.sysdatabases ON sys.sysdatabases.name = msdb.dbo.backupset.database_name
WHERE msdb..backupset.type = 'D'
GROUP BY
msdb.dbo.backupset.database_name
ORDER BY
last_db_backup_date
Список баз данных с количеством часов, которое прошло с последней сделанной резервной копии (базы, которые резервировались за последние 24 часа, в список не попадают)
SELECT
CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server,
msdb.dbo.backupset.database_name,
MAX(msdb.dbo.backupset.backup_finish_date) AS last_db_backup_date,
DATEDIFF(hh, MAX(msdb.dbo.backupset.backup_finish_date), GETDATE()) AS [Backup Age (Hours)]
FROM master.dbo.sysdatabases AS M LEFT JOIN msdb.dbo.backupset ON M.Name = msdb.dbo.backupset.database_name
WHERE msdb.dbo.backupset.type = 'D'
GROUP BY msdb.dbo.backupset.database_name
HAVING (MAX(msdb.dbo.backupset.backup_finish_date) < DATEADD(hh, - 24, GETDATE()))
UNION
SELECT
CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server,
M.NAME AS database_name,
NULL AS [Last Data Backup Date],
9999 AS [Backup Age (Hours)]
FROM
master.dbo.sysdatabases AS M LEFT JOIN msdb.dbo.backupset
ON M.name = msdb.dbo.backupset.database_name
WHERE msdb.dbo.backupset.database_name IS NULL AND M.name <> 'tempdb'
ORDER BY
msdb.dbo.backupset.database_name
Источник: http://www.mssqltips.com/sqlservertip/1601/script-to-retrieve-sql-server-database-backup-history-and-no-backups/
Комментариев нет:
Отправить комментарий