0

I have been at this for a long while now, getting to the point where I'm very frustrated. I'm hoping someone can point me in the right direction.

I need to run mysqldump, for 6 specific databases. I need each spawn to run after the last has finished. It should not run all the commands at the same time. It needs to wait.

I've tried this:

$dump = "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe"
$args = @('-u','databaseUser','-pMySuperAwesomePasswordHere','--single-transaction','--log-error=c:\backups\mysqldump_error.log',"$database > $backupFilePath\$database.sql")
Start-Process $dump $args -Wait

I've tried this:

$cmd = $backupCmd + $database + " > " + "$backupFilePath\$database.sql"
Write-Host $cmd
invoke-expression $cmd | out-null

How do I execute a command, especially one with redirecting the output to a file on the filesystem?

Thank you.

Mike
  • 1,532
  • 3
  • 21
  • 45

2 Answers2

2

Redirection is a shell feature. Start-Process will dutifully include the last argument with the database name and the > in the actual argument passed to mysqldump which in turn has no idea what to do with the > at all.

You probably want something like

$args = '-u','databaseUser','-pMySuperAwesomePasswordHere','--single-transaction','--log-error=c:\backups\mysqldump_error.log',"$database"
& "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" @args | Out-File $backupFilePath\$database.sql

Since mysqldump is a console application it will wait anyway until it's done before continuing with the script.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • 1
    A useful walk-through on handling external commands can be found here: http://edgylogic.com/blog/powershell-and-external-commands-done-right/ – andyb Mar 03 '14 at 11:54
  • @andyb: `--%` is handy; didn't know that yet. – Joey Mar 03 '14 at 13:44
0

| Out-File wasn't working for me. I had to use the Parameter -RedirectStandardOutput $PATH_TO_FILE instead. HTH.

TechFanDan
  • 3,329
  • 6
  • 46
  • 89