0

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.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Alex
  • 434
  • 4
  • 18
  • Presumably `Invoke-SSHCommand` creates a process directly from the given commandline without running it from a shell. The redirection operator, however, is a shell builtin feature. Try changing the commandline to `"cmd /c mysqldump ... > backup/backup.sql"`. Alternatively run the remote command via [`plink`](https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter7.html#plink) (the batch execution tool from the [PuTTY suite](https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html)). – Ansgar Wiechers Nov 27 '18 at 12:07
  • Sorry , I am new . I wanted to run mysql query using sshcommand – vbhosale Dec 05 '19 at 04:21

1 Answers1

0

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.