rsync+inotify多台服务器之间文件同步

修改rsync配置文件
vi /etc/rsyncd.conf
-----------------------------------
uid = nobody
gid = nobody
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
log file = /var/log/rsync
exclude = lost+found/
transfer logging = yes
timeout = 900
# ignore nonreadable = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

[ftp]
path = /root/data
comment = ftp export area
ignore errors = yes
read only = no
write only = no
hosts allow = *
# hosts deny = *
list = yes
uid = root
gid = root
auth users = root
secrets file = /etc/rsyncd.secrets
----------------------------------

vi /etc/rsyncd.secrets
---------------------------------
root:12345678
---------------------------------

vi /etc/rsyncd.passwd
---------------------------------
12345678
---------------------------------

chmod 0600 /etc/rsyncd.secrets
chmod 0600 /etc/rsyncd.passwd
启动同步服务
systemctl start rsyncd
systemctl restart rsyncd

在需要同步文件的客户机上安装inotify服务
yum install -y epel-release && yum update
yum --enablerepo=epel install inotify-tools

编写同步监控脚本
vi /usr/sbin/rsync/trigger.sh#两台服务器同步
----------------------------------

#!/bin/bash

DESIP=192.168.2.122
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /root/data "
RSYNC_CMD="rsync -azH --delete --password-file=/etc/rsyncd.passwd /root/data root@$DESIP::ftp"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if [ $(pgrep rsync | wc -l) -gt 0 ] ; then
        $RSYNC_CMD
    fi
done

----------------------------------

vi /root/rsync/multitrigger.sh #三台服务器同步
--------------------------------------

#!/bin/bash

des_ip=("192.168.2.121 192.168.2.122")
src_dir=/root/data/
des_model=ftp

INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /root/data "
#RSYNC_CMD="rsync -azH --delete --password-file=/etc/rsyncd.passwd /root/data/ root@$des_ip::ftp"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if [ $(pgrep rsync | wc -l) -gt 0 ] ; then
    for i in ${des_ip}
        do
        rsync -azH --delete --password-file=/etc/rsyncd.passwd $src_dir root@$i::$des_model
        done
    fi
done

--------------------------------------

chmod +x /root/rsync/multitrigger.sh
启动inofify监听
/root/rsync/multitrigger.sh
查看文件同步情况
tail -fn 20 /var/log/rsync.log

 

经过测试发现,一对多同步没有问题,多对多同步会报错“No such file or directory”

 

上一篇:实时同步sersync实战


下一篇:在没有轮询的情况下检测PHP中的文件更改