How can we automaitcally sync/copy files from one directoy to another ?

Hi,

I would like to achieve below requirement,

I have a directory "/mydir" and I want to automatically sync/copy all the content of /mydir directory to "/yourdir" directory all the time.
meaning, if some application creates a file in /mydir, it supposed to be copied/available in "/yourdir" directory

I need to have two copies.

Can we do this using rsync ? or hard link ? (*both /mydir and /yourdir available in same server)

please explain. Thanks.

the link will not result in "two copies" as you say. The rsync is ideal tool for this task IMO.

the simplest form would be

rsync -a source target

If you delete a file from your source and want it to be deleted from your target rsync does that as well.

As migurus said, a hard link doesn't create a copy of a file; it creates another name for a single file.

And, even if /mydir and /yourdir are on the same server, you can't create a hard link between files in those directories unless both of those directories are in the same filesystem (i.e. under the same mount point) on that server.

And to constantly check if the content of a directory has changed, you would need to write either:

  • Your own service, which then invokes rsync
  • A script that checks your path every single second, and if there was a change, start rsync - if it is not alredy running...

Which both is about the same in the effect, obviously...

You do NOT want start a new instance to rsync syncinc a multi GB large folder to another ever second.

hth

Hi.

Linux has a feature called intotify:

DESCRIPTION
       The  inotify API provides a mechanism for monitoring filesystem events.
       Inotify can be used to monitor individual files, or to monitor directo
       ries.   When  a  directory is monitored, inotify will return events for
       the directory itself, and for files inside the directory.

which is accessible from the shell with:

 inotify-tools is a set of command-line programs for Linux providing a
 simple interface to inotify. These programs can be used to monitor and
 act upon filesystem events. inotify-tools consists of two utilities:
 .
 inotifywait simply blocks for inotify events, making it appropriate
 for use in shell scripts.
 .
 inotifywatch collects filesystem usage statistics and outputs counts
 of each inotify event.

So one could run inotifywait which would block execution of a script until there was a change, then the script could call rsync , then start all over again.

Best wishes ... cheers, drl