LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   ssh help with scripting a remote command using redirect (https://www.linuxquestions.org/questions/programming-9/ssh-help-with-scripting-a-remote-command-using-redirect-4175481736/)

jbeiter 10-22-2013 10:17 AM

ssh help with scripting a remote command using redirect
 
Hoping someone can help me find a way around this.

I have a script that does a mysql dump, transfers it to a standby system then I want to import that into the remote mysql.

It's restricted to run one command that parses input for bad stuff then when satisfied it's only running mysql, runs the command which is passed as a variable.

my problem is, ssh fails to parse a redirect. eg:

ssh user@remotehost "mysql -v -pmy_password < file_I_just_scped"

I get a syntax error from mysql. I can't run bash (to parse the < correctly) because bash won't run the binary.

Anyone know of a clever way around this, besides using a script on the remote system?

evo2 10-23-2013 03:09 AM

Hi,

you shouldn't need to scp a sript or even the file, instead pipe it into ssh. Eg
Code:

cat file_that_I_dont_need_to_scp | ssh user@remotehost mysql -v -pmy_insecurepassword
Evo2.

evo2 10-23-2013 03:11 AM

Hi,

hmm, just realized that you don't even need the temp file. How about:
Code:

mysqldump somedatabase | ssh user@remotehost mysql -v -ppassword
Evo2.

jbeiter 10-23-2013 07:13 AM

Hmm I still get a syntax error from mysql. Do I need a "<" at the end of it or should it just take the dump from the pipe?

I was able to get mysql to interpret the redirection by invoking it with sudo. I like your suggestion better though, if I can get it to work.


mysqldump -ppassw --add-drop-table --all-databases | ssh user@remotehst /usr/bin/mysql -v -ppassw

yields a syntax error from the remote mysql.

Thank you for the assistance.

jbeiter 10-23-2013 10:05 AM

I don't think it was playing nice with the pipe, for some reason unknown to me. I got it to work by running this on the remote/destination host:

mysql -v -ppassw < <(ssh user@source-host "mysqldump -ppassw --all-databases")

jbeiter 10-25-2013 01:10 PM

Quote:

Originally Posted by jbeiter (Post 5050952)
mysql -v -ppassw < <(ssh user@source-host "mysqldump -ppassw --all-databases")

can anyone tell me why that works fine from the command line, but if I put it into a script I get this syntax error?:

sync_mysql.sh: line 122: syntax error near unexpected token `<'
sync_mysql.sh: line 122: `/usr/bin/mysql -v -ppass < <(ssh user@source-host "mysqldump -ppassw --add-drop-table --all-databases")'

ntubski 10-25-2013 01:56 PM

You should put #!/bin/bash at the top of your script. The <() construct is bash specific.

jbeiter 10-25-2013 02:29 PM

yes, it's in there. That was just a snippit of a larger file

ntubski 10-25-2013 05:26 PM

Quote:

Originally Posted by jbeiter (Post 5052326)
yes, it's in there.

Oh, it seems like the script is being run with /bin/sh instead of /bin/bash, if you do have #!/bin/bash, maybe you are actually running the script by sh myscript.sh? Otherwise, I don't know...


All times are GMT -5. The time now is 04:17 AM.