rsync+inotify高效实时同步

多服务器高效数据实时同步方案

实验环境

docker模拟两台centos7服务器

名称ip类型
centos_a172.17.0.2数据服务器
centos_b172.17.0.3备份服务器

实现将a的数据实时同步至b

安装配置软件

备份服务器(b)

1. 安装rsync

安装

#安装
yum -y install rsync
#启动
systemctl start rsyncd && systemctl enable rsyncd

配置rsync文件

vim /etc/rsyncd.conf 
 uid = root
 gid = root
 use chroot = no
 max connections = 4
# pid file = /var/run/rsyncd.pid
 lock file = /var/run/syncd.lock
 log file=/var/log/rsyncd.log
 exclude = lost+found/
 transfer logging = yes
 timeout = 900
 ignore nonreadable = yes
 dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

[test]
path=/work/share
comment=zsg test comment
ignore errors
read only=no
write only=no
list=no
auth users=utest
secrets file=/etc/rsyncd-test.pwd

字段解释

#工作中指定用户(可以不指定为0)
uid = 0
gid = 0
#相当于黑洞.出错定位
use chroot = no
##有多少个客户端同时传文件
max connections =200
##超时时间
timeout = 300
##进程号文件
pid ifle = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
##日志文件
log file = /var/log/rsyncd.log
##模块开始(这个模块就是待会儿对端写脚本的里面一个参数的名称)
[msone]
##需要同步的目录(准确的说是同步过来后放到哪里的目录路径)
path = /data/www
##表示出现错误忽略错误
ignore errors
##表示网络权限可写(本地控制真正可写)(亲测这里写false报错)
read only = no
##这里设置IP或让不让同步
list = false
#允许的ip或者ip段
hosts allow = 192.168.1.238
##拒绝,以下表示都不拒绝
hosts deny = 0.0.0.0/32
##认证用户
auth users = rsynclsl
##用户名和密码存放文件
secrets file = /etc/rsync.secrets

创建密码文件并重启

echo "utest:123456">/etc/rsyncd-test.pwd

chmod 600 /etc/rsyncd-test.pwd

systemctl restart rsyncd

数据服务器(a)

1. 安装rsync

安装

yum -y install rsync

配置密码

echo "123456">/etc/rsyncd-test.pwd

仅作为数据服务器,完成安装后无需启动

2.安装Inotify

下载

wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
#如果下载过慢可以使用下面
wget https://gitee.com/zsgwakk/mirror/raw/master/inotify-tools-3.14.tar.gz

安装

tar xzf inotify-tools-3.14.tar.gz ;cd inotify-tools-3.14

./configure --prefix=/usr/local/inotify 

make

make install

编写脚本 inotify-test

当文件发送变化时触发同步

vim inotify-test.sh
#!/bin/bash
src=/work/share/
des=test
rsync_passwd_file=/etc/rsyncd-test.pwd
ip1=172.17.0.3
user=utest
inotify_exclude="--fromfile '$PWD/notify-fromfile'"#排除监控文件
rsync_exclude="--exclude-from=$PWD/rsync-exclude"#排除同步文件
cd ${src}
/usr/local/inotify/bin/inotifywait -mrq --format  '%Xe %w%f' -e modify,create,delete,attrib,close_write,move  $inotify_exclude ./ | while read file

do
        INO_EVENT=$(echo $file | awk '{print $1}')
        INO_FILE=$(echo $file | awk '{print $2}')
        if [[ $INO_EVENT =~ 'CREATE' ]] || [[ $INO_EVENT =~ 'MODIFY' ]] || [[ $INO_EVENT =~ 'CLOSE_WRITE' ]] || [[ $INO_EVENT =~ 'MOVED_TO' ]]
        then
                echo '--CREATE or MODIFY or CLOSE_WRITE or MOVED_TO---'
                echo $file
                if [ -f $INO_FILE ]
                then
                   #文件
                   sudo /usr/bin/rsync -avzc $rsync_exclude  --password-file=${rsync_passwd_file} $INO_FILE ${user}@${ip1}::${des}/$INO_FILE
                elif [ -d $INO_FILE ]
                then
                   #目录
                   sudo /usr/bin/rsync -avzc $rsync_exclude  --password-file=${rsync_passwd_file} $INO_FILE/ ${user}@${ip1}::${des}/$INO_FILE
                else
                   echo "file not found"
                fi

        fi

        if [[ $INO_EVENT =~ 'DELETE' ]] || [[ $INO_EVENT =~ 'MOVED_FROM' ]]
        then
                echo '--DELETE or MOVED_FROM--'
                echo $file
                sudo /usr/bin/rsync -avz $rsync_exclude  --password-file=${rsync_passwd_file}  $(dirname ${INO_FILE})/ ${user}@${ip1}::${des}/$(dirname ${INO_FILE})
        fi
#不同步属性变化
#        if [[ $INO_EVENT =~ 'ATTRIB' ]]
#        then
#                if [ ! -d "$INO_FILE" ]
#                then
#                   echo 'ATTRIB'
#                    sudo /usr/bin/rsync -avzc --exclude-from=rsync-exclude  --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip1}::${des}        
#                fi
#        fi
done

排除监控目录

vim notify-fromfile

#过滤掉log和temp目录
@/work/share/runtime/log
@/work/share/runtime/temp

排除同步目录

data/conf/*
data/cliruntime/*
data/runtime/*

编写脚本inotify-test-all.sh

手动同步全部文件

#!/bin/bash
sudo /usr/bin/rsync -avzc --password-file=/etc/rsyncd-test.pwd --exclude=runtime/log --exclude=runtime/temp  /work/share utest@172.17.0.3::test

执行脚本

#给俩脚本执行权限
#手动同步全部文件到a
sh inotify-test-all.sh
#启动inotify监控同步
nohup sh inotify-test.sh &
#可以设置开机启动,自行google

开始使用

在a中修改文件内容

echo "hello world"> /work/share/test.txt

在b中对应目录即可查看

参考文章

https://blog.csdn.net/wintershang/article/details/89354539
https://blog.csdn.net/in_christ/article/details/80568384

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值