Linux下Rsync+Inotify-tools实现数据实时同步

rsync+inotify同步逻辑图

这里写图片描述

节点说明

主机名 主机IP地址
inotify-master 10.1.1.1
inotify-slave 10.1.1.2

inotify-slave部署

1 安装rsync

yum -y install rsync

2 编辑/etc/rsyncd.conf

#设置服务器信息提示文件名称,在该文件中编写提示信息
#
motd file = /etc/rsyncd.motd
#开启rsync数据传输日志功能
transfer logging = yes
#设置日志文件名称,可以通过log format参数设置日志格式
log file = /var/log/rsyncd.log
#设置rsync进程号
pid file = /var/run/rsyncd.pid
#设置锁文件名称
lock file = /var/run/rsync.lock
设置服务器监听的端口号,默认是873
port = 873
#设置服务器所监听网卡接口的IP地址,
address=10.1.1.2
#设置进行数据传输时使用的账号名称或ID,默认使用nobody
uid = nobody
#设置进行数据传输时所使用的组名或GID,默认使用nobody
gid = nobody

use chroot = no
#是否允许客户端上传数据,这里设置为只读
read only = no
#设置并发连接数,0代表无限制。超出并发数后,如果依然有客户端连接请求,则将会收到稍后重试的提示消息
max connections = 10
#模块,rsync通过模块定义同步的目录,模块以【name】的形式定义,这与samba定义共享目录是一样的效果。在rsync中也可以定义多个模块

#同步目录的真实路径通过path指定
path=/var/www/html/
#忽略一些io错误
ignore errors

#exclude 可以指定例外的目录,即将web1目录下的某个目录设置为不同步数据。
#exclude = /test

#设置允许连接服务器的账户,账户可以是系统中不存在的用户
auth users=web1user

#设置密码验证文件名称,注意该文件的权限要求为只读,建议权限600,仅在设置auth users参数后有效
secrets file = /etc/rsyncd.secrets

#设置允许那些主机可以同步数据,可以是单个IP,也可以是网段,多个IP与网段之间使用空格分隔
hosts allow = 10.1.1.1/255.255.255.0

#设置拒绝所有(除hosts allow定义的主机外)
hosts deny=*

#客户端请求显示模块列表,本模块名称是否显示,默认为true
list = false

3 配置写入目录权限

chmod -R 777 /var/www/html

4 创建账户

echo "web1user:gcks@pass">/etc/rsyncd.secrets
chmod 600 /etc/rsyncd.secrets

5 设置欢迎词

echo "welcome to access ">/etc/rsyncd.motd

6 以服务形式启动rsync

rsync --daemon

7 设置开机启动

echo "/usr/bin/rsync --daemon">>/etc/rc.local

8 开放873端口

iptables -I INPUT -p tcp --dport 873 -j ACCEPT
service iptables save

inotify-master部署

1 安装rsync

yum -y install rsync

2 安装编译工具

yum -y install make gcc gcc-c++  

3 安装inotify

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.tar.gz
./configure --prefix=/usr/local/inotify
make && make install

4 添加密码文件(#注意:这里只要写密码即可,切记。)

echo "gcks@pass">/etc/rsyncd.secrets
chmod 600 /etc/rsyncd.secrets

5 编写监控脚本inotify_rsync.sh

#!/bin/bash
host1=10.1.1.2

# 本地目录
src=/storage/radius
dst1=web1
user1=web1user
rsync_passfile=/etc/rsync.password
inotify_home=/usr/local/inotify


${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M'  --format '%T %w%f%e' -e close_write,modify,delete,create,attrib $src |while read lines
do
    rsync -vzrtopg --delete --progress --password-file=${rsync_passfile} $src $user1@$host1::$dst1

    echo "${files} was rsynced">>/tmp/rsync.log 2>&1
done

6 执行监控脚本inotify_rsync.sh

chmod 777 inotify_rsync.sh
./inotify_rsync.sh > /tmp/rsync.log 2 >&1 &
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章