如何结合使用find、ssh和rsync通过时间戳传输文件

3

我希望能够自动地将远程服务器上最近24小时的文件传输到本地服务器(两者都是GNU Linux系统)。我尝试了很多方法,用findrsync命令(以及ssh)分别进行尝试,现在我只能:

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

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

我的问题是无法将findsshrsync结合起来。也就是说,我无法将find给出的最近24小时文件输出与rsync所需的要传输的文件列表相结合。

这是我目前所做的:

(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:

感谢您的任何帮助。

# 更新:

你好,当我分别尝试两个命令时,它们都可以正常工作,但是如果按照您建议的测试方法进行测试,它会要求输入SSH密码3次,然后我会收到拒绝许可和以下错误信息。

$ 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).

我不明白为什么你的rsync命令中包含“ssh”... 也可以看一下这个Unix StackExchange 答案 - Amessihel
嗨Amessihel,我在rsync中包含ssh以添加加密安全性,以便在传输时使用。 - Ger Cas
感谢您发布这个问题。这种方法比我之前的做法更加简洁! - Charlie Gorichanaz
我正在尝试以下命令:ssh -i digital.pem ubuntu@SERVER_IP 'find /home/user/import_live/content/* -mtime -5 -type f -name ".xml" -printf %P\0' | rsync -rtuzv -e 'ssh -i digital.pem' --files-from=- --from0 ubuntu@SERVER_IP:/home/user/import_live/content/ /home/user/test/我收到了以下错误信息。 rsync error: syntax or usage error (code 1) at options.c(2308) [server=3.1.1] 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] - Amarjit Datta
1个回答

2

在使用工具之前,您应该了解您提供的所有选项将会执行什么操作。在您的情况下,您正在使用-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/"

1
你的最终命令有点问题。--rsh=ssh 应该设置远程 shell 命令,而 root@192... 是源。按照你的写法,远程 shell 命令包括 ssh 和一个无效的参数,"/Destination... 是源,但没有目标。请注意,--files-from 不指定源,它只提供源处文件的列表。 - ghoti
1
谢谢,虚假引号已经被删除! - miken32
嗨miken32,谢谢你的回答。我会尝试在周一尝试你的建议,希望它能起作用。 - Ger Cas
你好miken32,感谢你的帮助。我已经尝试过了,但是我得到了权限被拒绝的错误。请查看我原始帖子中的更新部分。 - Ger Cas
当然,如果您在远程服务器上没有设置公钥身份验证,仍然会提示您输入密码。 - miken32
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接