Real-time file synchronization

Haven't used it myself but read about it recently. There is a daemon called lsyncd, which I presume does exactly what you need.

Read more about it HERE


Realtime File synchronisation between multiple servers in multi master mode

There is a good tool called lsyncd to sync files between multiple servers on real time basis. Here I have tried with two servers.

Hosts: Server1 and Server2

OS Used: CentOS 7

Install below packages on both the servers.

# yum install -y epel-release
# yum -y install lua lua-devel pkgconfig gcc asciidoc lsyncd

generate ssh-key in both the servers and add to authorized_keys file. [add public key of server1 to server2's authorized_keys and public key of server2 to server1's authorized_keys file]

Server1 configuration

Open /etc/lsyncd.conf and comment out the default configuration using -- at the beginning of the line and add below configuration to the file.

settings {
  logfile = "/var/log/lsyncd/lsyncd.log",
  delay = 1
}
sync {
  default.rsync,
  source="/home/test/public_html/",
  target="server2:/home/test/public_html/",
  rsync = {
    compress = true,
    acls = true,
    verbose = true,
    owner = true,
    group = true,
    perms = true,
    rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no"
  }
}

Change the target IP in target parameter.

You can change the delay parameter as per your requirement. Here it is set 1 second.

Now create the log directory.

# mkdir -p /var/log/lsyncd

Enable the lsyncd service to start automatically.

# systemctl enable lsyncd.service

Start the service.

# systemctl start lsyncd.service

Server2 Configuration

Follow the same configuration as Server1 and change the target IP.

Now the synchronisation is set.

You can check the activity from tailf /var/log/lsyncd/lsyncd.log

Thanks to MelBurslan for his suggestion.


Inotify-tools

Provide an interface to inotify, consisting of:

inotifywait

This command simply blocks for inotify events, making it appropriate for use in shell scripts. It can watch any set of files and directories, and can recursively watch entire directory trees.

inotifywatch

This command collects filesystem usage statistics and outputs counts of each inotify event.