Viewing backup history with T-SQL

  • There seems to be an sp_help for everything in sql - except sp_helpbackuphistory 'my db'.

    Is there no quick way to do this with a built in function? Must I query the system's backup tables?

    I just can't believe it could be so, since this is an admins #1 duty - making sure databases are backed up.

    I'm trying to use the GUI less and my keyboard more...:cool:

    Thanks!

  • Here's my homemade version:

    SELECT sysdb.name, bkup.description, bkup.backup_finish_date,

    case

    when type='D' then '** FULL **'

    when type='I' then 'DIFFERENTIAL'

    when type='L' then 'LOG'

    end as Backup_Type,

    (STR(ABS(DATEDIFF(day, GetDate(),(backup_finish_date))))) as 'Days_Ago',

    ceiling(bkup.backup_size /1048576) as 'Size Meg' ,

    cast((bkup.backup_size /1073741824) as decimal (9,2)) as 'Gig',

    server_name, sysdb.crdate

    ,datediff(minute, bkup.backup_start_date, bkup.backup_finish_date) as 'Mins'

    ,cast(cast(datediff(minute, bkup.backup_start_date, bkup.backup_finish_date)

    as decimal (8,3))/60 as decimal (8,1)) as 'Hours', first_lsn, last_lsn, checkpoint_lsn

    FROM master.dbo.sysdatabases sysdb LEFT OUTER JOIN msdb.dbo.backupset bkup ON bkup.database_name = sysdb.name

    where backup_finish_date > DATEADD(DAY, -60, (getdate())) -- Last 60 days

    --AND sysdb.name = 'MY_DB_NAME'

    ORDER BY sysdb.name, bkup.backup_finish_date desc

  • Thanks for that.

    Made a little mod to it to take optional parameters

    example call

    sp_helpbackups 'sql_dba', 'D'

    or

    sp_helpbackups

    ALTER PROCEDURE [dbo].[sp_helpbackups]

    @MYDBNAME VARCHAR(50) = null,

    @BACKUPTYPE CHAR(1) = null

    AS

    SELECT sysdb.name,bkup.[name] AS FileName, bkup.description, bkup.backup_finish_date,

    case

    when type='D' then 'FULL'

    when type='I' then 'DIFFERENTIAL'

    when type='L' then 'LOG'

    end as Backup_Type,

    (STR(ABS(DATEDIFF(day, GetDate(),(backup_finish_date))))) as 'Days_Ago',

    ceiling(bkup.backup_size /1048576) as 'Size Meg' ,

    cast((bkup.backup_size /1073741824) as decimal (9,2)) as 'Gig',

    server_name, sysdb.crdate

    ,datediff(minute, bkup.backup_start_date, bkup.backup_finish_date) as 'Mins'

    ,cast(cast(datediff(minute, bkup.backup_start_date, bkup.backup_finish_date)

    as decimal (8,3))/60 as decimal (8,1)) as 'Hours', first_lsn, last_lsn, checkpoint_lsn

    FROM master.dbo.sysdatabases sysdb LEFT OUTER JOIN msdb.dbo.backupset bkup ON bkup.database_name = sysdb.name

    where backup_finish_date > DATEADD(DAY, -7, (getdate()))

    AND sysdb.name LIKE ISNULL(@MYDBNAME,'%')

    AND [type] LIKE ISNULL(@BACKUPTYPE,'%')

    ORDER BY sysdb.name, bkup.backup_finish_date desc

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply