rsync+inotify实现文件实时同步
服务端配置
<1>:检查安装包,查看是否已经安装
rpm -aq|grep rsync
<2>:服务端 编辑配置文件rsyncd.conf并授权(默认这个文件是不存在的)
我把配置文件建在/etc下
#工作中指定用户(需要指定用户)
uid =root#系统用户
gid =root#系统用户
use chroot = no#相当于黑洞.出错定位
max connections = 200#有多少个客户端同时传文件
timeout = 300#超时时间
pid file = /var/run/rsyncd.pid#进程号文件
lock file = /var/run/rsync.lock#日志文件
log file = /var/log/rsyncd.log#日志文件
[web]#模块名称随便起
path = /web/#需要同步的目录
ignore errors#表示出现错误忽略错误
read only = false#表示网络权限可写(本地控制真正可写)
list = false#这里设置IP或让不让同步
hosts allow = 10.0.0.0/24#指定允许的网段(0代表随意数字)
hosts deny = 0.0.0.0/32#拒绝链接的地址,一下表示没有拒绝的链接。
auth users =shangyun#虚拟用户
secrets file = /etc/rsync.password#虚拟用户的密码文件
<3>:创建用户并授权
useradd -s /sbin/nologin -M rsync
chown -R rsync.rsync /web/
<4>:创建密码文件并设置权限
echo "shangyun:admin123" >/etc/rsync.password
chmod 600 /etc/rsync.password
注意:这里的shangyun是虚拟账号 admin123是密码
<5>:启动服务
/usr/bin/rsync --daemon
<6>:查看是否成功运行
查看日志cat /var/log/rsyncd.log
查看netstat -lntup|grep rsync
<7>:设置开机启动
echo "/usr/bin/rsync --daemon" >>/etc/rc.local
客户端配置
<1>:检查安装包,查看是否已经安装
rpm -aq|grep rsync
<2>:创建密码文件并授权
echo "admin123" >/etc/rsync.password 注意这里只要密码不需要账号,和服务端不一样
chmod 600 /etc/rsync.password
<3>:测试文件推/拉
拉取数据:rsync -avz shangyun@10.26.119.174::web /web --password-file=/etc/rsync.password
推送数据:rsync -avz /web/ shangyun@10.26.119.174::web --password-file=/etc/rsync.password
rsync正常之后才能安装inotify
下载地址:注意inotify安装在rsync的客户端,而不是服务端
<1>:
wgethttp://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
解压tar -zxvfinotify-tools-3.14.tar.gz
cdinotify-tools-3.14
指定安装目录:./configure --prefix=/usr/local/inotify
编译安装make && make install
<2>:shell编写
#!/bin/bash host01=10.26.119.174 host02=10.29.255.200 src=/web/ dst=web user=shangyun rsync_passfile=/etc/rsync.password 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 readfile do cd $src && rsync -aruz --delete -R ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1 \ && rsync -aruz --delete -R ./ --timeout=100 $user@$host02::$dst --password-file=${rsync_passfile} done exit 0
#!/bin/bash #服务端ip host01=172.16.40.127 src=/web/#本地监控目录 dst=web#模块名 user=greg rsync_passfile=/etc/rsync.password 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 && rsync -aruz -R --delete ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1 done exit 0
<3>:脚本测试
sh -xinotify.sh
<4>:脚本开机运行
echo "sh/usr/local/inotify/bin/inotify.sh" >>/etc/rc.local