I need a script in which Files of One server will be copied to other folder of another server ONLY AND ONLY IF the file on SOURCE SERVER file doesn't exist on DESTINATION SERVER.

Further Detail

This will be running all time on a SOURCE server if the user create the FILE on SOURCE SERVER and when the file is completed and saved it(the Command or script) first check on DESTINATION SERVER if the file exist on DEST. SERVER if not then copy if to DEST. SERVER

How to do that I have no idea of scripting.


Use rsync's --ignore-existing flag. From man rsync:

--ignore-existing       skip updating files that exist on receiver

To do this between two distinct servers you'd do something like this:

rsync --ignore-existing -avz -e ssh source remoteuser@remoteserver:destination

If you are truly going to be running this "all the time" (ie. continuously) like you say in the question, I'd highly recommend using an inotify based solution instead, which would eliminate a lot of overhead.

  • Please correct if im wrong rsync --ignore-existing -avz -e ssh source remoteuser@remoteserver:destination shouldn't the source comes BEFORE the ssh. – OmiPenguin Nov 11 '12 at 15:03
  • @UmairMustafa - No, ssh is an argument to rsync (specifically its the option for the -e parameter being passed) which specifies the program to be used to acquire the remote shell. Take a look at man rsync. – Chris Down Nov 11 '12 at 15:05
  • In most cases the -e ssh can actually be omitted, as it will default to that. – jordanm Nov 11 '12 at 17:55
  • @jordanm Is that undocumented? I can't see that in the manual anywhere... if it is undocumented I really would not recommend omitting it, especially in a script. – Chris Down Nov 11 '12 at 18:09
  • It's not really spelled out, but the documentation does explain user@host:/foo will use a remote shell and user@host::/foo will attempt to connect to an rsync daemon. I have only used -e when needing to pass extra options to ssh such as -i. – jordanm Nov 11 '12 at 18:30

Unison is another such tool that does not have the same feature set as rsync, but might be just as well suited for this partifular case.

There is a nice howto which also explains how to integrate with inotify

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.