CentOS 8.2 환경에서 Rsync 기반 실시간 파일 동기화 구축: sersync와 inotify-tools 비교

rsync + sersync 조합으로 실시간 동기화 환경 구축

역할호스트명IP 주소
백업 서버 (rsync 데몬)backup-svr.example.com192.168.6.200
데이터 소스 서버 (sersync)source-svr.example.com192.168.6.65

백업 서버 (rsync 데몬) 설정

호스트명을 설정하고 rsync 설치 여부를 확인합니다.

[root@backup-svr ~]# hostnamectl set-hostname backup-svr.example.com

[root@backup-svr ~]# rpm -qa | grep rsync
rsync-3.1.3-7.el8.x86_64

필요한 패키지를 설치하고 rsync 데몬을 구동합니다.

[root@backup-svr ~]# dnf -y install xinetd rsync

[root@backup-svr ~]# rsync --daemon
[root@backup-svr ~]# ss -antp | grep 873
tcp   LISTEN  0  128  0.0.0.0:873  0.0.0.0:*  users:(("rsync",pid=51170,fd=3))
tcp   LISTEN  0  128  [::]:873     [::]:*     users:(("rsync",pid=51170,fd=5))

/etc/rsyncd.conf 설정 파일을 작성합니다.

[root@backup-svr ~]# cat > /etc/rsyncd.conf <<'EOF'
# Rsync 데몬 설정
uid = root
gid = root
use chroot = yes
max connections = 100
timeout = 600
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 192.168.6.0/24
hosts deny = 0.0.0.0/32
auth users = syncuser
secrets file = /etc/rsync.secret

[webdata]
comment = Website Data
path = /data/www
EOF

서비스를 활성화하고 동기화 대상 디렉터리와 인증 파일을 생성합니다.

[root@backup-svr ~]# systemctl enable --now xinetd

[root@backup-svr ~]# rsync --daemon --config=/etc/rsyncd.conf

[root@backup-svr ~]# mkdir -p /data/www

[root@backup-svr ~]# echo 'syncuser:P@ssw0rd!2024' > /etc/rsync.secret
[root@backup-svr ~]# chmod 600 /etc/rsync.secret

데이터 소스 서버에서 rsync 동기화 테스트

소스 서버에서 인증 파일을 준비하고 동기화를 테스트합니다.

[root@source-svr ~]# hostnamectl set-hostname source-svr.example.com

[root@source-svr ~]# echo 'P@ssw0rd!2024' > /etc/rsync.secret
[root@source-svr ~]# chmod 600 /etc/rsync.secret

[root@source-svr ~]# mkdir -p /data/www
[root@source-svr ~]# cp /etc/passwd /data/www/

[root@source-svr ~]# md5sum /data/www/passwd
b42f457347877832fc6da97e6cc3481e  /data/www/passwd

[root@source-svr ~]# rsync -avzP /data/www/ syncuser@192.168.6.200::webdata --password-file=/etc/rsync.secret
sending incremental file list
./
passwd

sent 2,953 bytes  received 38 bytes  5,982.00 bytes/sec
total size is 2,923  speedup is 0.98

백업 서버에서 파일이 정상적으로 전송되었는지 확인합니다.

[root@backup-svr ~]# cd /data/www
[root@backup-svr www]# ls
passwd
[root@backup-svr www]# md5sum passwd
b42f457347877832fc6da97e6cc3481e  passwd

데이터 소스 서버에 sersync 배포

sersync 패키지를 다운로드하고 설치합니다.

[root@source-svr ~]# cd /tmp
[root@source-svr tmp]# wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz

[root@source-svr tmp]# tar xzf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@source-svr tmp]# mv GNU-Linux-x86 /opt/sersync

[root@source-svr ~]# tree /opt/sersync
/opt/sersync/
├── confxml.xml      # 설정 파일
└── sersync2         # 실행 바이너리

0 directories, 2 files

설정 파일을 백업하고 동기화 설정을 수정합니다.

[root@source-svr ~]# cp /opt/sersync/confxml.xml /opt/sersync/confxml.xml.orig

[root@source-svr ~]# cat > /opt/sersync/confxml.xml <<'EOF'
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>
    <debug start="false"/>
    <fileSystem xml:attr="local"/>
    <sersync>
        <localpath watch="/data/www">
            <remote ip="192.168.6.200" name="webdata"/>
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="syncuser" passwordfile="/etc/rsync.secret"/>
            <userDefinedPort start="false" port="874"/>
            <timeout start="false" time="100"/>
            <ssh start="false"/>
        </rsync>
        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/>
        <crond start="false" schedule="600">
            <shall start="false"/>
        </crond>
    </sersync>
</head>
EOF

sersync 데몬을 실행하여 실시간 동기화를 시작합니다.

[root@source-svr ~]# /opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml
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:  /opt/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	syncuser
passwordfile is 	/etc/rsync.secret
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: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data/www && rsync -artuz -R --delete ./ syncuser@192.168.6.200::webdata --password-file=/etc/rsync.secret >/dev/null 2>&1 
run the sersync: 
watch path is: /data/www

