Sersync 基于 rsync+inotify 实现数据实时同步

- 1)确保远程数据传输服务部署完成

[root@nfs01 ~]$rsync -avz /data/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password

- 2)检查当前系统是否支持inotify

uname -r

  #是否存在下面三个文件

[root@nfs01 ~]$ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Nov 14 21:28 max_queued_events
-rw-r--r-- 1 root root 0 Nov 14 21:28 max_user_instances
-rw-r--r-- 1 root root 0 Nov 14 21:28 max_user_watches

- 3)在nfs01上安装配置sersync

mkdir /server/tools
cd /server/tools/
#项目为空,下载不能用
wget https://github.com/wsgzao/sersync/blob/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
提供其他两个版本下载
https://www.shnne.com/zb_users/upload/2022/11/sersync2.5.4_64bit_binary_stable_final.tar.gz
老男孩修改版
https://www.shnne.com/zb_users/upload/2022/11/sersync_oldboy_64bit_20160928.tar.gz

- 4)解压配置

tar xf sersync_oldboy_64bit_20160928.tar.gz
mv sersync /application
[root@nfs01 tools]$tree /application
application/
└── sersync
    ├── bin
    │   └── sersync
    ├── conf
    │   ├── confxml.xml
    │   └── confxml.xml.ori
    ├── logs
    │   └── rsync_fail_log.sh
    └── readme.txt
4 directories, 5 files

- 5)配置文件讲解

#完成监控配置
inotifywait -mrq --format '%w%f' -e createFolder,close_write,delete,moveFrom,moveTo /data

#完整命令拼接
rsync -avz /data/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password

- 6)启动sersync

[root@nfs01 bin]$/application/sersync/bin/sersync -h
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
_______________________________________________________
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
#启动进程
[root@nfs01 bin]$/application/sersync/bin/sersync -r -d -o /application/sersync/conf/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: -r  rsync all the local files to the remote servers before the sersync work
option: -d  run as a daemon
option: -o  config xml name:  /application/sersync/conf/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
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data && rsync -artuz -R --delete ./  --timeout=100 rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password >/dev/null 2>&1 
run the sersync: 
watch path is: /data
#结束进程
[root@nfs01 conf]$pkill sersync

- 7)配置systemd启动

[root@nfs01 /data]# tail -1 /etc/rc.local
/application/sersync/bin/sersync -d

参考配置:systemctl start sersync启动方案
https://blog.51cto.com/oldboy/2155931 
 #配置脚本

[root@nfs01 /data]# cat /etc/rc.d/init.d/sersync
#!/bin/bash
# chkconfig: 2345 21 81
# description: rsync service start and stop scripts
# Author: oldboy
# Organization: www.oldboyedu.com
start(){
    /application/sersync/bin/sersync -d -o /application/sersync/conf/confxml.xml &>/dev/null
}
stop(){
    killall sersync 2>/dev/null
}
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        sleep 2
        start
        ;;
    *)
        echo $"Usage:$0 {start|stop|restart}"
        exit 1
esac
[root@nfs01 /data]# chmod +x /etc/rc.d/init.d/sersync
[root@nfs01 /data]# cat /usr/lib/systemd/system/sersync.service
[Unit]
Description=sersyncd service
After=network.target
 
[Service]
Type=forking           
ExecStart=/etc/rc.d/init.d/sersync start   
ExecReload=/etc/rc.d/init.d/sersync restart
ExecStop=/etc/rc.d/init.d/sersync stop     
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

[root@nfs01 /data]# chmod +x /usr/lib/systemd/system/sersync.service
[root@nfs01 /data]# systemctl enable sersync.service
[root@nfs01 /data]# systemctl status sersync.service
[root@nfs01 /data]# systemctl stop sersync
[root@nfs01 /data]# ps -ef|grep sersync|grep -v grep
[root@nfs01 /data]# systemctl start sersync


分享到:
关键词:Linux运维

网友留言(0 条)

发表评论