Welcome to our website.

How rsync and sersync Work Together for Real-Time File Sync on Linux

What rsync is and why it is still widely used

rsync is an open-source synchronization and backup tool designed for fast local and remote data transfer. It supports both full and incremental synchronization, which makes it useful for everything from scheduled backups to cross-server data replication.

Its practical strengths include:

  1. It can copy special files such as symlinks and device files.
  2. It supports excluding specific files or directories during sync, similar to exclusion behavior in tar.
  3. It can preserve permissions, timestamps, symbolic links, hard links, owner, and group information.
  4. It supports incremental transfer, so only changed data is sent.
  5. It can work over rcp, rsh, and ssh for transport, although rsync itself does not encrypt data.
  6. It can also transfer data through a socket in daemon mode.
  7. It supports anonymous or authenticated daemon-mode transfers without requiring a normal system login user.
  8. In some usage patterns, it can also act a bit like ls, since it can list remote module contents.

In production, rsync is commonly used in two scenarios:

  • Scheduled synchronization between two servers using cron + rsync
  • Real-time synchronization to reduce single points of failure on storage or application servers

Rsync + inotify-tools vs. Rsync + sersync

These two architectures are often grouped together because both use filesystem event monitoring to trigger synchronization, but they behave differently when the data set becomes large.

Rsync + inotify-tools

With inotify-tools, you can detect that something in a watched directory changed, including file creation, deletion, or modification. The limitation is that it does not keep a practical synchronization-oriented record of exactly which file or directory changed in the way sersync does for rsync execution.

As a result, when rsync runs, it typically has to rescan and compare the entire directory tree. If the directory contains a lot of data, this becomes slow because rsync must traverse the whole directory to determine what changed.

That means:

  • directory changes are detected
  • exact sync targeting is weak
  • full directory comparison happens repeatedly
  • efficiency drops sharply when the number of files is high

Rsync + sersync

sersync can record the specific file or directory that changed in the monitored path, including create, delete, and modify events.

When rsync is triggered in this model, it only needs to synchronize the changed file or changed directory. Since each change is usually tiny compared with the full sync tree, rsync can complete the comparison and transfer much faster.

That means:

  • precise change tracking
  • targeted sync of changed paths only
  • much better performance when the directory is large
  • more suitable for hundreds of GB or even TB-scale trees with many files

How the sync flow works

A typical deployment looks like this:

  1. Start sersync on the source server.
  2. sersync watches configured filesystem paths for event changes.
  3. When a change occurs, it calls rsync to push the updated content to the target server.
  4. The source server runs sersync.
  5. The destination server runs rsync in daemon mode.

In other words, sersync is responsible for local event monitoring, and rsync is responsible for the actual transfer.

A simpler way to think about it is:

  • users write or update files on the source server
  • sersync detects those changes in real time
  • rsync pushes the corresponding files to the target machine through the rsync daemon

Which one should you choose?

  • If the synchronized directory is small, Rsync + Inotify-tools is usually enough.
  • If the synchronized directory is very large and contains many files, Rsync + sersync is the better fit.

Example deployment layout

This example uses two servers:

  • Rsync server (backup target): 192.168.0.10
  • Sersync server (source/data origin): 192.168.0.20

Goal:

  • Back up /backup on 192.168.0.20 to /backup on 192.168.0.10

Roles:

  • 192.168.0.10: rsync daemon server, dedicated backup/storage node
  • 192.168.0.20: sersync source server, where project files or website data live

Step 1: Install rsync

Both servers need rsync installed:

yum -y install xinetd rsync

The source server 192.168.0.20 also needs sersync.

Step 2: Configure the rsync daemon on the backup server

The destination server uses /etc/rsyncd.conf, creates a backup account, and runs rsync in daemon mode.

rsyncd.conf

#!/bin/bash
uid = root
gid = root
use chroot = no
disable = no
fake super = yes
max connections = 200
timeout = 300
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsync.lock
log file = /var/log/rsyncd.log
[backup]
path = /backup
ignore errors
read only = false
list = false
hosts allow = 192.168.0.0/24
host deny = 0.0.0.0/0
auth users = rsync_backup
secrets file = /etc/rsync.password
[root@backup ~]# useradd -M -s /sbin/nologin rsync #可以使用rsync,我使用的是root
[root@backup backup]# mkdir /backup/    创建备份的目录