rsync + inotify-tools 조합으로 실시간 동기화 환경 구축

이 방식은 파일 시스템 이벤트를 감지하여 변경 발생 시 rsync 명령을 트리거하는 방식입니다.

역할호스트명IP 주소
백업 서버 (rsync 데몬)nginx01.example.com192.168.6.198
클라이언트 (inotify 감시)nginx02.example.com192.168.6.199

백업 서버 설정

기본 도구를 설치합니다.

[root@nginx01 ~]# dnf install -y lrzsz gcc wget gcc-c++ vim rsync

rsync 데몬 설정 파일을 작성합니다.

[root@nginx01 ~]# cat > /etc/rsyncd.conf <<'EOF'
uid = 0
gid = 0
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

[reports]
path = /data/www
ignore errors
read only = no
list = false
hosts allow = 192.168.6.199
auth users = reportuser
secrets file = /etc/rsync.secrets
EOF

동기화 디렉터리와 인증 파일을 생성합니다.

[root@nginx01 ~]# mkdir -p /data/www
[root@nginx01 ~]# chmod 765 /data/www

[root@nginx01 ~]# echo 'reportuser:SecurePass456' > /etc/rsync.secrets
[root@nginx01 ~]# chmod 600 /etc/rsync.secrets

rsync 서비스를 시작합니다.

[root@nginx01 ~]# systemctl start rsyncd
[root@nginx01 ~]# systemctl enable rsyncd

[root@nginx01 ~]# ss -anplt | grep 873
tcp  LISTEN  0  128  0.0.0.0:873  0.0.0.0:*  users:(("rsync",pid=13127,fd=3))
tcp  LISTEN  0  128  [::]:873     [::]:*     users:(("rsync",pid=13127,fd=5))

클라이언트 (inotify) 설정

inotify 지원 여부를 확인합니다.

[root@nginx02 ~]# ls /proc/sys/fs/inotify/
max_queued_events  max_user_instances  max_user_watches

필요한 패키지를 설치하고 inotify-tools를 빌드합니다.

[root@nginx02 ~]# dnf install -y rsync gcc make

[root@nginx02 ~]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@nginx02 ~]# tar xzf inotify-tools-3.14.tar.gz
[root@nginx02 ~]# cd inotify-tools-3.14
[root@nginx02 inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[root@nginx02 inotify-tools-3.14]# make && make install

감시 및 동기화 스크립트를 작성합니다.

[root@nginx02 ~]# cat > /opt/sync-monitor.sh <<'EOF'
#!/bin/bash
#chkconfig: 345 88 14

DST_HOST="192.168.6.198"
SRC_DIR="/data/www/"
MOD_NAME="reports"
PASS_FILE="/etc/rsync.passwd"
SYNC_USER="reportuser"
INOTIFY_BIN="/usr/local/inotify/bin/inotifywait"

${INOTIFY_BIN} -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' \
    -e modify,delete,create,attrib,move ${SRC_DIR} \
| while read event
do
    rsync -avzP --delete --timeout=100 --password-file=${PASS_FILE} \
        ${SRC_DIR} ${SYNC_USER}@${DST_HOST}::${MOD_NAME}
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] ${event} synced" >> /var/log/sync-monitor.log 2>&1
done
EOF

[root@nginx02 ~]# chmod 755 /opt/sync-monitor.sh

클라이언트 측 인증 파일을 생성합니다.

[root@nginx02 ~]# echo 'SecurePass456' > /etc/rsync.passwd
[root@nginx02 ~]# chmod 600 /etc/rsync.passwd

inotify 관련 커널 파라미터를 조정하여 대규모 파일 감시를 지원합니다.

[root@nginx02 ~]# sysctl -w fs.inotify.max_queued_events=200000000
[root@nginx02 ~]# sysctl -w fs.inotify.max_user_watches=2000000000
[root@nginx02 ~]# sysctl -w fs.inotify.max_user_instances=65535

모니터링 스크립트를 백그라운드에서 실행합니다.

[root@nginx02 ~]# nohup /opt/sync-monitor.sh &> /dev/null &
[1] 16011

rsync + inotify-tools vs rsync + sersync 아키텍처 비교

구분rsync + inotify-toolsrsync + sersync
변경 감지 수준 디렉터리 단위로 변경 이벤트 감지. 어떤 파일이 변경되었는지 특정 불가 개별 파일/디렉터리 단위로 정확한 변경 대상 식별 가능
동기화 효율 전체 디렉터리를 매번 순회하며 대상 파일 탐색. 데이터량이 클수록 성능 저하 심각 변경된 파일만 대상으로 동기화. 대규모 환경에서도 우수한 성능
설정 복잡도 스크립트 직접 작성 필요, 설정이 복잡할 수 있음 XML 기반 설정, 데몬 형태로 간편하게 운영
적합 환경 소규모 파일 동기화, 간헐적 변경이 적은 환경 대규모 실시간 동기화, 빈번한 파일 변경이 발생하는 환경

태그: rsync sersync inotify-tools CentOS 실시간동기화

6월 27일 22:00에 게시됨