Mysqldump with ssh connection in shell script

I use a shell script that opens an ssh connection and creates sqldump.

But, when I use mysqldump in a shell script, it gives me an error: "There is no such file or directory"

This is the line I'm using:

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

mysqldump works when I use it in an open ssh connection or on a server at the command line.

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

+4
source share
2 answers

You need to escape the> character. Try the following:

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

You can also enclose your remote ssh commands in quotation marks. eg.

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

Thus, if you want to copy the dump to your local folder all in one, you can:

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

Source: https://habr.com/ru/post/1402861/


All Articles