获取每个数据库的上次完整备份和事务日志备份


15

如何使用系统表“ backupset”和相关的备份表编写脚本或查询,以显示每个生产服务器上每个数据库的最新完整备份和最新日志备份。

Answers:


16

这样的事情应该会为您提供所需的输出。以下查询为每个数据库提取完整,差异或日志备份类型的最新备份。

;with backup_cte as
(
    select
        database_name,
        backup_type =
            case type
                when 'D' then 'database'
                when 'L' then 'log'
                when 'I' then 'differential'
                else 'other'
            end,
        backup_finish_date,
        rownum = 
            row_number() over
            (
                partition by database_name, type 
                order by backup_finish_date desc
            )
    from msdb.dbo.backupset
)
select
    database_name,
    backup_type,
    backup_finish_date
from backup_cte
where rownum = 1
order by database_name;

9

如果您对表格的更多详细信息感兴趣,我在这里写了一篇关于使用backupset的文章。尽管这里只是一个查询,以提取您想要的东西。

use msdb
go

-- D = Full, I = Differential and L = Log.
-- There are other types of backups too but those are the primary ones.
SELECT backupset.database_name, 
    MAX(CASE WHEN backupset.type = 'D' THEN backupset.backup_finish_date ELSE NULL END) AS LastFullBackup,
    MAX(CASE WHEN backupset.type = 'I' THEN backupset.backup_finish_date ELSE NULL END) AS LastDifferential,
    MAX(CASE WHEN backupset.type = 'L' THEN backupset.backup_finish_date ELSE NULL END) AS LastLog
FROM backupset
GROUP BY backupset.database_name
ORDER BY backupset.database_name DESC

注意:提到我已经加入了backmediaset,因此不需要它。这是真的,我只能假设我忘记从链接的原始查询中删除它。我已将其从此处删除。


4

您可能希望查看msdb数据库,以获取这些详细信息。

例如:

DECLARE @DBName SYSNAME;
SET @DBName = DB_NAME(); -- modify these as you desire.
SET @DBName = NULL; -- comment this line if you want to limit the displayed history

SELECT DatabaseName = bs.database_name
    , BackupStartDate = bs.backup_start_date
    , CompressedBackupSize = bs.compressed_backup_size
    , ExpirationDate = bs.expiration_date
    , BackupSetName = bs.name
    , RecoveryModel = bs.recovery_model
    , ServerName = bs.server_name
    , BackupType = CASE bs.type 
        WHEN 'D' THEN 'Database' 
        WHEN 'L' THEN 'Log' 
        ELSE '[unknown]' END
    , LogicalDeviceName = bmf.logical_device_name
    , PhysicalDeviceName = bmf.physical_device_name
FROM msdb.dbo.backupset bs
    INNER JOIN msdb.dbo.backupmediafamily bmf 
        ON [bs].[media_set_id] = [bmf].[media_set_id]
WHERE (bs.database_name = @DBName
    OR @DBName IS NULL)
    AND bs.type = 'D'
ORDER BY bs.backup_start_date DESC;

1

用于显示每个数据库的上次完整,差异日志记录备份,考虑如果未进行任何备份,则显示null 。使用数据透视(从源代码修改)

请注意,如果不加入master.sys.databases并仅依赖msdb.dbo.backupset,则不会找到没有备份的数据库

  SELECT  name ,
            recovery_model_desc ,
            state_desc ,
            d AS 'Last Full Backup' ,
            i AS 'Last Differential Backup' ,
            l AS 'Last log Backup'
    FROM    ( SELECT    db.name ,
                        db.state_desc ,
                        db.recovery_model_desc ,
                        type ,
                        backup_finish_date
              FROM      master.sys.databases db
                        LEFT OUTER JOIN msdb.dbo.backupset a ON a.database_name = db.name
            ) AS Sourcetable 
        PIVOT 
            ( MAX(backup_finish_date) FOR type IN ( D, I, L ) ) AS MostRecentBackup

在此处输入图片说明

@根据@SQLBoy请求,我们可以在下面的查询中编写以在此查询中包含BackupSize和BackupSet.Name。我省略了数据透视图并使其更简单

WITH    backupsetSummary
          AS ( SELECT   bs.database_name ,
                        bs.type bstype ,
                        MAX(backup_finish_date) MAXbackup_finish_date
               FROM     msdb.dbo.backupset bs
               GROUP BY bs.database_name ,
                        bs.type
             ),
        MainBigSet
          AS ( SELECT   db.name ,
                        db.state_desc ,
                        db.recovery_model_desc ,
                        bs.type ,
                        bs.name AS BackupSetName ,
                        bs.backup_size ,
                        bs.backup_finish_date
               FROM     master.sys.databases db
                        LEFT OUTER JOIN backupsetSummary bss ON bss.database_name = db.name
                        LEFT OUTER JOIN msdb.dbo.backupset bs ON bs.database_name = db.name
                                                              AND bss.bstype = bs.type
                                                              AND bss.MAXbackup_finish_date = bs.backup_finish_date
             )
    SELECT  *
    FROM    MainBigSet

