Linux 在shell脚本中使用ssh连接的mysqldump

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9821111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:20:14  来源:igfitidea点击:

mysqldump with ssh connection in shell script

linuxshellsshmysqldump

提问by 9edge

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

我正在使用打开 ssh 连接并创建 sqldump 的 shell 脚本。

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

但是,当我mysqldump在 shell 脚本中使用时,它给了我错误:“没有这样的文件或目录”

This is the line I use:

这是我使用的线路:

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

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

mysqldump当我在打开的 ssh 连接中或在命令行中的服务器上使用它时,工作正常。

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

所以我的问题是为什么我会收到错误,为什么我不能在 shell 脚本中运行 mysqldump 命令?

采纳答案by gogators

You need to escape the > symbol. Try this:

您需要转义 > 符号。尝试这个:

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

回答by eggmatters

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

您还可以将 ssh 远程命令括在引号中。例如

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