Search code examples
mysqlbashpowershellsshposh-ssh

MYSQLDUMP over Powershell SSH (POSH)


I'm struggeling with this for a day now. Basically I want to backup a MySQL database on our webspace with a powershell script which runs daily on my windows computer.

When I use Putty and enter the following command, a backup file is created:

mysqldump XXXX --add-drop-table -u XXXX -p******* > backup/backup.sql

But when I run it from powershell, it will not create the backup file, even when I invoke the exact same command:

$sshsession = New-SSHSession -ComputerName $sshserver -Credential $Creds -Force -Verbose
[string]$backupcmd = "mysqldump XXXX --add-drop-table -u XXXX -p******* > backup/backup.sql"
Write-Output $backupcmd
$backupdb = Invoke-SSHCommand -SSHSession $sshsession -Command "$backupcmd"

It seems like Posh-SSH has problems with the ">" operator, maybe it does not have enough time to execute, I don't know. Also tried things like Timeout on Invoke-SSHCommand, but nothing did work yet.

I can't do stuff like crons on the remote server, it's just a webspace with limited functionalities. Also starting a bash-script does not work, I have no rights to execute scripts on the remote server.


Solution

  • If your need is specifically regarding the mysqldump command, you can use the --result-file or just -r parameter.

    In this case it would look like this:

    $ backupcmd = "mysqldump XXXX --add-drop-table -u XXXX -p ******* -r backup/backup.sql"

    I did not perform tests because I do not have POSH available at this time, but you can refer to the documentation: https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html#option_mysqldump_result-file

    Tell us it worked out this way.

    Hope this helps.