繁体   English   中英

如何结合使用find,ssh和rsync来按时间戳传输文件

[英]How to combine find, ssh and rsync to transfer files by timestamp

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

1-)使用“find and 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/"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM