西数超哥博客
运维经验教程分享

Linux环境下通过rsync+inotify实现数据实时同步

本站一直缺少类似方面教程,今天就补充一下。其中rsync是一个远程数据同步工具,使用方法可以参考之前的文章《linux系统下Rsync远程数据同步命令介绍以及使用整理》 ,但数据更新以后数据还需要手工同步,无法实现自动化。这里就借助资料配置一个rsync+innotify实现数据实时同步,解决手工处理烦恼。

环境准备:centos三台
源数据服务器:192.168.1.8
目标服务器:192.168.1.9    192.168.1.10
测试目的:将源服务器的/home/21yunwei 自动同步到目标服务器。
为了测试,关闭selinux以及iptables(iptables也可以放行873端口,不用关闭)
一,两个目标服务器配置
1,安装rsync和xinetd,centos下是通过xinetd管理rsync。

yum install rsync xinetd  -y

编辑vi /etc/xinetd.d/rsync 设置rsync启动,即disable = no 。默认是不启动yes。
修改完以后启动xinetd:/etc/init.d/xinetd start
netstat -tnlp |grep 873 如果看到有xinetd服务,说明启动成功。
2,创建rsyncd.conf配置文件并修改参数。
vi /etc/rsyncd.conf

log file = /var/log/rsyncd.log 
pidfile = /var/run/rsyncd.pid  
lock file = /var/run/rsync.lock  
secrets file = /etc/rsync.pass 
motd file = /etc/rsyncd.Motd 

[home_21yunwei]
path = /home/21yunwei/ 
comment = home_21yunwei
uid = root 
gid = root 
port=873 
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600  
auth users = 21yunwei 
hosts allow = 192.168.1.8
hosts deny = 192.168.1.200

其中参数说明这里列举一下:

log file = /var/log/rsyncd.log #日志文件位置,启动rsync后自动产生这个文件,无需提前创建
pidfile = /var/run/rsyncd.pid  #pid文件的存放位置
lock file = /var/run/rsync.lock  #支持max connections参数的锁文件
secrets file = /etc/rsync.pass  #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
motd file = /etc/rsyncd.Motd  #rsync启动时欢迎信息页面文件位置(文件内容自定义)

[home_21yunwei] #自定义名称
path = /home/21yunwei/ #rsync服务端数据目录路径
comment = home_21yunwei #模块名称与[home_21yunwei]自定义名称相同
uid = root #设置rsync运行权限为root
gid = root #设置rsync运行权限为root
port=873  #默认端口
use chroot = no #默认为true,修改为no,增加对目录文件软连接的备份
read only = no  #设置rsync服务端文件为读写权限
list = no #不显示rsync服务端资源列表
max connections = 200 #最大连接数
timeout = 600  #设置超时时间
auth users = 21yunwei #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = 192.168.1.8  #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.1.200 #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开

3,创建用户认证文件

vi /etc/rsync.pass
21yunwei:123456   #格式,用户名:密码,可以设置多个,每行一个用户名:密码

4,设置文件权限
chmod  600 /etc/rsyncd.conf
chmod  600  /etc/rsync.pass
如果不设置这个权限,那么会报错password file must not be other-accessible。

重启xinetd服务:
/etc/init.d/xinetd restart

二,源数据服务器配置

1,安装rsync和xinetd
基本上目标服务器安装是一样的,安装完将服务设置成启动。启动xinetd服务。
2,创建认证密码文件
vi /etc/passwd.txt  写入:
123456 #密码

设置权限:
chmod 600 /etc/passwd.txt
如果不设置权限会报错:password file must not be other-accessible
3,测试源服务器192.168.1.8到两台目标服务器之间数据同步情况。
测试目的:将/home/21yunwei下的文件同步到两台目标服务器。
我们touch /home/21yunwei{1..10}.php 后执行测试:

rsync -avz --port=873  --delete  /home/21yunwei/   21yunwei@192.168.1.9::home_21yunwei    --password-file=/etc/passwd.txt
rsync -avz --port=873  --delete  /home/21yunwei/   21yunwei@192.168.1.10::home_21yunwei   --password-file=/etc/passwd.txt

测试结果:

[root@kvmserver home]# rsync -avz --port=873  --delete  /home/21yunwei/   21yunwei@192.168.1.10::home_21yunwei   --password-file=/etc/passwd.txt

sending incremental file list
./
1.php
10.php
2.php
3.php
4.php
5.php
6.php
7.php
8.php
9.php

sent 557 bytes  received 206 bytes  1526.00 bytes/sec
total size is 4  speedup is 0.01

查看下10目标服务器目录:

[root@web10 home]# ll 21yunwei/
total 8
-rw-r--r-- 1 root root    0 Sep  1 15:36 10.php
-rw-r--r-- 1 root root    4 Sep  1 15:43 1.php
-rw-r--r-- 1 root root    0 Sep  1 15:36 2.php
-rw-r--r-- 1 root root    0 Sep  1 15:36 3.php
-rw-r--r-- 1 root root    0 Sep  1 15:36 4.php
-rw-r--r-- 1 root root    0 Sep  1 15:36 5.php
-rw-r--r-- 1 root root    0 Sep  1 15:36 6.php
-rw-r--r-- 1 root root    0 Sep  1 15:36 7.php
-rw-r--r-- 1 root root    0 Sep  1 15:36 8.php
-rw-r--r-- 1 root root    0 Sep  1 15:36 9.php

两台服务器测试完毕,效果测试同步数据没问题。

三,源服务器安装Inotify-tools工具,实时触发rsync进行同步。

