Jump to content

SQLCMD Automation


Nunos
 Share

Recommended Posts

I am trying to write a script to backup a SQL DB that I need to automate. 

I have been told that it can be done in a batch file with the syntax below but the parameters may need changed.

SQLCMD -S.\SQLSERVER -E -Q "BACKUP DATABASE DBName TO DISK='C:\DBBackup\DBBackup.bak' WITH INIT" -oC:\DBBackup\DBBackupLog.log

So I tried writing the below which executes with a Exit Code = 0 but never actually does the backup. 

#RequireAdmin


$Server = ".\SQLServer"
$DBName = "TheDBName"
$Destination = "C:\DBBackup\DBBackup.bak"
$LogFile = "C:\DBBackup\DBBackup.Log"

Run("sqlcmd -S " & $Server  & ' -E ' & ' -Q "BACKUP DATABASE' & $DBName 'TO DISK=' & $Destination)

MsgBox(4096, "", "Done")

It also never displays the MsgBox. I have tried Run, RunWait, ShellExecute, and ShellExecuteWait.

Can someone please tell me what I am missing?

Thank you

Link to comment
Share on other sites

Do the following and I think you will start to see the problems

$Server = ".\SQLServer"
$DBName = "TheDBName"
$Destination = "C:\DBBackup\DBBackup.bak"
$LogFile = "C:\DBBackup\DBBackup.Log"

ConsoleWrite("sqlcmd -S " & $Server  & ' -E ' & ' -Q "BACKUP DATABASE' & $DBName 'TO DISK=' & $Destination & @crlf)

 

Link to comment
Share on other sites

Thank you for the help Danp2. 

 

==> Unable to parse line.:
ConsoleWrite("sqlcmd -S " & $Server & ' -E ' & ' -Q "BACKUP DATABASE' & $DBName 'TO DISK=' & $Destination & @crlf)
ConsoleWrite("sqlcmd -S " & $Server ^ ERROR
>Exit code: 1    Time: 0.04998

 

I am not knowledgeable enough to know what that means. Does it not like -E switch or the &'s?

Link to comment
Share on other sites

Ok.. there was a missing amperand. Try this --

$Server = ".\SQLServer"
$DBName = "TheDBName"
$Destination = "C:\DBBackup\DBBackup.bak"
$LogFile = "C:\DBBackup\DBBackup.Log"

ConsoleWrite("sqlcmd -S " & $Server  & ' -E ' & ' -Q "BACKUP DATABASE' & $DBName & 'TO DISK=' & $Destination & @crlf)

and then compare the output to your original command.

Link to comment
Share on other sites

(16) : ==> Unable to parse line.:
ConsoleWrite("sqlcmd -S " & $Server & ' -E ' & ' -Q "BACKUP DATABASE' & $DBName & 'TO DISK=' & $Destination & @crlf)
ConsoleWrite("sqlcmd -S " & $Server ^ ERROR
>Exit code: 1    Time: 0.04897
 

Same error. I also just noticed that I am missing part of the batch file in my script at the end of the destination name where it says WITH INIT" -oC:\DBBackup\DBBackupLog.log 

Would that require another & and single quotes around the WITH INIT then another & for the -o and then the log variable and quotes again?

I am in & and quote nightmare here. :sweating:

Link to comment
Share on other sites

>Running:(3.3.14.2):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\Scripts\DBBU.au3"   
--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
sqlcmd -S .\SQLServer -E  -Q "BACKUP DATABASEthedbnameTO DISK=C:\DBBackup\DBBackup.bakWITH INIT
+>16:21:19 AutoIt3.exe ended.rc:0
+>16:21:19 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 0.4921

 

Found an extra space between the '-E ' and the &.

 

The above is the output. Should I next try to change the consolewrite to another option? If so what one do you suggest? 

 

UPDATE:

Tried Run and it seems to run but still no actual backup. 
 

Edited by Nunos
Link to comment
Share on other sites

sqlcmd -S.\SQLServer -E  -Q "BACKUP DATABASE thedbname TO DISK=C:\DBBackup\DBBackup.bak WITH INIT+>12:16:54 AutoIt3.exe ended.rc:0

 

That is the result of the ConsoleWrite does that seem right? And the below is what I have now.

ConsoleWrite("sqlcmd -S"&$Server & ' -E ' & ' -Q "BACKUP DATABASE ' & $DBName & ' TO DISK=' & $Destination & " WITH INIT")

 

 

Link to comment
Share on other sites

  • Developers

Don't you need a space between -S and $Server?: 

ConsoleWrite('sqlcmd -S ' & $Server & ' -E  -Q "BACKUP DATABASE ' & $DBName & ' TO DISK=' & $Destination & ' WITH INIT"')

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Wouldn't it be easier to revise the example Microsoft gives to create an automated process to backup a database?

https://support.microsoft.com/en-us/help/2019698/how-to-schedule-and-automate-backups-of-sql-server-databases-in-sql-se

Edit: This one may be of use

https://blogs.msdn.microsoft.com/sqlagent/2010/10/12/create-a-database-backup-job-using-sql-server-management-studio/

Edited by aa2zz6
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...