簡體   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