sql backup script file with sqlcmd

Hi,
I am fairly new to writing .sql files to run with sqlcmd.
I have a SQL Express server with a few database files on the server and i was hoping someone could help with writing a .sql file which i can then get the task scheduler to execute say every 3 hours.

this is what i have at the moment
sqlcmd -S CPCTRACKING\SQLEXPRESS -E -i c
:\progra~1\datash~1\dssql.sql -o c:\sql_backups\Bck_Log.txt
In my dssql.sql file i have
BACKUP DATABASE [db1] TO DISK='c:\SQL_backups\db1.bak' WITH INIT
BACKUP DATABASE [db2] TO DISK='c:\SQL_backups\db2.bak' WITH INIT
BACKUP DATABASE [db3] TO DISK='c:\SQL_backups\db3.bak' WITH INIT

What i would liek to do is first when the script runs, to delete and files in the destination folder older than 48 hrs. Then add to the .bak file a date and time stamp for example
db1_20100428_2101.bak.
s this possible?

thanks allot
LVL 4
CraigLazarAsked:
Who is Participating?
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Put this into your dssql.sql:

declare @dt varchar(13)
set @dt = convert(varchar(8), getdate(), 112) + '_' + replace(convert(varchar(5), 8), ':', '')
exec ('BACKUP DATABASE [db1] TO DISK=''c:\SQL_backups\db1_'+@dt+'.bak'' WITH INIT')
exec ('BACKUP DATABASE [db2] TO DISK=''c:\SQL_backups\db2_'+@dt+'.bak'' WITH INIT')
exec ('BACKUP DATABASE [db3] TO DISK=''c:\SQL_backups\db3_'+@dt+'.bak'' WITH INIT')

and then create the following batch file:
@echo off
forfiles  /p c:\SQL_backups /s /m *.bak /d -2 /c "cmd /c del @file'
sqlcmd  -S CPCTRACKING\SQLEXPRESS -E -i c:\progra~1\datash~1\dssql.sql ^
              -o  c:\sql_backups\Bck_Log.txt


0
 
mdagisCommented:
forfiles /p c:\SQL_backups /s /m *.* /dt-2 /c "cmd /c del @file : date >= 2 days"

sqlcmd -S CPCTRACKING\SQLEXPRESS -E -i c:\progra~1\datash~1\dssql.sql -o c:\sql_backups\Bck_Log.txt

Rename c:\SQL_backups\db1.bak c:\SQL_backups\db1_%DATE:~7,2%-%DATE:~4,2%-%DATE:~10,4%.txt
Rename c:\SQL_backups\db2.bak c:\SQL_backups\db2_%DATE:~7,2%-%DATE:~4,2%-%DATE:~10,4%.txt
Rename c:\SQL_backups\db3.bak c:\SQL_backups\db3_%DATE:~7,2%-%DATE:~4,2%-%DATE:~10,4%.txt
0
 
cyberkiwiCommented:
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
CraigLazarAuthor Commented:
Hi guys thanks for this,
i will give it a bash first thing next week
cheers
0
 
CraigLazarAuthor Commented:
hi,
do i download forfiles, install it and will it automatically integrate into SQL 2005 and SQL 2008 express?
 
thanks
0
 
CraigLazarAuthor Commented:
I went to the above link but the page was no loner available
thanks
0
 
cyberkiwiCommented:
It does not integrate to SQL per se. It  is just a command line tool to make the statements in the first comment work (batch file).

ftp://ftp.microsoft.com/reskit/y2kfix/x86/forfiles.exe
0
 
CraigLazarAuthor Commented:
hi,
i was hoping that in my sql file, i could then pass the date and time as part of the database bckup file name?
is that possible?
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Since MSSQL does not have integrated tools for deleting backup files, there are two approaches to use a batch file:

  • Either use a T-SQL script to launch a FORFILE command via xp_cmdshell (http://msdn.microsoft.com/de-de/library/ms175046.aspx), which has to be allowed in your MSSQL Express Surface features first, and create the date for backup in SQL
  • or use a batch file to perform FORFILES, and provide the date to the SQL script.
There is no big difference in doing any of them. Date formatting and calculation is much more easy in T-SQL than in a batch file, so I would prefer that.
To construct a date, do like
declare @dt varchar(13)set @dt = convert(varchar(8), getdate(), 112) + '_' + replace(convert(varchar(5), 8), ':', '')
and use the @dt variable for date.
0
 
nmcdermaidCommented:
If I had time, I would learn how to do this in Powershell, which appearrs to be an appropriate tool for the job as it allows SQL backups and file deletions all in the one scripting environment.
Unfortunately learning Powershell is one of those things I haven't had time to do, but I suggest that you do a quick search on it - you might find someone has already done this.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Even with PowerShell it is not much easier. You have to use ADO connections to execute the T-SQL commands. It looks more integrated, but indeed you do not gain much - if you don't know how to use PowerShell, that is.
0
 
CraigLazarAuthor Commented:
Thanksallot for the code.

cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.