我希望能够自动地将远程服务器上最近24小时的文件传输到本地服务器(两者都是GNU Linux系统)。我尝试了很多方法,用find
和rsync
命令(以及ssh
)分别进行尝试,现在我只能:
1-) 使用“find和ssh”在远程服务器上的/FilesInRemoteServer/
目录中获取最近24小时文件的列表。
2-) 成功使用rsync
和ssh
从远程服务器传输文件到本地服务器。
我的问题是无法将find
、ssh
和rsync
结合起来。也就是说,我无法将find
给出的最近24小时文件输出与rsync
所需的要传输的文件列表相结合。
这是我目前所做的:
(1) ssh
和find
命令(在本地服务器上发送的命令)
使用这种ssh
和find
的组合方法,我可以获取最近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).
ssh
”... 也可以看一下这个Unix StackExchange 答案。 - Amessihel