Create the password file

[root@backup ~]# echo 'rsync_backup:123456' >/etc/rsync.password
[root@backup ~]# cat /etc/rsync.password
rsync_backup:123456

Set file permissions

[root@backup ~]# chmod 600 /etc/rsync.password
[root@backup ~]# ll /etc/rsync.password
-rw-------. 1 root root 20 Jul 24 04:27 /etc/rsync.password

Start the rsync service

[root@backup ~]# rsync -–daemon

Enable startup on boot

[root@backup ~]# vim /etc/rc.local
# rsync server progress
/usr/bin/rsync --daemon
1.5.7 第六个里程碑-检查 rsync的端口号为873
[root@backup ~]# ss -tlunp|grep rsync
tcp    LISTEN     0      5         :::873                    :::*                   users:(("rsync",2452,5))
tcp    LISTEN     0      5          *:873                     *:*                   users:(("rsync",2452,4))

Restart rsync if needed

[root@rsync-client-sersync ~]# ps -ef | grep rsync
root      1287     1  0 22:52 ?        00:00:00 rsync --daemon
root      1294  1248  0 22:52 pts/0    00:00:00 grep --color=auto rsync
[root@rsync-client-sersync ~]# kill -9 1287
[root@rsync-client-sersync ~]# ps -ef | grep rsync
root      1298  1248  0 22:53 pts/0    00:00:00 grep --color=auto rsync
[root@rsync-client-sersync ~]# rsync --daemon
[root@rsync-client-sersync ~]# ps -ef | grep rsync
root      1300     1  0 22:53 ?        00:00:00 rsync --daemon
root      1302  1248  0 22:53 pts/0    00:00:00 grep --color=auto rsync
重启,就是杀掉rsync的进程,重新rsync --daemon

Step 3: Test rsync from the client

A basic push test from 192.168.0.20:

[root@rsync-client-sersync ~]# rsync -avz /etc/hosts [email protected]::backup
Password:
sending incremental file list
hosts

sent 140 bytes  received 43 bytes  33.27 bytes/sec
total size is 158  speedup is 0.86
成功的将hosts文件推向服务器端的/backup目录中

To avoid interactive password entry, create a client-side password file:

[root@nfs01 tmp]# echo '123456' >/etc/rsync.password   将密码重定向到/etc/rsync.password文件
[root@nfs01 tmp]# cat /etc/rsync.password   查看
123456
[root@nfs01 tmp]# chmod 600 /etc/rsync.password   给/etc/rsync.password文件600的权限

Then use it with rsync:

rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password    指定密码文件

Useful rsync examples

Excluding files

[root@backup tmp]# rsync -a –-exclude=/etc/hosts /etc/services 172.16.1.31:/tmp/  –-exclude=/etc/hosts 排除/etc/services中的/etc/hosts文件
--bwlimit=RATE        limt socket I/O bandwidth
        传输的时候限速
--delete              让源目录和目标目录一模一样(即:我有什么你就有什么,我没什么你就没有什么)

Exact mirror sync

[root@backup tmp]# rsync -a --delete /tmp/ 172.16.1.31:/tmp/
[email protected]'s password:
--delete 本地有什么,远端就有什么
         本地没有什么,远端就没有什么
         本地有远端没有,就会删除远

Step 4: Install sersync on the source server

sersync is distributed as a binary package with both a configuration file and an executable.

mkdir -p /applition/tools
cd /applition/tools
wgethttps://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz
【有时下载失败,所有要本地留存才行】
[root@web ~]# tar fxzsersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
[root@web ~]# cd /usr/local/
[root@cache local]# mv GNU-Linux-x86 sersync
[root@cache local]# treesersync/
sersync/
├── confxml.xml # 配置文件
└── sersync2    # 二进制文件【启动sersync使用】

0 directories, 2 files

Step 5: Back up and edit the sersync configuration

Before changing the config, create a copy:

[root@cache local]# cp sersync/confxml.xmlsersync/confxml.xml.$(date +%F)
[root@cache local]# ll sersync/confxml.xml
-rwxr-xr-x. 1 root root 2214Oct 26  2011 sersync/confxml.xml
[root@cache local]# llsersync/confxml.xml*
-rwxr-xr-x. 1 root root 2214Oct 26  2011 sersync/confxml.xml
-rwxr-xr-x. 1 root root 2214Jun  5 06:38sersync/confxml.xml.2015-06-05