嗨,@ imanabidi,您能帮我在上面发布的代码中添加两列吗?我想要msdb.dbo.backupset表中的backup_size和名称,其中msdb.dbo.backupset.name ='XXXXXXX'。我需要这样做是因为我们正在使用第三方工具。该名称反映在msdb.dbo.backupset.name中。请告诉我。
SQLBoy

0

您可以使用以下脚本:

1)如果要获取最新备份,请使用以下命令:

SELECT sdb.Name AS DatabaseName,
COALESCE(CONVERT(VARCHAR(12), MAX(bus.backup_finish_date), 101),'-') AS LastBackUpTime
FROM sys.sysdatabases sdb
LEFT OUTER JOIN msdb.dbo.backupset bus ON bus.database_name = sdb.name
GROUP BY sdb.Name

参考:http : //blog.sqlauthority.com/2010/11/04/sql-server-finding-last-backup-time-for-all-database/

2)如果要获取最近七天内所有数据库的备份历史记录:

SELECT 
   CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server,
   msdb.dbo.backupset.database_name, 
   msdb.dbo.backupset.backup_start_date, 
   msdb.dbo.backupset.backup_finish_date,
   msdb.dbo.backupset.expiration_date,
   CASE msdb..backupset.type 
       WHEN 'D' THEN 'Database' 
       WHEN 'L' THEN 'Log' 
   END AS backup_type, 
   msdb.dbo.backupset.backup_size, 
   msdb.dbo.backupmediafamily.logical_device_name, 
   msdb.dbo.backupmediafamily.physical_device_name,  
   msdb.dbo.backupset.name AS backupset_name,
   msdb.dbo.backupset.description
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() - 7) 
ORDER BY 
   msdb.dbo.backupset.database_name,
   msdb.dbo.backupset.backup_finish_date

参考:http : //www.mssqltips.com/sqlservertip/1601/script-to-retrieve-sql-server-database-backup-history-and-no-backups/


0

所有数据库的上次备份日期。简单。

SELECT sdb.Name AS DatabaseName,
COALESCE(CONVERT(VARCHAR(12), MAX(bus.backup_finish_date), 101),'-') AS LastBackUpTime
FROM sys.sysdatabases sdb
LEFT OUTER JOIN msdb.dbo.backupset bus ON bus.database_name = sdb.name
GROUP BY sdb.Name

0

我认为以下是我们大家一直在寻找的查询:

使用系统表“ backupset”和相关备份表,将显示每个生产服务器上每个数据库的最后一次完整备份和最后一个日志备份的脚本或查询。

-- Query to find latest "Full" as well as "T-Log" backup of all specified Databases 
USE MSDB
GO

SELECT msdb.dbo.backupset.server_name
    ,msdb.dbo.backupset.database_name
    ,CASE msdb.dbo.backupset.[type] -- Let's decode the main types of backup here
        WHEN 'D'
            THEN 'Full'
        WHEN 'I'
            THEN 'Differential'
        WHEN 'L'
            THEN 'Transaction Log'
        WHEN 'F'
            THEN 'File or filegroup'
        WHEN 'G'
            THEN 'Differential file'
        WHEN 'P'
            THEN 'Partial'
        WHEN 'Q'
            THEN 'Differential partial'
        ELSE msdb.dbo.backupset.[type]
        END AS BackupType
    ,msdb.dbo.backupset.backup_start_date
    ,msdb.dbo.backupset.backup_finish_date
    ,CAST((CAST(DATEDIFF(s, msdb.dbo.backupset.backup_start_date, msdb.dbo.backupset.backup_finish_date) AS INT)) / 3600 AS VARCHAR) + ' hours, ' + CAST((CAST(DATEDIFF(s, msdb.dbo.backupset.backup_start_date, msdb.dbo.backupset.backup_finish_date) AS INT)) / 60 AS VARCHAR) + ' minutes, ' + CAST((CAST(DATEDIFF(s, msdb.dbo.backupset.backup_start_date, msdb.dbo.backupset.backup_finish_date) AS INT)) % 60 AS VARCHAR) + ' seconds' AS [Total_Time_Taken_For_Backup]
    ,msdb.dbo.backupmediafamily.physical_device_name AS Backup_File_Location
FROM msdb.dbo.backupmediafamily
INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
WHERE (
        backup_set_ID IN (
            SELECT MAX(backup_set_id)
            FROM msdb.dbo.backupset
            WHERE msdb.dbo.backupset.[type] = 'D'
            GROUP BY database_name
            )
        OR backup_set_ID IN (
            SELECT MAX(backup_set_id)
            FROM msdb.dbo.backupset
            WHERE msdb.dbo.backupset.[type] = 'L'
            GROUP BY database_name
            )
        )
ORDER BY msdb.dbo.backupset.database_name
    ,BackupType
GO
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.