在 shell 脚本中,sftp put 命令失败

发布于 2024-12-24 19:35:16 字数 474 浏览 2 评论 0原文

我正在尝试制作一个 shell 脚本来创建 mysql 转储,然后将其放在另一台计算机上。我已经设置了 keyless ssh 和 sftp。下面的脚本在运行时在本地计算机上创建 mysql 转储文件,并且不会引发任何错误,但是文件“dbdump.db”永远不会放在远程计算机上。如果我执行 sftp 连接并手动输入命令,那么它就可以工作。

mysql_backup.sh 的内容

mysqldump --all-databases --master-data > dbdump.db
sftp -b /home/tim [email protected] <<EOF
put dbdump.db
exit
EOF

I am trying to make a shell script that creates a mysql dump and then puts it on another computer. I have already set up keyless ssh and sftp. They script below creates the mysql dump file on the local computer when it is run and doesn't throw any errors, however the file "dbdump.db" is never put on the remote computer. If I execute the sftp connection and put command by hand then it works.

contents of mysql_backup.sh

mysqldump --all-databases --master-data > dbdump.db
sftp -b /home/tim [email protected] <<EOF
put dbdump.db
exit
EOF

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

依 靠 2024-12-31 19:35:16

尝试使用 scp ,这对于您的情况应该更容易。

scp dbdump.db [email protected]:/home/tim/dbdump.db

sftp 和 scp 都使用 SSH。

Try to use scp that should be easier in your case.

scp dbdump.db [email protected]:/home/tim/dbdump.db

Both sftp and scp are using SSH.

画中仙 2024-12-31 19:35:16

请将 mput/put 命令写入一个文件 (file_contains_put_command) 并尝试以下命令。

sftp2 -B file_contains_put_command /home/tim [email protected] >> log_file

例子:

echo binary > sample_file
echo mput dbdump.db >> sample_file
echo quit >> sample_file
sftp2 -B sample_file /home/tim [email protected] >> log_file

Please write mput/put command into one file (file_contains_put_command) and try below command.

sftp2 -B file_contains_put_command /home/tim [email protected] >> log_file

Example:

echo binary > sample_file
echo mput dbdump.db >> sample_file
echo quit >> sample_file
sftp2 -B sample_file /home/tim [email protected] >> log_file
任谁 2024-12-31 19:35:16

不过,您最初的方法还差几个字符。您告诉 sftp 从 /home/tim -b /home/tim 读取它的批处理命令。因此,如果将其更改为 -b -,它应该从标准输入读取批处理命令。

沿着这些思路,如果 -b /home/tim 旨在远程更改目录,您可以将 cd /home/tim 添加到您的此处文档中。

mysqldump --all-databases --master-data > dbdump.db
sftp -b - [email protected] <<EOF
put dbdump.db
exit
EOF

Your initial approach is a few characters off working though. You're telling sftp to read it's batch-commands from /home/tim -b /home/tim. So, if you change this to -b -, it should read it's batch commands from stdin.

Something along these lines, if -b /home/tim were intended to i.e. change directory remotely, you can add cd /home/tim to your here document.

mysqldump --all-databases --master-data > dbdump.db
sftp -b - [email protected] <<EOF
put dbdump.db
exit
EOF
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文