Bidirectional Real-Time Remote File Sync with inotify+rsync and NFS

        I encountered a requirement in a project to synchronize files in real time between two machines, and tried the following two approaches:
Project requirement: Machine A (190) and Machine B (217) need a folder’s contents to be identical to each other. Both Machine A and Machine B might receive files independently and need to sync them to the other party.
Method 1: Use rsync-server with inotify for file synchronization
1. rsync-server configuration (217)

1
2
3
4
5
6
7
8
9
10
11
12
13
# vi /etc/xinetd.d/rsync
# default: off
#       allows crc checksumming etc.
service rsync
{
        disable = no
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}

2. Specify file location

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# vi /etc/rsyncd.conf
uid = root
gid = root
use chroot = yes
max connections = 4
strict modes = yes
syslog facility = local5
port = 873
[backup]
path = /home/rsync/test/  ## Folder to synchronize
comment = This is a test
ignore errors
read only = no   ## Write permission
list = yes
auth users = rsync
secrets file = /etc/rsync.pas   ## Auth file, must also exist on the peer machine
hosts allow = 192.168.1.190 ## Client IP

3. Create rsync.pas

1
2
# vi /etc/rsync.pas
sync:test

4. Client sync script (190)

1
2
3
4
5
6
7
8
#vi inotify_rsync.sh
#!/bin/bash
SRC=/home/rsync/test/
DST=rsync@192.168.1.217::backup
/usr/bin/inotifywait -mrq -e modify,<del datetime="2011-11-07T01:44:44+00:00">delete</del>,create,attrib ${SRC} | while read D E F
do
/usr/bin/rsync -ahqzt --progress <del datetime="2011-11-07T01:44:44+00:00">--delete</del> --password-file=/etc/rsync.pas $SRC $DST
done

5. Configure script to run at startup

1
# cat "/root/inotify_rsync.sh &" >> /etc/rc.local

6. Implementing two-way sync and its issues The above only implements syncing from machine A (190) to machine B (217). To achieve two-way sync, set up an rsync server on machine A (190) by adapting the configuration. However, delete operations cannot be performed because there is no way to determine which machine’s operations take precedence, which would cause newly created files to be deleted.

Method 2: Using NFS shared server for file synchronization
1. Configure NFS

1
2
3
4
5
#service portmap start
#service nfs start
#vi /etc/exports
/home/rsync/test *(rw,sync,no_root_squash) ##rw: readable and writable permissions; sync: write data synchronously to storage; no_root_squash: users logging into the NFS host as ROOT will have ROOT privileges
# exportfs -rv  ##Reload the configuration

2. Mount the NFS shared directory Before mounting, use SSH to connect the two machines first, so you don’t need to configure authentication. Then write it into the /etc/fstab file, and it’s done.

1
mount -t nfs 192.168.1.190:/home/rsync/test /home/rsync/test

Final Thoughts: Method one keeps files on both machines simultaneously, offering real-time performance and security, but it is only suitable for environments where files are not deleted. Method two keeps files only on the NFS server. If combined with rsync server for scheduled backups to another location, manual recovery is possible in case of failure, which is also a good choice.

This article is from the “kras-linux” blog. Please be sure to keep this source link http://klinux.blog.51cto.com/2972664/708294

Leave a Comment

Your email address will not be published.