Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqldump with ssh connection in shell script

I'm using a shell script that opens a ssh connection and creates an sqldump.

But, when I use mysqldump in the shell script it gives me the error: "No such file or directory"

This is the line I use:

ssh user@host mysqldump -uusername -hlocalhost -ppassword --all-databases > /home/user/sqlfile.sql

The mysqldump works when i use it in an opened ssh connection or at the server in the commandline.

So my question is why do I get the error and why can't I run the mysqldump command in a shell script?

like image 272
9edge Avatar asked Mar 22 '12 11:03

9edge


2 Answers

You can also enclose your ssh remote commands in quotes. e.g.

ssh user@host "mysqldump -uusername -hlocalhost -ppassword --all-databases > /home/user/sqlfile.sql"

That way, if you want to scp the dump to your local folder all in one, you can:

ssh user@host "mysqldump -uusername -hlocalhost -ppassword --all-databases" > /home/user/sqlfile.sql
like image 126
eggmatters Avatar answered Sep 28 '22 15:09

eggmatters


You need to escape the > symbol. Try this:

ssh user@host mysqldump -uusername -hlocalhost -ppassword --all-databases \> /home/user/sqlfile.sql
like image 37
gogators Avatar answered Sep 28 '22 14:09

gogators