简体   繁体   English

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

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

I want to transfer automatically files of last 24 hours from Remote Server to Local Server (both GNU linux). 我想自动将最近24小时的文件从远程服务器传输到本地服务器(均为GNU linux)。 I've been trying a lot with find and rsync commands (and ssh ) separately, and now I'm only able to: 我一直在尝试使用findrsync命令(和ssh ),现在我只能:

1-) Get the list of last 24 hours files in directory /FilesInRemoteServer/ on Remote Server using "find and ssh" 1-)使用“find and ssh”获取目录/FilesInRemoteServer/远程服务器上最近24小时文件的列表

2-) Transfer successfully files using rsync and ssh from Remote to Local server 2-)使用rsyncssh从远程服务器转移到本地服务器成功传输文件

My issue now is that I cannot combine find , ssh and rsync . 我现在的问题是我无法将findsshrsync结合起来。 I mean I cannot combine the output of last 24 hour's files given by find in order that rsync takes that list of files I want to transfer from stdin . 我的意思是我无法组合find给出的最后24小时文件的输出,以便rsync获取我想从stdin传输的文件列表。

This is what I've done so far: 这是我到目前为止所做的:

(1) ssh and find command (command sent in Local Server) With this combination of ssh and find I'm able to get the list of files of last 24 hours: (1) sshfind命令(在本地服务器中发送的命令)通过sshfind这种组合,我能够获得最近24小时的文件列表:

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

(2) rsync and ssh command (command sent in Local Server) With "rsync" in combination with "ssh" I can copy from Remote Server to Local Server the files from directory "/FilesInRemoteServer" in this way: (2) rsync和ssh命令(在本地服务器中发送的命令)将“rsync”与“ssh”结合使用,我可以通过这种方式从目录“/ FilesInRemoteServer”将远程服务器复制到本地服务器:

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

But when I try to combine both commands (1 and 2) is not working and get this error: 但是当我尝试将两个命令(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:

Thanks for any help. 谢谢你的帮助。

# Update: #更新:

Hello, again when I try both commands separately they work, but if I test like you suggest it asks 3 times the SSH password and I get permission denied and errors below. 您好,再次当我单独尝试这两个命令时,它们可以正常工作,但如果我按照您的建议进行测试,则会询问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).

Before using a utility, you should understand what all the options you're providing it will do. 在使用实用程序之前,您应该了解您提供的所有选项将执行的操作。 In your case, you're using the -e option. 在您的情况下,您正在使用-e选项。 What does the man page have to say about this? 该手册有什么说法呢?

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

So, -e is expecting an argument. 所以, -e期待一个争论。 It gets one in your first command: 它在你的第一个命令中得到一个:

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

But in your second, you decided not to give it an argument: 但在你的第二个,你决定不给它一个论点:

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

I'm sure if you call it properly it will start working again. 我敢肯定,如果你打电话给它,它将重新开始工作。 I find using the long version of options is a good way to remember what they're doing: 我发现使用长版本的选项是记住他们正在做的事情的好方法:

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