为 SQL Server 2008-2014 的备份状态创建视图

Posted

技术标签:

【中文标题】为 SQL Server 2008-2014 的备份状态创建视图【英文标题】:Creating a view for the backup status of a SQL Server 2008-2014 【发布时间】:2016-03-04 09:06:44 【问题描述】:

我需要为 SQL Server 创建备份状态的视图。输出应如下所示:

差异:0 天 备份数据库已完成 开始时间:2016.01.30 19:48:03 结束:2016.01.30 22:34:51 时长 02:46:48 尺寸 (GB) : 1,156.77 备份类型(TSM 或 SQL Server 维护计划) 数据库名称

关于如何为 SQL Server 执行此操作的任何想法? 是否可以确定备份是使用 TSM TDP 还是使用 SQL Server 维护计划进行的?

【问题讨论】:

【参考方案1】:

MSDB:

SELECT
      backup_type =
            CASE f.[type]
                WHEN 'D' THEN 'Full'
                WHEN 'L' THEN 'Log'
                WHEN 'I' THEN 'Diff'
            END
    , f.database_name
    , f.backup_start_date
    , f.backup_finish_date
    , duration = CAST(f.backup_finish_date - f.backup_start_date AS TIME)
    , f.backup_size
    , b.physical_device_name
FROM (
    SELECT
          s.media_set_id
        , s.[type]
        , s.database_name
        , s.backup_start_date
        , s.backup_finish_date
        , backup_size =
            CASE WHEN s.backup_size = s.compressed_backup_size
                THEN s.backup_size
                ELSE s.compressed_backup_size
            END / 1048576.
        , RowNum = ROW_NUMBER() OVER (PARTITION BY s.database_name, s.[type] ORDER BY s.backup_finish_date DESC)
    FROM msdb.dbo.backupset s
    --WHERE s.database_name = DB_NAME()
) f
JOIN msdb.dbo.backupmediafamily b ON f.media_set_id = b.media_set_id
WHERE f.RowNum = 1
ORDER BY f.backup_finish_date DESC

输出 -

backup_type database_name        backup_start_date    backup_finish_date   duration   backup_size   physical_device_name
----------- -------------------- -------------------- -------------------- ---------- ------------- ------------------------------------------------------------
Full        model                2016-02-25 15:14:20  2016-02-25 15:14:21  00:00:01   0.52112197    G:\model.bak
Full        kh_sql_server        2016-02-25 13:13:38  2016-02-25 13:13:38  00:00:00   0.53086376    D:\DATABASES\SQL_2012\BACKUP\kh_sql_server.bak
Full        master               2016-02-24 13:27:24  2016-02-24 13:27:26  00:00:02   32.95404911   G:\tempdb.bak
Full        AdventureWorks2012   2016-02-11 13:46:14  2016-02-11 13:46:16  00:00:02   44.70329856   E:\SHARE_BACKUP\AdventureWorks\AdventureWorks2012.bak
Full        Refactoring          2016-01-05 18:59:08  2016-01-05 18:59:10  00:00:02   37.53390598   NUL
Log         shrink_test          2016-01-05 18:56:07  2016-01-05 18:56:07  00:00:00   0.02001571    NUL
Full        shrink_test          2016-01-05 18:49:14  2016-01-05 18:49:14  00:00:00   0.45571231    NUL
Full        backup_test          2015-12-21 14:49:01  2015-12-21 14:49:02  00:00:01   0.45937156    D:\DATABASES\SQL_2012\BACKUP\backup_test.bak
Full        users                2015-11-23 13:15:44  2015-11-23 13:15:45  00:00:01   0.72006702    D:\DATABASES\SQL_2012\BACKUP\users.bak
Full        OnlineFormatFull     2015-11-06 15:08:38  2015-11-06 15:08:39  00:00:01   23.63910865   D:\DATABASES\SQL_2012\BACKUP\T20151106_OnlineFormatFull.bak

默认跟踪:

SELECT
    CASE WHEN t.EventSubClass = 1
        THEN 'BACKUP' 
        ELSE 'RESTORE'
    END, t.TextData, t.ApplicationName, t.LoginName, t.StartTime, t.EndTime
FROM sys.traces i
CROSS APPLY sys.fn_trace_gettable([path], DEFAULT) t
WHERE i.is_default = 1
    AND t.EventClass = 115 -- Audit Backup/Restore Event

【讨论】:

谢谢,TSM TDP 备份在physical_dive_name 列中显示如下:TDPSQL-000014A0-0000 纯属巧合;)))

以上是关于为 SQL Server 2008-2014 的备份状态创建视图的主要内容,如果未能解决你的问题,请参考以下文章

怎样把sql server2000数据库转化为sql server2000的数据库

sql server2000中CONVERT中各个参数的意思

装SQL server 2008问题解决

sql server 如何改变计算结果的值 如null改变为0

sql server中如何写脚本为数据表增加一个字段

sql SQL为SQL Server创建文件组和文件