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?
 
bchoorCommented:
exec sp_msforeachdb @Command1 = N'BACKUP DATABASE [?] TO DISK=''%BACKUP_DIR%\?.Full.%mydate%.%XTIME%.bak'''

Added: @Command1 = N
0
 
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
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.

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