Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Database backup script Expand / Collapse
Author
Message
Posted Monday, August 27, 2012 3:50 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, October 4, 2013 1:45 PM
Points: 39, Visits: 136
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_Name varchar(30) Not null,
Backup_Freq varchar(10) Not null,
Backup_Type varChar(10) Not null,
Backup_To Varchar(300) Not null,
Format char(10) Not null,
Init_NoInit char(30) Not Null,
Backup_name Varchar(50) Not null,
Skip_NoSkip Char(6) Not Null,
rewind_Norewind Char(8) Not Null,
Nounload_unload Char(8) Not Null,
Stat_report Char(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
Post #1350630
Posted Monday, August 27, 2012 4:12 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 10:19 AM
Points: 934, Visits: 1,750
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?
Post #1350636
Posted Monday, August 27, 2012 4:20 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 10:19 AM
Points: 934, Visits: 1,750
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
Post #1350639
Posted Monday, August 27, 2012 6:18 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, October 4, 2013 1:45 PM
Points: 39, Visits: 136
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
Post #1350653
Posted Tuesday, August 28, 2012 6:04 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, October 4, 2013 1:45 PM
Points: 39, Visits: 136
Well, the statement executes now that I made the syntax changes, thanks! No I have to deal with the log and cursor issues.
Post #1350858
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse