简体   繁体   中英

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). I've been trying a lot with find and rsync commands (and ssh ) separately, and now I'm only able to:

1-) Get the list of last 24 hours files in directory /FilesInRemoteServer/ on Remote Server using "find and ssh"

2-) Transfer successfully files using rsync and ssh from Remote to Local server

My issue now is that I cannot combine find , ssh and rsync . 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 .

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:

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:

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:

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.

$ 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. 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. 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/"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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