How to Achieve Real-Time Data Sync with rsync, inotify, and sersync on CentOS
This step‑by‑step guide shows how to set up two CentOS 7 virtual machines, install and configure rsync, inotify‑tools, and sersync, and combine them with scripts and cron jobs to obtain reliable, real‑time file synchronization and automated backups across the network.
This article explains how to use rsync together with inotify‑tools or sersync to achieve real‑time data synchronization between two CentOS 7 virtual machines.
1. Create virtual machines and prepare hosts
Two VMs (xy1 and xy2) are created on VMware Workstation (NAT mode) using the CentOS‑7‑x86_64‑DVD‑1708.iso image. Hostnames and IPs are set as follows:
xy1: 192.168.91.131
xy2: 192.168.91.132
Configuration steps include setting the hostname, mounting the ISO, configuring YUM sources, assigning a static IP, and installing vim.
2. Install rsync
(1) Install xinetd and rsync
<code>[root@xy1 ~]# yum install xinetd rsync -y
[root@xy2 ~]# yum install xinetd rsync -y
[root@xy2 ~]# rsync --daemon
[root@xy2 ~]# netstat -an | grep 873</code>(2) Use rsync to back up data
Backup the web root directory
/var/www/htmlon xy1 to
/web_bakon xy2.
Commands:
<code># create test user on both hosts
[root@xy1 ~]# useradd rget1 && echo "123456" | passwd --stdin rget1
[root@xy2 ~]# useradd rget1 && echo "123456" | passwd --stdin rget1
# set ACL permissions on the source directory
[root@xy1 ~]# mkdir -p /var/www/html/
[root@xy1 ~]# setfacl -R -m user:rget1:rwx /var/www/html/
[root@xy1 ~]# setfacl -R -m default:rget1:rwx /var/www/html/
# copy test data
[root@xy1 ~]# cp -r /boot/* /var/www/html/
# create destination directory and set ownership
[root@xy2 ~]# mkdir /web_bak
[root@xy2 ~]# chown rget1:rget1 -R /web_bak/
# perform the rsync transfer
[root@xy1 ~]# rsync -avz /var/www/html/ [email protected]:/web_bak/</code>(3) Backup with a non‑system user
Create
/etc/rsyncd.confwith global and module parameters, then start rsync as a daemon.
<code># edit /etc/rsyncd.conf (create if missing)
# add a welcome message
[root@xy2 ~]# echo "Welcome to Backup Server" > /etc/rsyncd.motd
# create password file
[root@xy2 ~]# echo "rsyncuser:123456" > /etc/rsync.passwd
[root@xy2 ~]# chmod 600 /etc/rsync.passwd
# start services
[root@xy2 ~]# systemctl start xinetd
[root@xy2 ~]# systemctl enable xinetd
[root@xy2 ~]# rsync --daemon --config=/etc/rsyncd.conf
[root@xy2 ~]# netstat -antup | grep 873</code>(4) Scripted automatic backup
Create a simple bash script that runs the rsync command and schedule it with cron.
<code>#!/bin/bash
rsync -avz /var/www/html/ [email protected]::wwwroot --password-file=/etc/rsync.passwd</code>Make it executable and add a cron entry, e.g.:
<code># method 1
echo "01 3 * sh /root/autobackup.sh &" >> /var/spool/cron/root
# method 2
crontab -e # then add: 01 3 * sh /root/autobackup.sh &</code>3. Real‑time sync with rsync + inotify‑tools
Increase inotify limits:
<code>fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 90000000</code>Install inotify‑tools, then run a watcher that triggers rsync on file events:
<code># install inotify‑tools
wget https://sourceforge.net/projects/inotify-tools/files/inotify-tools/3.13/inotify-tools-3.13.tar.gz
# compile and install
tar -zxvf inotify-tools-3.13.tar.gz
cd inotify-tools-3.13
./configure && make && make install
# start monitoring and syncing
#!/bin/bash
SRC=/var/www/html/
[email protected]:/web_bak
inotifywait -mrq -e create,move,delete,modify $SRC | while read a b c; do
rsync -azP --delete $SRC $DST
done</code>Make the script executable, run it, and add it to
/etc/rc.localfor boot‑time start.
4. Real‑time sync with rsync + sersync
Sersync is an inotify‑based tool that reports the exact files that changed, allowing rsync to transfer only those files.
Key steps:
Download the binary package (e.g.,
sersync2.5.4_64bit_binary_stable_final.tar.gz) and extract it to
/opt/sersync.
Edit
confxml.xmlto set the local watch path and remote rsync module:
<code><sersync>
<localpath watch="/var/www/html">
<remote ip="192.168.91.132" name="wwwroot"/>
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="rsyncuser" passwordfile="/etc/rsync.passwd"/>
</rsync>
</sersync></code>Start the sersync daemon:
<code>/opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml</code>Now any create, delete, or modify operation in
/var/www/htmlon xy1 is instantly mirrored to
/web_bakon xy2.
5. Summary
Rsync is a powerful Linux tool for incremental, compressed, and secure file synchronization. Combined with inotify‑tools it can provide near‑real‑time updates, but it syncs the whole directory each time. Sersync improves efficiency by reporting only the changed files, allowing rsync to transfer a minimal data set. Using scripts, cron jobs, or daemon services, administrators can build automated backup solutions that scale from simple push/pull models to fully automated, real‑time replication across servers.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.