bash - 如何结合find、ssh和rsync按时间戳传输文件

标签 bash ssh find rsync

我想将最近 24 小时的文件从远程服务器自动传输到本地服务器(均为 GNU linux)。我一直在分别尝试使用 findrsync 命令(以及 ssh),现在我只能:

1-) 使用“查找和 ssh”获取远程服务器上目录 /FilesInRemoteServer/ 中最近 24 小时文件的列表

2-) 使用rsyncssh 从远程服务器成功传输文件到本地服务器

我现在的问题是我无法组合使用 findsshrsync。我的意思是我无法合并 find 给出的最近 24 小时文件的输出,以便 rsync 接受它 我要从 stdin 传输的文件列表。

这是我到目前为止所做的:

(1) sshfind 命令(本地服务器发送的命令) 通过 sshfind 的组合,我能够获得最近 24 小时的文件列表:

ssh root@192.168.X.X 'find /FilesInRemoteServer/ -mtime -1 -type f -printf %P\\0'

(2) rsync 和 ssh 命令(本地服务器发送的命令) 将“rsync”与“ssh”结合使用,我可以通过这种方式将目录“/FilesInRemoteServer”中的文件从远程服务器复制到本地服务器:

rsync -avzhe ssh root@192.168.X.X:/FilesInRemoteServer/ "/DestinationInLocalServer/"

但是当我尝试组合这两个命令(1 和 2)时,它不起作用并出现此错误:

ssh root@192.168.X.X 'find /FilesInRemoteServer/ -mtime -1 -type f -printf %P\\0' |
rsync -avzhe --files-from=- -0 ssh root@192.168.X.X:/FilesInRemoteServer/ \
      "/DestinationInLocalServer/" 
Unexpected remote arg: root@192.168.X.X:/FilesInRemoteServer/
rsync error: syntax or usage error (code 1) at main.c(1330) [sender=3.1.1]
root@192.168.X.X's password:

感谢您的帮助。

#更新:

你好,当我分别尝试这两个命令时,它们仍然有效,但如果我按照你的建议进行测试,它会询问 3 次 SSH 密码,我得到权限被拒绝并出现以下错误。

$ ssh root@192.168.X.X 'find /FilesInRemoteServer/ -mtime -1 -type f -printf %P\\0' | rsync --archive --verbose --compress --human-readable --files-from=- --from0 --rsh=ssh root@192.168.X.X:/FilesInRemoteServer/ /DestinationInLocalServer/
root@192.168.X.X's password: root@192.168.X.X's password:

Permission denied, please try again.
root@192.168.X.X's password: Permission denied, please try again.
root@192.168.X.X's password:

Permission denied, please try again.
root@192.168.X.X's password: Permission denied, please try again.
root@192.168.X.X's password:

Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(226) [Receiver=3.1.1]
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

最佳答案

在使用实用程序之前,您应该了解您为其提供的所有选项的作用。在您的情况下,您使用的是 -e 选项。手册页对此有何说明?

  -e, --rsh=COMMAND
          This option allows you to choose an alternative remote shell program to use for communication between the  local
          and  remote copies of rsync. Typically, rsync is configured to use ssh by default, but you may prefer to use rsh
          on a local network.

          If this option is used with [user@]host::module/path, then the remote shell COMMAND will be used to run an rsync
          daemon  on  the  remote host, and all data will be transmitted through that remote shell connection, rather than
          through a direct socket connection to a running rsync daemon on the remote host.  See the section "USING  RSYNC-
          DAEMON FEATURES VIA A REMOTE-SHELL CONNECTION" above.

因此,-e 需要一个参数。它在您的第一个命令中得到一个:

rsync -avzhe ssh root@192.168.X.X:/FilesInRemoteServer/ "/DestinationInLocalServer/"

但是在你的第二个,你决定不给它一个参数:

rsync -avzhe --files-from=- -0 ssh root@192.168.X.X:/FilesInRemoteServer/ \
  "/DestinationInLocalServer/"

我敢肯定,如果您正确调用它,它会再次开始工作。我发现使用长版本的选项是记住它们在做什么的好方法:

ssh root@192.168.X.X 'find /FilesInRemoteServer/ -mtime -1 -type f -printf %P\\0' |
rsync --archive --verbose --compress --human-readable --files-from=- --from0 \
    --rsh=ssh root@192.168.X.X:/FilesInRemoteServer/ \
    "/DestinationInLocalServer/"

关于bash - 如何结合find、ssh和rsync按时间戳传输文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35510835/

相关文章:

php - 如何替换 Netbeans 中的字符串?

linux - 测试命令和重定向 bash

linux - ssh 连接在 Raspberry Pi 上被拒绝

php - fatal error : Call to undefined function mysqli_connect

amazon-web-services - 用于 SSH 的 Amazon EC2 实例连接 - 安全组?

makefile - 使用 GNU Make 查找 Unix 以自动更新文件

Bash in vi-mode : browsing shell history with cursor up/down, 光标位置不在末尾?

linux - Bash 获取等待进程的退出代码

linux 对话框 --checklist 错误

VBA Access - 在 Word 文档中查找和替换文本