sql backup script file with sqlcmd

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
:\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
s this possible?

thanks allot
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
CraigLazarAuthor Commented:
Hi guys thanks for this,
i will give it a bash first thing next week
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

CraigLazarAuthor Commented:
do i download forfiles, install it and will it automatically integrate into SQL 2005 and SQL 2008 express?
CraigLazarAuthor Commented:
I went to the above link but the page was no loner available
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).

CraigLazarAuthor Commented:
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?
Qlemo"Batchelor", 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.
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.
Qlemo"Batchelor", 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.
Qlemo"Batchelor", 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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CraigLazarAuthor Commented:
Thanksallot for the code.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.