Help with SQLCmd Backup Script

Hi Guys,

I want to run a command line script using taskscheduler for backing up all my databases and have them named by the date and time they were backed up. I trawled the internet for some background info and hit upon the script below. The idea is that you supply the server name, destination directory and user credentials and call the script from a bat file. I keep getting an error Incorrect systax near 'exec sp_msforeachdb

Can you cast your expert eyes over this script nd let me know what needs to be corrected.

Thanks in advance,

Joe

 @echo off  

 SET BACKUP_DIR=C:\SQLServerBackups  

 SET SERVER=xxxxxxxx\SQLEXPRESS

   

 for /f "tokens=2" %%d in ('echo %date%') do (  

    for /f "tokens=1-3 delims=/" %%j in ('echo %%d') do (  

       set month=%%j  

       set day=%%k  

       set year=%%l  

    )  

 )  

 set /a day=%day%-1  

 if %day% lss 10 (  

    set day=0%day%  

 )  

 set year=%year:~2,3%  

 set mydate=%month%%day%%year%  

 for /f "tokens=1-2 delims=: " %%a in ('time /t') do set XTime=%%a%%b  


 sqlcmd -S %SERVER% -U sa -P xxxxxxxx -d master -Q "exec sp_msforeachdb 'BACKUP DATABASE [?] TO DISK=''%BACKUP_DIR%\?.Full.%mydate%.%XTIME%.bak'''
LVL 2
dataflowjoeAsked:
Who is Participating?

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

x
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.

bchoorCommented:
exec sp_msforeachdb @Command1 = N'BACKUP DATABASE [?] TO DISK=''%BACKUP_DIR%\?.Full.%mydate%.%XTIME%.bak'''

Added: @Command1 = N
0

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
dataflowjoeAuthor Commented:
Hi there,

Thanks for answering so quickly.

I've tried the script with your change and I'm still getting the attached error message. I've pasted the entire script as it currently stands with your correction. Can you come up with any more suggestions?

Thanks again.

Joe
@echo off  

SET BACKUP_DIR=C:\SQLServerBackups  

SET SERVER=xxxxxxxx\SQLEXPRESS 

for /f "tokens=2" %%d in ('echo %date%') do (  

    for /f "tokens=1-3 delims=/" %%j in ('echo %%d') do (  

       set month=%%j  

       set day=%%k  

       set year=%%l  
    )  

 )  

set /a day=%day%-1  

if %day% lss 10 (  

   set day=0%day%  

 )  

set year=%year:~2,3%  

set mydate=%month%%day%%year%  

for /f "tokens=1-2 delims=: " %%a in ('time /t') do set XTime=%%a%%b  

sqlcmd -S %SERVER% -U sa -P xxxxxxxx -d master -Q "exec sp_msforeachdb @Command1 = N'BACKUP DATABASE [?] TO DISK=''%BACKUP_DIR%\?.Full.%mydate%.%XTIME

%.bak''

Open in new window

error.jpg
0
bchoorCommented:
try this for last line:

sqlcmd -S %SERVER% -U sa -P xxxxxxxx -d master -Q "exec sp_msforeachdb @Command1 = N'BACKUP DATABASE [?] TO DISK=''%BACKUP_DIR%\''?''.Full.%mydate%.%XTIME%.bak''"

post results, looks like there are some superfluous spaces
SET BACKUP_DIR="C:\SQLServerBackups"
0
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

bchoorCommented:

@echo off

SET BACKUP_DIR=C:\SQLServerBackups
SET SERVER=xxxxxxx\SQLEXPRESS

FOR /F "tokens=2" %%d IN ('DATE /T') DO (
	FOR /F "tokens=1-3 delims=/-" %%j IN ("%%d") DO (
		SET month=%%j
		SET day=%%k
		SET year=%%l
	)
)

SET /A day=%day%-1

IF %day% lss 10 (
	SET day=0%day%
)

SET year=%year:~2,3%
SET mydate=%month%%day%%year%

FOR /F "tokens=1-2 delims=: " %%a in ('time /t') do set XTime=%%a%%b
	
sqlcmd -S %SERVER% -U sa -P xxxxxxxx -d master -Q "exec sp_msforeachdb @Command1 = N'BACKUP DATABASE [?] TO DISK=''%BACKUP_DIR%\''?''.Full.%mydate%.%XTIME%.bak''"

Open in new window

0
bchoorCommented:
oops missed a single quote at the end there should be:
.bak'''" (so 3 single quotes, followed by double quote)
0
dataflowjoeAuthor Commented:
Brilliant it's exactly what I needed. Thanks again.
0
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

From novice to tech pro — start learning today.