Adjust the monitored path and remote target

<localpathwatch="/opt/tongbu">       # 定义本地要同步的目录
        <remote ip="127.0.0.1"name="tongbu1"/>    <!--<remoteip="192.168.8.39" name="tongbu"/>--> # 同步到哪台机器上 tongbu模块rsync端模块名字
        <!--<remoteip="192.168.8.40" name="tongbu"/>--> # 同步到哪台机器上 tongbu模块
</localpath>

Configure rsync authentication in sersync

<rsync>
        <commonParamsparams="-artuz"/>
        <auth start="false"users="root" passwordfile="/etc/rsync.pas"/>
        <userDefinedPortstart="false" port="874"/><!-- port=874 -->
        <timeoutstart="false" time="100"/><!-- timeout=100 -->
        <sshstart="false"/>
</rsync>
# ***修改内容为 rsync的密码文件以及 同步所使用的账号类似: rsync -avzP /data/www/ [email protected]::www/ --password-file=/etc/rsync.password

Failed transfer retry log

<failLog path="/usr/local/sersync/logs/rsync_fail_log.sh"timeToExecute="60"/><!--default every 60mins execute once-->
# 当同步失败后,日志记录到/usr/local/sersync/logs/rsync_fail_log.sh文件中,并且每60分钟对失败的log进行重新同步

Full sersync configuration example

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>
    <debug start="false"/>
    <fileSystem xfs="false"/>
    <filter start="false">
        <exclude expression="(.*)\.svn"></exclude>
        <exclude expression="(.*)\.gz"></exclude>
        <exclude expression="^info/*"></exclude>
        <exclude expression="^static/*"></exclude>
    </filter>
    <inotify>
        <delete start="true"/>
        <createFolder start="true"/>
        <createFile start="false"/>
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="false"/>
        <modify start="false"/>
    </inotify>

    <sersync>
        <localpath watch="/etc/openvpn">
            <remote ip="192.168.0.10" name="openvpn"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="start" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
        <crontab start="false" schedule="600"><!--600mins-->
            <crontabfilter start="false">
                <exclude expression="*.php"></exclude>
                <exclude expression="info/*"></exclude>
            </crontabfilter>
        </crontab>
        <plugin start="false" name="command"/>
    </sersync>
    <plugin name="command">
        <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
        <filter start="false">
            <include expression="(.*)\.php"/>
            <include expression="(.*)\.sh"/>
        </filter>
    </plugin>
    <plugin name="socket">
        <localpath watch="/opt/tongbu">
            <deshost ip="192.168.138.20" port="8009"/>
        </localpath>
    </plugin>
    <plugin name="refreshCDN">
        <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
            <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
            <sendurl base="http://pic.xoyo.com/cms"/>
            <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
        </localpath>
    </plugin>
</head>

Step 6: Start sersync in daemon mode

[root@web ~]# /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
配置sersync环境变量
[root@web ~]# echo"PATH=$PATH:/usr/local/sersync/">>/etc/profile
[root@web ~]# source /etc/profile
[root@web ~]# sersync2

A normal startup output looks like this:

