rsync+inotify触发式远程同步

 为了同步网站的网页文件等(集群),网上查找资料看到rsync+inotify触发式远程同步

然后尝试实验下,效果不错,点滴记忆,方便以后查询使用!
安装软件可以利用rpm包或者编码包,都是可行的!建议使用编码包!



服务器端操作
因为CentOS系统安装的时候,自带安装了rsync软件,查询如下: 
rpm -ql rsync
当然如果没有查询到,请按照如下命令操作:
yum install rsync* -y

然后我们编辑rsync的配置文件(这个文件不存在,可以自己修改文件名 )
为了方便直接使用默认的配置文件名 rsyncd.conf
cat >/etc/rsyncd.conf <<EOF
uid = nobody
gid = nobody
user chroot = no                                                 权限方面的问题
max connections =200                                         最大连接数
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[yxfshellhome]                                                  对外公布的目录
path = /home/shell                                           同步目录
ignore errors                                                    忽略I/O错误
read only = no                                                  可读写
list = no                                                            列表
hosts allow = 192.168.0.0/255.255.255.0             网段
auth users = yxf                                                 用户
secrets file = /etc/rsyncd.password                     格式:yxf:123456 (用户名:密码 ) 本机需要拥有这个用户
EOF

touch /etc/rsyncd.password
echo “yxf:123456” >/etc/rsyncd.password                
多个用户请按照如下方式排列
yxf:123456
yxf1:123456
....

为了管理rsync方便 我们可以利用xinetd来管理它
yum install xinetd* -y

[root@mail shell]# vim /etc/xinetd.d/rsync

# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
        disable = no                                        这里原先是yes 请更改为no
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}
重启xinetd 
/etc/init.d/xinetd restart
chkconfig xinetd on
那么rsync也随之启动起来了






客户端(从服务器端)
同上 检查下rsync是否安装了 没有的话 请安装上面的命令安装下
即可
同步的命令
rsync -avzP [email protected]::yxfshellhome   /home/shell        (yxfshellhome 为主服务器对外公开的共享目录          /home/shell 为从服务器上接受主服务器共享文档存放的目录)
但是这样 会提示输入密码         (主服务器上用来共享的用户的密码)
为了方便加入计划任务 ,添加一条命令 
rsync -avzP  --delete 
--password-file=/etc/rsyncd.password [email protected]::yxfshellhome   /home/shell  
/etc/rsyncd.password 只要存放共享用户的密码即可 不存在的文件 请单独建立
echo "123456" >/etc/rsyncd.password
--delete:意思是让主从服务器上共享的目录内容完全一致 从服务器上有多余的文件 在执行命令时 将全部删除
好的如此 这个rsync文件共享服务器完成
 
 
 
 
但是在实际的生产环境中,不可能让计划任务开启rsync同步功能 那样不能够保证文件的统一性
这在我们需要利用inotify来检测双方文件的差异性而同步文件
 
主服务器上
安装inotify很是简单
tar zxvf inotify-tools-3.13.tar.gz
cd inotify-tools-3.13
./configure --prefix=/usr/local/inotify
make && make install 
注意 需要编译的环境 yum install gcc gcc-c++ -y
如此 inotify安装完成
cat > rsync_tongbu.sh <<EOF
#!/bin/sh
srcdir="/home/shell/"                                                                 表示主服务器上共享目录路径
ip="192.168.0.67 192.168.0.68"                                                表示需要同步的从服务器IP 多个IP 请中间留一个空格
dstdir="/home/shell/"                                                                 表示需要同步的从服务器上接受共享文件的目录路径
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y-%H:%M' --format '%T %w%f' -e modify,delete,create,attrib ${srcdir} \
| while read file
do
        for i in $ip
                do
                rsync -aqztH --delete --progress ${srcdir} root@${i}:${dstdir}
                done
done
EOF 

建立ssh密钥 需要传送到个个从服务器上
ssh-kegen -t rsa
scp /root/.sss/id_rsa.pub [email protected]:/root/.ssh/authorized_keys
scp /root/.sss/id_rsa.pub [email protected]:/root/.ssh/authorized_keys
最后 执行rsync_tongbu.sh
 
检查同步效果 
主服务器上
cd /home/shell
touch {1,2,3,4,5,6,7,8,9,0}{a,b,c,d,e,o,f,g,h,t}.txt
 
然后到从服务器上的/home/shell 下查看 
会看到你想要的内容的
好的 至此 rsync+inotify配置触发式远程同步配置完成
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章