Database backup script

  • I have been tasked with creating a sql table that contains database backups.

    The idea is to have all databases listed in rows that can be read by a script that will execute a backup for each row. I know SQL Server agent can do this but there are reasons that this needs to be in this format. In the following script I have created a table and populated it with an adventureworks database. The table is built so that the fields can be concatenated to form a Backupstatement(With some text added in).

    Use Adventureworks;

    If object_ID('Adventureworks.dbo.Db_Backups') is not null

    Drop table Adventureworks.dbo.DB_Backups

    Create Table Adventureworks.dbo.DB_Backups

    (

    database_Namevarchar(30)Not null,

    Backup_Freqvarchar(10)Not null,

    Backup_TypevarChar(10)Not null,

    Backup_ToVarchar(300)Not null,

    Formatchar(10)Not null,

    Init_NoInitchar(30) Not Null,

    Backup_nameVarchar(50)Not null,

    Skip_NoSkipChar(6)Not Null,

    rewind_NorewindChar(8)Not Null,

    Nounload_unloadChar(8)Not Null,

    Stat_reportChar(3)Not null)

    GO

    Insert into Adventureworks.dbo.DB_Backups

    values

    (

    'Adventureworks',

    'Daily',

    'FUll',

    '''C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\Adventureworks_backup_2012_08_27_093633_3380858.bak''',

    'NOFORMAT',

    'NOINIT',

    '''Adventureworks-Full Database Backup''',

    'SKIP',

    'NOREWIND',

    'NOUNLOAD',

    100)

    Go

    Select *

    FROM Adventureworks.dbo.DB_Backups

    Declare @StrSql varchar(1000)

    Set @strsql =

    'Backup Database' + ' ' + (Select Database_Name From Adventureworks.dbo.Db_Backups)

    + ' ' + 'To Disk' + ' ' + '=' + ' ' + (Select Backup_to From Adventureworks.dbo.Db_Backups)

    + ' ' + (Select Format From Adventureworks.dbo.Db_Backups) + ','

    + ' ' + (Select Init_NoInit From Adventureworks.dbo.db_Backups) + ','

    + ' ' +'Name' + ' ' + '=' + ' ' + (Select Backup_name From Adventureworks.dbo.db_Backups) + ','

    + ' ' + (Select Skip_Noskip From Adventureworks.dbo.db_backups) + ','

    + ' ' + (Select rewind_Norewind From Adventureworks.dbo.db_Backups) + ','

    + ' ' + (Select Nounload_unload From Adventureworks.dbo.db_Backups) + ','

    + ' ' +'Stats =' + ' ' + ' ' + (Select Stat_report From Adventureworks.dbo.db_backups)

    Exec @strsql

    GO

    I am not too experienced with stored procedures and don't know it that is the way to go. I also tried making one column that contained a complete backup statement, but when I tried to execute the command on that column I got the same results.

    Any help is appreciated or alternative suggestions,

    Thanks,

    GH

  • As it's currently written your sql will only work if you have a single database listed in your backups table.

    I think I would use a cursor to loop down your table a row at a time.

    For each row then build up the sql statement (similar to your sql)

    and then exec it

    You need to think about how errors will be handled. What happens if the first database in the list can't be backed up. Does the script continue, how will you be alerted?

    What's the error message you are currently seeing?

  • two things wrong with the sql. You need to add + ' WITH ' + to the second line. And the exec command needs brackets.

    Declare @StrSql varchar(1000)

    Set @strsql =

    'Backup Database' + ' ' + (Select Database_Name From dbo.Db_Backups)

    + ' ' + 'To Disk' + ' ' + '=' + ' ' + (Select Backup_to From dbo.Db_Backups) + ' WITH ' +

    + ' ' + (Select Format From dbo.Db_Backups) + ','

    + ' ' + (Select Init_NoInit From dbo.db_Backups) + ','

    + ' ' + 'Name' + ' ' + '=' + ' ' + (Select Backup_name From dbo.db_Backups) + ','

    + ' ' + (Select Skip_Noskip From dbo.db_backups) + ','

    + ' ' + (Select rewind_Norewind From dbo.db_Backups) + ','

    + ' ' + (Select Nounload_unload From dbo.db_Backups) + ','

    + ' ' + 'Stats =' + ' ' + ' ' + (Select Stat_report From dbo.db_backups)

    print @strsql

    Exec (@strsql)

    GO

  • I have left work but I recall the message stating that the database name was not valid or something similar, I'll check it out tomorrow. Sometimes I was getting a syntax error too regarding an apostrophe. I think that may have been related to the "with" that the other post pointed out. I will check it out tomorrow, thanks for your help and the tips about the errors!

    GH

  • Well, the statement executes now that I made the syntax changes, thanks! No I have to deal with the log and cursor issues. :crazy:

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

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