set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d      run as a daemon
option: -r      rsync all the local files to the remote servers before the sersync work
option: -o config xml name:  /usr/local/sersync/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost          host port: 8008
daemon start,sersync run behind the console
use rsync password-file :  user is rsync_backup     passwordfile is /etc/rsync.password
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 32 = 12(Thread pool nums) + 20(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
chmod: cannot access `/usr/local/sersync/logs/rsync_fail_log.sh': No such file or directory
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /backup && rsync -artuz -R --delete ./ [email protected]::www --password-file=/etc/rsync.password >/dev/null 2>&1
run the sersync: 
watch path is: /data/www

Step 7: Enable sersync at boot

If the previous checks are successful, add it to startup:

#vi /etc/rc.d/rc.local
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
#设置开机自动运行脚本
# chmod +x /etc/rc.d/rc.local

Step 8: Add a health-check script for sersync

This script checks whether sersync2 is running and starts it if it is not.

cd /root
touch check_sersync.sh
chmod 755 check.sersync.sh
vim check_sersync.sh
#!/bin/sh
sersync="/usr/local/sersync/sersync2"
confxml="/usr/local/sersync/confxml.xml"
status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)
if [ $status -eq 0 ]; then
  $sersync -d -r -o $confxml &
else
  exit 0;
fi
check_sersync.sh

Run it from cron every 5 minutes:

#vi /etc/crontab
*/5 * * * * root /root/check_sersync.sh >/dev/null 2>&1
#每隔5分钟执行一次脚本

Verification notes

A few practical checks matter during testing:

  1. Run check_sersync.sh manually first to confirm that the monitor works correctly.
  2. If you do not want to wait for a reboot, start sersync directly.
  3. Create, modify, or delete files under the watched directory on the source server and verify that the target directory updates accordingly.

Manual start command:

/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml

Common rsync parameters reference

The following options appeared in the original deployment notes and are useful during daily operations:

1 -v, --verbose 详细模式输出
2
3 -q, --quiet 精简输出模式
4
5 -c, --checksum 打开校验开关,强制对文件传输进行校验
6
7 -a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
8
9 -r, --recursive 对子目录以递归模式处理
10
11 -R, --relative 使用相对路径信息
12
13 -b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。
14
15 --backup-dir 将备份文件(如~filename)存放在在目录下。
16
17 -suffix=SUFFIX 定义备份文件前缀
18
19 -u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
20
21 -l, --links 保留软链结
22
23 -L, --copy-links 想对待常规文件一样处理软链结
24
25 --copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结
26
27 --safe-links 忽略指向SRC路径目录树以外的链结
28
29 -H, --hard-links 保留硬链结
30
31 -p, --perms 保持文件权限
32
33 -o, --owner 保持文件属主信息
34
35 -g, --group 保持文件属组信息
36
37 -D, --devices 保持设备文件信息
38
39 -t, --times 保持文件时间信息
40
41 -S, --sparse 对稀疏文件进行特殊处理以节省DST的空间
42
43 -n, --dry-run现实哪些文件将被传输
44
45 -W, --whole-file 拷贝文件,不进行增量检测
46
47 -x, --one-file-system 不要跨越文件系统边界
48
49 -B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节
50
51 -e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步
52
53 --rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息
54
55 -C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件
56
57 --existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件
58
59 --delete 删除那些DST中SRC没有的文件
60
61 --delete-excluded 同样删除接收端那些被该选项指定排除的文件
62
63 --delete-after 传输结束以后再删除
64
65 --ignore-errors 及时出现IO错误也进行删除
66
67 --max-delete=NUM 最多删除NUM个文件
68
69 --partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
70
71 --force 强制删除目录,即使不为空
72
73 --numeric-ids 不将数字的用户和组ID匹配为用户名和组名
74
75 --timeout=TIME IP超时时间,单位为秒
76
77 -I, --ignore-times 不跳过那些有同样的时间和长度的文件
78
79 --size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间
80
81 --modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0
82
83 -T --temp-dir=DIR 在DIR中创建临时文件
84
85 --compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份
86
87 -P 等同于 --partial
88
89 --progress 显示备份过程
90
91 -z, --compress 对备份的文件在传输时进行压缩处理
92
93 --exclude=PATTERN 指定排除不需要传输的文件模式
94
95 --include=PATTERN 指定不排除而需要传输的文件模式
96
97 --exclude-from=FILE 排除FILE中指定模式的文件
98
99 --include-from=FILE 不排除FILE指定模式匹配的文件
100
101 --version 打印版本信息
102
103 --address 绑定到特定的地址
104
105 --config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件
106
107 --port=PORT 指定其他的rsync服务端口
108
109 --blocking-io 对远程shell使用阻塞IO
110
111 -stats 给出某些文件的传输状态
112
113 --progress 在传输时现实传输过程
114
115 --log-format=formAT 指定日志文件格式
116
117 --password-file=FILE 从FILE中得到密码
118
119 --bwlimit=KBPS 限制I/O带宽,KBytes per second
120
121 -h, --help 显示帮助信息
rsync详细参数

For small sync trees, rsync combined with basic event watching is often enough. Once the directory becomes large, the number of files grows rapidly, or near-real-time push becomes important, sersync + rsync daemon is the more efficient design.

Related Posts