• SQL Server ->> 查看SQL Server备份历史


    找到最近数据库备份的细节信息,包括备份设备名和地址,最后备份时间

    ------------------------------------------------------------------------------------------- 
    --Most Recent Database Backup for Each Database - Detailed 
    ------------------------------------------------------------------------------------------- 
    SELECT  
       A.[Server], 
       A.database_name, 
       A.last_db_backup_date,  
       B.backup_start_date,  
       B.expiration_date, 
       B.backup_size,  
       B.logical_device_name,  
       B.physical_device_name,   
       B.backupset_name, 
       B.description 
    FROM 
       ( 
       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  
       WHERE   msdb..backupset.type = 'D' 
       GROUP BY 
           msdb.dbo.backupset.database_name  
       ) AS A 
        
       LEFT JOIN  
    
       ( 
       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, 
       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  msdb..backupset.type = 'D' 
       ) AS B 
       ON A.[server] = B.[server] AND A.[database_name] = B.[database_name] AND A.[last_db_backup_date] = B.[backup_finish_date] 
    ORDER BY  
       A.database_name 
    
    --Most Recent Database Backup for Each Database
    SELECT CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server, 
           database_name,  
           backup_start_date,
           backup_finish_date,
           Backup_duration_in_min,
           CAST(Backup_duration_in_min/60 AS VARCHAR(10))+' hour(s) and '+CAST(Backup_duration_in_min%60 AS VARCHAR(10))+' minute(s)' AS [description]
    FROM (
    SELECT ROW_NUMBER() OVER(PARTITION BY database_name ORDER BY backup_finish_date) AS row_no,
       database_name,  
       backup_start_date,
       backup_finish_date,
       DATEDIFF(MINUTE,backup_start_date,backup_finish_date) AS Backup_duration_in_min
    FROM msdb.dbo.backupset
    WHERE type = 'D' ) t
    WHERE row_no = 1
    ORDER BY  
       database_name

    查看上周数据库备份情况

    --------------------------------------------------------------------------------- 
    --Database Backups for all databases For Previous Week 
    --------------------------------------------------------------------------------- 
    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 

    过去24小时内没有备份的数据库

    ------------------------------------------------------------------------------------------- 
    --Databases Missing a Data (aka Full) Back-Up Within Past 24 Hours 
    ------------------------------------------------------------------------------------------- 
    --Databases with data backup over 24 hours old 
    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    msdb.dbo.backupset 
    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()))  

    每个数据库最后备份的时间和信息

    ------------------------------------------------------------------------------------------- 
    --Most Recent Database Backup for Each Database - Detailed 
    ------------------------------------------------------------------------------------------- 
    SELECT  
       A.[Server], 
       A.database_name, 
       A.last_db_backup_date,  
       B.backup_start_date,  
       B.expiration_date, 
       B.backup_size,  
       B.logical_device_name,  
       B.physical_device_name,   
       B.backupset_name, 
       B.description 
    FROM 
       ( 
       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  
       WHERE   msdb..backupset.type = 'D' 
       GROUP BY 
           msdb.dbo.backupset.database_name  
       ) AS A 
        
       LEFT JOIN  
    
       ( 
       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, 
       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  msdb..backupset.type = 'D' 
       ) AS B 
       ON A.[server] = B.[server] AND A.[database_name] = B.[database_name] AND A.[last_db_backup_date] = B.[backup_finish_date] 
    ORDER BY  
       A.database_name 

    从来没有备份过的数据库

    --Databases without any backup history 
    SELECT      
       CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server,  
       master.dbo.sysdatabases.NAME AS database_name,  
       NULL AS [Last Data Backup Date],  
       9999 AS [Backup Age (Hours)]  
    FROM 
       master.dbo.sysdatabases LEFT JOIN msdb.dbo.backupset 
           ON master.dbo.sysdatabases.name  = msdb.dbo.backupset.database_name 
    WHERE msdb.dbo.backupset.database_name IS NULL AND master.dbo.sysdatabases.name <> 'tempdb' 
    ORDER BY  
    
       msdb.dbo.backupset.database_name 
  • 相关阅读:
    异常:调用链顺序
    日志分割建议
    java异常处理规范
    忽略字母大小写情况下统计字符出现的次数
    十六进制转化为十进制
    搜索(剪枝优化):HDU 5113 Black And White
    水题:HDU 5112 A Curious Matt
    综合(奇技淫巧):HDU 5118 GRE Words Once More!
    网络流(费用流)CodeForces 321B:Ciel and Duel
    数学(概率)CodeForces 626D:Jerry's Protest
  • 原文地址:https://www.cnblogs.com/jenrrychen/p/4869438.html
Copyright © 2020-2023  润新知