Operations 17 min read

Mastering Rsync: From Basic Sync to Daemon and Real‑Time Inotify Automation

This guide explains Rsync's fundamentals, SSH and daemon modes, detailed configuration options, practical command examples, and how to combine it with inotify‑tools for real‑time, automated file synchronization across Linux machines.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering Rsync: From Basic Sync to Daemon and Real‑Time Inotify Automation

Introduction

Rsync (remote synchronize) is a fast file‑synchronization tool that works over LAN or WAN by transferring only the differences between source and destination files, making it much faster than full copies.

Key Features

Supports most Unix‑like systems (Linux, Solaris, BSD) and Windows versions such as cwRsync.

Can run over rsh, ssh, or as a daemon listening on port 873.

Preserves permissions, timestamps, symbolic links, and can mirror entire directory trees.

SSH Mode Examples

Local‑to‑local sync:

# mkdir src</code>
<code># touch src/{1,2,3,4}</code>
<code># mkdir dest</code>
<code># rsync -av src/ dest/    # sync contents of src to dest (exclude src itself)</code>
<code># rsync -av src dest/      # sync src directory itself

LAN sync from a remote host:

# rsync -av 172.16.22.12:/data/test/src/ dest/</code>
<code># rsync -av src/ 172.16.22.12:/data/test/dest/</code>
<code># rsync -av src 172.16.22.12:/data/test/dest/</code>
<code># rsync -av --delete src/ 172.16.22.12:/data/test/dest/

Sync as a specific user:

# useradd george && passwd george</code>
<code># mkdir /home/george/test && touch /home/george/test/g{1,2,3,4}</code>
<code># rsync -av src -e "ssh -l george" 172.16.22.12:/home/george</code>
<code># rsync -av 172.16.22.12:/home/george/test/g* -e "ssh -l george -p 22" dest/

Daemon Mode

Start the daemon directly (heavy load) or via xinetd (light load):

# yum install rsync xinetd</code>
<code># /usr/bin/rsync --daemon
# yum install rsync xinetd</code>
<code># vim /etc/xinetd.d/rsync   # set "disable = no"</code>
<code># /etc/init.d/xinetd start</code>
<code># chkconfig rsync on</code>
<code># netstat -ntpl | grep 873   # verify port 873 is listening

Configuration (rsyncd.conf)

Global parameters (similar to Samba): address, port, pid file, log file, timeout, etc.

Module parameters define which directories are shared. Example module [web1]:

uid = root</code>
<code>gid = root</code>
<code>use chroot = no</code>
<code>max connections = 5</code>
<code>timeout = 600</code>
<code>pid file = /var/run/rsyncd.pid</code>
<code>lockfile = /var/run/rsyncd.lock</code>
<code>log file = /var/log/rsyncd.log</code>
<code>[web1]</code>
<code>   path = /data/test/src</code>
<code>   read only = no</code>
<code>   write only = no</code>
<code>   hosts allow = 192.168.22.12</code>
<code>   auth users = web</code>
<code>   secrets file = /etc/web.passwd

Typical rsyncd.conf editing command:

# vim /etc/rsyncd.conf

Client Testing

# yum -y install rsync</code>
<code># mkdir /data/test

Common options: -a: archive mode (equivalent to -rlptgoD) -v: verbose -z: compress during transfer -P: show progress and keep partial files --delete: delete extraneous files on the receiver

# rsync -avzP [email protected]::web1 /data/test/</code>
<code># rsync -avzP --delete [email protected]::web1 /data/test/</code>
<code># rsync -avzP --exclude="*3*" --password-file=/tmp/rsync.password [email protected]::web1 /data/test/

Automating with Password Files and Cron

# echo "123" > /tmp/rsync.password</code>
<code># chmod 600 /tmp/rsync.password</code>
<code># rsync -avzP --delete --password-file=/tmp/rsync.password [email protected]::web1 /data/test/

Add a daily cron job:

# crontab -e</code>
<code>10 0 * * * rsync -avzP --delete --password-file=/tmp/rsync.password [email protected]::web1 /data/test/

Real‑Time Synchronization with inotify‑tools

Install inotify‑tools (C library and command‑line utilities):

# yum install -y gcc</code>
<code># mkdir /usr/local/inotify</code>
<code># tar -xf inotify-tools-3.14.tar.gz</code>
<code># cd inotify-tools-3.14</code>
<code># ./configure --prefix=/usr/local/inotify/</code>
<code># make && make install

Update PATH and library path:

# vim /root/.bash_profile</code>
<code>export PATH=/usr/local/inotify/bin/:$PATH</code>
<code># source /root/.bash_profile</code>
<code># echo '/usr/local/inotify/lib' >> /etc/ld.so.conf</code>
<code># ldconfig</code>
<code># ln -s /usr/local/inotify/include /usr/include/inotify

Typical inotify‑wait command to monitor a source directory and trigger rsync:

# /usr/local/inotify/bin/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
  done

Make the script executable and start it in the background, optionally adding it to /etc/rc.local for boot‑time execution.

Running the same script on the destination machine with reversed source/destination creates a bidirectional real‑time sync.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AutomationBackupdaemonrsyncinotifyfile synchronization
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.