一、准备

Inotify 是一种强大的、细粒度的、异步的文件系统事件监控机制,Linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而Inotify-tools就是这样的一个第三方软件。

1、服务器



操作系统 CentOS-6.5-x86_64 6.5 Linux内核 2.6.32 master 192.168.0.200 客户端(数据服务器) slave1 192.168.0.110 数据同步服务器(备份服务器1) slave2 192.168.0.111 数据同步服务器(备份服务器2)



2、基础命令安装(所有服务器都需要安装,已经安装可跳过)



yum install -y gcc gcc-c++ make wget



二、数据同步服务端配置(备份服务器)

1、检查是否已安装rsync



rpm -qa | grep rsync



2、安装rsync和xinetd超级守护进程



yum -y install xinetd rsync



3、创建rsync用户及模块目录并更改其用户组(用户运行rsync的用户,不建议使用root)



useradd rsync -s /sbin/nologin  -M
grep rsync /etc/passwd
mkdir -p /data/www
chown -R rsync:rsync /data


4、配置rsync认证文件 /etc/rsync.password(格式 用户:密码)



echo "rsync:rsync" > /etc/rsync.password



4.1、为密码文件提权,增加安全性,文件权限修改成600



chmod 600 /etc/rsync.password



5、为rsync服务提供配置文件,vim  /etc/rsyncd.conf 文件默认不存在,手动创建即可



# 工作中指定用户(需要指定用户)
uid = rsync
gid = rsync

# 相当于黑洞.出错定位
use chroot = no

# 有多少个客户端同时传文件
max connections = 200

# 超时时间
timeout = 600

# 进程号文件
pid file = /var/run/rsyncd.pid

# 锁文件
lock file = /var/run/rsync.lock

# 日志文件
log file = /var/log/rsyncd.log

# 模块配置
# 这个模块对应的是推送目录,模块名称随便起
[rsync]

# 需要同步的目录
path = /data/www/

# 表示出现错误忽略错误
ignore errors

# 表示网络权限可写(本地控制真正可写)
read only = false

# 这里设置IP或让不让同步
list = false

# 白名单,可以访问此模块的主机
hosts allow = 192.168.0.200

# 黑名单,*表示任何主机
hosts deny = *

# 认证此模块的用户名,虚拟用户
auth users = rsync

# 虚拟用户的密码文件
secrets file = /etc/rsync.password



6、配置rsync操作脚本

A、启动脚本



echo "rsync --daemon --config=/etc/rsyncd.conf" > /etc/init.d/rsync-start chmod +x /etc/init.d/rsync-start



B、停止脚本



echo "killall -9 rsync > /dev/null 2>&1; rm -rf /var/run/rsyncd.pid" > /etc/init.d/rsync-stop chmod +x /etc/init.d/rsync-stop



C、重启脚本



echo "/etc/init.d/rsync-stop; /etc/init.d/rsync-start" > /etc/init.d/rsync-restart chmod +x /etc/init.d/rsync-restart



6.1、以后服务器启动后,手动启动rsync脚本即可



/etc/init.d/rsync-start 启动 /etc/init.d/rsync-stop 停止 /etc/init.d/rsync-restart 重启



7、启动xinetd超级守护进程服务



service xinetd start



8、配置防火墙允许873端口通信  vim /etc/sysconfig/iptables



-A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT



9、查看rsync服务是否成功开启



ps -ef | grep rsync



10、检查873端口是否成功监听



netstat -ntlp | grep 873



三、客户端配置

1、检查是否已安装rsync



rpm -qa | grep rsync



1.1、安装rsync服务



yum -y install rsync



2、创建本地监控的目录



mkdir -p /data/www



3、配置rsync认证文件 /etc/rsync.password(这里只要写密码即可,切记)



echo "rsync" > /etc/rsync.password



3.1、为密码文件提权,增加安全性,文件权限修改成600



chmod 600 /etc/rsync.password


4、测试一下,进入 /data/www


5、inotify-tools实现实时同步



mkdir /data/soft
cd /data/soft
wget 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



6、编写rsync脚本,添加一下内容  vim /etc/rsync.sh


#!/bin/bash

# 数据同步监控脚本
# @author	Devil
# @version	0.0.1

# inotify-slave的ip地址(多个ip空格分割)
host="192.168.0.110 192.168.0.111"

# 本地监控的目录
src=/data/www/

# inotify-slave的rsync服务的模块名
dst=rsync

# inotify-slave的rsync服务的虚拟用户
user=rsync

# 本地调用rsync服务的密码文件
rsync_passfile=/etc/rsync.password

# inotify的安装目录
inotify_home=/usr/local/inotify

# 参数校验
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
	echo "Check File and Folder"
	exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do
	cd $src
	for tmp_host in $host
	do
		rsync -aruz -R --delete ./  --timeout=100 $user@$tmp_host::$dst --password-file=${rsync_passfile} > /dev/null 2>&1
	done
done
exit 0


6.1、给rsync.sh执行权限



chmod +x /etc/rsync.sh



6.2、配置操作rsync脚本文件,添加以下内容  vim /etc/init.d/rsync



# 脚本名称
name="rsync"

# 拉取正在运行的脚本进程号
PROCESS=`ps -ef | grep $name | grep -v grep | grep -v PPID | awk '{ print $2}'`

# 输出正在停止脚本的提示
if [ -n "$PROCESS" ]
then
	echo "正在停止"
fi

# 停止脚本
for i in $PROCESS
do
	kill -9 $i > /dev/null 2>&1
done

# 是否停止成功
if [ $? != 0 ]
then
	echo "停止失败"
	exit 0
fi

# 启动脚本
/etc/rsync.sh &

# 等待2秒
sleep 2

# 判断脚本是否启动成功
is_rsync=$(ps -ef | grep "$name" | grep -v grep | wc -l)
if [ $is_rsync != 0 ]
then
	echo "启动成功"
else
	echo "启动失败"
fi



6.3、给rsync服务脚本执行权限



chmod +x /etc/init.d/rsync



7、以后每次只需运行rsync脚本即可,停止启动合并



/etc/init.d/rsync



8、进入监控目录操作文件试试看效果



cd /data/www touch test.txt echo "hello" > test.txt touch world.txt



9、查看备份服务器 192.168.0.100,192.168.0.110



cd /data/www ls 文件都同步过来了