Mastering Rsync: From Basic Sync to Daemon and Real‑Time Inotify Integration
This guide explains Rsync's fundamentals, demonstrates local and LAN synchronization via SSH, details daemon deployment and configuration, provides testing commands, and shows how to achieve real‑time bidirectional file sync using inotify‑tools on Linux systems.
1. Introduction
Rsync is a remote data synchronization tool that uses the Rsync algorithm to transfer only file differences, making it fast over LAN or WAN. It runs on most Unix‑like systems and has Windows ports such as cwRsync.
2. SSH Mode
2.1 Local synchronization
# mkdir src
# touch src/{1,2,3,4}
# mkdir dest
# rsync -av src/ dest/ # sync contents of src to dest (excluding src itself)
# rsync -av src dest/ # sync the src directory itself
# rsync -avR src/ dest/ # same effect even with trailing slash2.2 LAN synchronization
# mkdir src
# touch src/{a,b,c,d}
# mkdir dest
# rsync -av 172.16.22.12:/data/test/src/ dest/ # pull from remote host
# rsync -av src/ 172.16.22.12:/data/test/dest/ # push to remote host
# rsync -av src 172.16.22.12:/data/test/dest/ # sync whole directory
# rm -rf src/d
# rsync -av --delete src/ 172.16.22.12:/data/test/dest/ # delete extraneous files on target2.3 Synchronization with a specific user
# useradd george
# passwd george
# mkdir /home/george/test
# touch /home/george/test/g{1,2,3,4}
# rsync -av src -e ssh -l george 172.16.22.12:/home/george
# rsync -av 172.16.22.12:/home/george/test/g* -e ssh -l george -p 22 dest/3. Daemon Mode
3.1 Service start methods
# yum install rsync xinetd # install packages
# /usr/bin/rsync --daemon # run as standalone daemon
# vim /etc/xinetd.d/rsync # edit xinetd config, set disable = no
# /etc/init.d/xinetd start # start xinetd
# chkconfig rsync on
# netstat -ntpl | grep 873 # verify daemon is listening3.2 rsyncd.conf configuration
Both daemon start methods require an rsyncd.conf file whose syntax resembles Samba's configuration. Global parameters control address, port, PID file, log file, timeout, etc. Module sections define the shared path and access rules.
# vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 5
timeout = 600
pid file = /var/run/rsyncd.pid
lockfile = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[web1]
path = /data/test/src
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.22.12
auth users = web
secrets file = /etc/web.passwd4. Client testing
Install rsync, create a target directory, then run typical commands:
# yum -y install rsync
# mkdir /data/test
# rsync -avzP [email protected]::web1 /data/test/
# rsync -avzP --delete [email protected]::web1 /data/test/
# rsync -avzP --exclude "*3*" [email protected]::web1 /data/test/
# echo "123" > /tmp/rsync.password && chmod 600 /tmp/rsync.password
# rsync -avzP --delete --password-file=/tmp/rsync.password [email protected]::web1 /data/test/Schedule automatic synchronization with cron:
# crontab -e
10 0 * * * rsync -avzP --delete --password-file=/tmp/rsync.password [email protected]::web1 /data/test/5. Real‑time synchronization with inotify‑tools
5.1 Install inotify‑tools
# yum install -y gcc
# mkdir /usr/local/inotify
# tar -xf inotify-tools-3.14.tar.gz
# cd inotify-tools-3.14
# ./configure --prefix=/usr/local/inotify/
# make && make install5.2 Environment setup
# echo 'export PATH=/usr/local/inotify/bin:$PATH' >> /root/.bash_profile
# source /root/.bash_profile
# echo '/usr/local/inotify/lib' >> /etc/ld.so.conf
# ldconfig
# ln -s /usr/local/inotify/include /usr/include/inotify5.3 Monitoring script (push side)
#!/bin/bash
inotifywait -mrq -e modify,create,move,delete,attrib /data/test/src |
while read events; do
rsync -a --delete /data/test/src/ 192.168.22.12:/data/test/dest
echo "`date +'%F %T'` event: $events" >> /tmp/rsync.log 2>&1
doneMake the script executable, run it in the background, and add the command to /etc/rc.local for boot‑time start. A symmetric script on the destination can pull changes, achieving bidirectional real‑time synchronization.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