1、查看服务器内核是否支持inotify
ll /proc/sys/fs/inotify

[root@kvmserver home]# ll /proc/sys/fs/inotify 
total 0
-rw-r--r-- 1 root root 0 Sep  1 17:20 max_queued_events
-rw-r--r-- 1 root root 0 Sep  1 17:20 max_user_instances
-rw-r--r-- 1 root root 0 Sep  1 17:20 max_user_watches

如果出现上述内容,说明支持innotify。
2,安装inotify-tools工具
下载http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz 压缩包并进行解压,编译、安装。

tar zxvf inotify-tools-3.14.tar.gz 
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify
make 
make install

3,设置环境变量并建立软链接。

echo "PATH=/usr/local/inotify/bin:$PATH" >>;/etc/profile.d/inotify.sh
source /etc/profile.d/inotify.sh 
echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf
ln -s /usr/local/inotify/include  /usr/include/inotify

4,修改inotify默认参数。
修改参数:

sysctl -w fs.inotify.max_queued_events="99999999"
sysctl -w fs.inotify.max_user_watches="99999999"
sysctl -w fs.inotify.max_user_instances="65535"

编辑vi /etc/sysctl.conf

fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535

参数说明:
max_queued_events:
inotify队列最大长度,如果值太小,会出现”** Event Queue Overflow **”错误,导致监控文件不准确

max_user_watches:
要同步的文件包含多少目录。防止该值太小导致失败,尤其是小文件目录比较多的情况。
max_user_instances:
每个用户创建inotify实例最大值。

5、创建脚本,实时触发rsync进行同步

#!/bin/sh

srcdir=/home/21yunwei/
dstdir=home_21yunwei
excludedir=/usr/local/inotify/exclude.list
rsyncuser=21yunwei
rsyncpassdir=/etc/passwd.txt
dstip="192.168.1.9  192.168.1.10"

for ip in $dstip
do
rsync -avH --port=873 --progress --delete  --exclude-from=$excludedir  $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir
done

/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $srcdir |  while read file
do
  for ip in $dstip
  do
  rsync -avH --port=873 --progress --delete  --exclude-from=$excludedir  $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir
  echo "  ${file} was rsynced" >> /tmp/rsync.log 2>&1
  done
done

脚本参数说明:

srcdir=/home/21yunwei/ #源服务器同步目录
dstdir=home_21yunwei #目标服务器rsync同步目录模块名称
excludedir=/usr/local/inotify/exclude.list
#不需要同步的目录,如果有多个,每一行写一个目录,使用相对于同步模块的路径;
#例如:不需要同步/home/21yunwei/目录下的a目录和b目录下面的b1目录,exclude.list文件可以这样写
a/
b/b1/

rsyncuser=21yunwei #目标服务器rsync同步用户名
rsyncpassdir=/etc/passwd.txt #目标服务器rsync同步用户的密码在源服务器的存放路径
dstip=”192.168.1.9 192.168.1.10″ #目标服务器ip,多个ip用空格分开
/tmp/rsync.log #脚本运行日志记录

6、设置脚本开机自动执行

vi /etc/rc.d/rc.local  
/bin/bash /usr/local/inotify/rsync.sh & #设置开机自动并在后台运行脚本

重启服务器。
7,测试inotify实时触发rsync同步脚本是否正常运行。
我们在源服务器的/home/21yunwei 创建文件:

[root@kvmserver 21yunwei]# echo "test inotify" >> 21yunwei.php
[root@kvmserver 21yunwei]# 
sending incremental file list
./
21yunwei.php
          13 100%    0.00kB/s    0:00:00 (xfer#1, to-check=8/14)

sent 268 bytes  received 30 bytes  596.00 bytes/sec
total size is 17  speedup is 0.06

sending incremental file list
21yunwei.php
          13 100%    0.00kB/s    0:00:00 (xfer#1, to-check=8/14)

然后进去192.168.1.9 和192.168.1.10目录查看, 查看这两个文件也是存在的。 删除也是一样:

[root@web10 21yunwei]# ll
total 12
-rw-r--r-- 1 root root    0 Sep  1 15:36 10.php
-rw-r--r-- 1 root root    4 Sep  1 15:43 1.php
-rw-r--r-- 1 root root    0 Sep  1 17:49 1.txt
-rw-r--r-- 1 root root   13 Sep  1 17:51 21yunwei.php

再多测试一道,我们从svn更新下内容到/home/21yunwei,看下是否web 9和web10两个节点也发生变化:

[root@kvmserver 21yunwei]# svn  co   svn://59.46.x.x/21yunweinew  /home/21yunwei/  --username=21yunwei  --password=xxxxxx

进入web9和web10,发现新更新的数据也都是在的。
inotify常用参数:

inotify参数
-m 是保持一直监听
-r 是递归查看目录
-q 是打印出事件
-e create,move,delete,modify,attrib 是指 “监听 创建 移动 删除 写入 权限” 事件

至此,inotify+rsync设置完成。

本操作的顺利实现,主要参考了文章http://www.osyunwei.com/archives/7435.html ,特此说明。

 

转载请注明:西数超哥博客www.ysidc.top» Linux环境下通过rsync+inotify实现数据实时同步

https://www.ysidc.top 西数超哥博客,数据库,西数超哥,虚拟主机,域名注册,域名,云服务器,云主机,云建站,ysidc.top

赞(0)
声明:本站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,若涉及侵权请及时告知,将会在第一时间删除。本站原创内容未经允许不得转载:西数超哥博客 » Linux环境下通过rsync+inotify实现数据实时同步