一、概念讲解
1、实现实时同步
1)要利用监控服务(inotify),监控同步数据服务器目录中信息的变化
2)发现目录中数据产生变化,就利用rsync服务推送到备份服务器上
2、Inotify简介
Inotify是一种强大的,细粒度的。异步的文件系统事件监控机制,linux内核从2.6.13起,加入了 Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而 inotify-tools 正是实施这样监控的软件。国人周洋在金山公司也开发了类似的实时同步软件sersync。
Inotify实际是一种事件驱动机制,它为应用程序监控文件系统事件提供了实时响应事件的机制,而无须通过诸如cron等的轮询机制来获取事件。cron等机制不仅无法做到实时性,而且消耗大量系统资源。相比之下,inotify基于事件驱动,可以做到对事件处理的实时响应,也没有轮询造成的系统资源消耗,是非常自然的事件通知接口,也与自然世界事件机制相符合。
3、使用方式
inotify:对同步数据目录信息的监控
rsync:完成对数据信息的实时同步
4、部署inotify软件的前提
需要2.6.13以后内核版本才能支持inotify软件
[root@localhost ~]# ll /proc/sys/fs/inotify/ total 0 -rw-r--r-- 1 root root 0 Oct 17 10:12 max_queued_events -rw-r--r-- 1 root root 0 Oct 17 10:12 max_user_instances -rw-r--r-- 1 root root 0 Oct 17 10:12 max_user_watches
5、实验资源准备
192.168.112.138 server 192.168.112.139 backup
二、部署backup端
1、关闭防火墙
systemctl stop firewalld systemctl disable firewalld #关闭selinux sed -i 's/enforcing/disabled/' /etc/selinux/config #永久 setenforce 0 #临时
2、安装rsync服务
yum intsall rsync -y
3、修改配置文件
vim /etc/rsyncd.conf uid = root gid = root use chroot = no #如果你上面uid不是root用户启动,你这里就要注意是否需要切换到root用户 port = 873 max connections = 4 #pid file = /var/run/rsyncd.pid #这个注释有人说去掉后启动会报错,但我去掉也没事,看你自己情况 exclude = lost+found/ lock file = /var/run/rsyncd.lock log file = /var/log/rsyncd.log #输出日志 log format = %t %a %m %f %b #日志格式 transfer logging = yes timeout = 900 ignore nonreadable = yes dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 #传输这几种格式时不压缩 hosts allow = 192.168.112.138 #允许传输数据的ip,x.x.x.x/16生产建议精确到ip [master_web] #[ ]里面自定义一个名字 path = /opt/ #这个路径代表你本地需要同步的路径,表示本地/opt/下的数据可以进行传输 comment = master_web #和[ ]中保持一致 ignore errors read only = no #是否允许client上传文件到server 设置成yes则client的数据无法推送到master上 write only = no #是否允许client从server上拉取数据 设置成yes则client无法从server上拉去数据 list = no auth users = rsync #这个用户时连接时的用户,不是系统用户,不用创建 secrets file = /etc/rsyncd.passwd #保存密码和用户名文件,需要自己生成,这个路径可以自定义
4、创建密码文件
vim /etc/rsyncd.passwd rsync:123456 #用户和路径是上面设置的,密码自定义,
5、设置权限
chmod 600 /etc/rsyncd.passwd #设置权限,不设置会报错
6、启动服务
systemctl restart rsyncd systemctl enable rsyncd
7、查看服务
[root@backup ~]# netstat -nutlp|grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 12809/rsync tcp6 0 0 :::873 :::* LISTEN 12809/rsync
三、部署server端
1、安装rsync服务
yum intsall rsync -y
2、创建一个文件存放server端的密码
vim /root/rsyncd.passwd 123456 #只写上面设置好的密码,不写用户名 chmod 600 /root/rsyncd.passwd
3、现在我们把server机器定义的/opt/下面的数据同步到backup的/data目录夹,/data自己创建
注意该操作是在backup上操作
rsync -avzp --delete rsync@192.168.112.139::master_web /data --password-file=/root/rsyncd.passwd
–delete 表示在同步时,server端/opt/目录中不论是删除,添加,修改,都会同步到backup,如果不加该参数,则只会同步server端新添加和修改的数据,server端删除掉的数据,client端不会删除,依然存在,该选项按个人需求选择.
4、实现自动检测server文件发生变化,自动同步到backup端上去
1)安装inotify yum install inotify-tools -y 2)调整inotify内核参数 当要监控的目录、文件数量较多或者变化较频繁时,建议加大这三个参数的值。可以直接修改/etc/sysctl.conf的配置文件,将管理队列、实例数、监控数进行设置。 vim /etc/sysctl.conf #For more information, see sysctl.conf(5) and sysctl.d(5). //添加 fs.inotify.max_queued_events = 16384 fs.inotify.max_user_instances = 1024 fs.inotify.max_user_watches = 1048576 sysctl -p //启动 3)编写inotify脚本 #!/bin/bash host=192.168.112.139(server) src=/opt/ #client上的需要同步的路径 des=master_web #master上配置文件里的模块名 password=/root/rsyncd.passwd #密码文件 user=rsync inotifywait=/usr/bin/inotifywait $inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \ | while read files ;do rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$des echo "${files} was rsynced" >>/tmp/rsync.log 2>&1 done 4)运行脚本 chmod +x /root/inotify.sh nohup bash /root/inotify.sh & #后台形式运行 5)设置开机自启 chmod +x /etc/rc.d/rc.local #这步一定要做,因为该文件默认没有执行权限 echo "nohup bash /root/inotify.sh" >> /etc/rc.d/rc.local
备注:执行完脚本第一次不会马上同步数据,只有当server端文件发生改变时,才会触发同步数据操作
暂无评论内容