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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

A useful walk-through on handling external commands can be found here: edgylogic.com/blog/powershell-and-external-commands-done-right
@andyb: --% is handy; didn't know that yet.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.