Backup history

  • Hi,

    I have created a job that makes backups.

    Code sample:

    declare @date as varchar(25)

    Declare @Nome as varchar(50)

    DECLARE Backups CURSOR FOR

    SELECT name from sys.databases where state =0

    order by name

    set @date = convert(varchar,getdate(),120)

    set @date = replace(replace(@date,'-',''),':','')

    OPEN backups

    FETCH NEXT FROM backups INTO @nome

    WHILE @@FETCH_STATUS = 0

    BEGIN

    exec('backup database '+@nome+' to disk =''c:\database_backups\'+@nome+'\'+@nome+'_'+@date+'.BAK''')

    FETCH NEXT FROM backups INTO @nome

    END

    CLOSE backups

    DEALLOCATE backups

    The backups that are created by this job are name as:

    e.g: SGI_20101209 111200.BAK

    Now i would like to add to the name of the backup, the number of backup.

    If the backup is the 3 backup of this database, i would like that the name become like this:

    SGI_3_20101209 111200.BAK

    Where can i get the information of the number of backup already made to this database?

  • select count(*) from msdb..backupset where database_name = 'yourdb' and type = 'D'

    However older backup history can be purged from this table by cleanup operations so unless you never run this purge and just allow the table to grow ad infinitum the info could be wrong. This table has an identity column backup_set_id you could use as an ever increasing number, but it would not reflect the actual number of backups for that database.

    ---------------------------------------------------------------------

  • What did you meant buy "cleanup operations" ?

    Thank you

  • There's a stored procedure sp_delete_backuphistory that you can use to purge old backup records from the msdb database.

    John

  • we don't use that.

    Thanks for the tip.

  • The above proc would also be run if you use the history cleanup task in a maintenance plan and select Backup and restore history

    ---------------------------------------------------------------------

Viewing 6 posts - 1 through 5 (of 5 total)

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