rsync数据同步优缺点
与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。
随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足。首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!
inotify
inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。
一、环境准备
IP地址: inotify_rsync_client:192.168.1.103 rsync_server:192.168.1.189 操作系统: centos 6.9 64位
二、rsync部署(每步说明了在那里配置)
1、关闭SELINUX(inotify_rsync_client、rsync_server均配置) vi /etc/selinux/config 修改下面一行代码: SELINUX=disabled 运行此命令立即生效。 setenforce 0 2、开启防火墙tcp 873端口、或关掉防火墙(inotify_rsync_client、rsync_server均配置) vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT 或者关闭防火墙 service iptables stop chkconfig iptables off 3、安装rsync服务端软件(inotify_rsync_client配置) yum install rsync xinetd vi /etc/xinetd.d/rsync 修改disable的值为no: disable = no 启动xinetd(CentOS中是以xinetd来管理Rsync服务的) /etc/init.d/xinetd start chkconfig xinetd on 4、安装xinetd(rsync_server配置) yum install rsync xinetd vi /etc/xinetd.d/rsync 修改disable的值为no: disable = no 启动xinetd(CentOS中是以xinetd来管理Rsync服务的) /etc/init.d/xinetd start chkconfig xinetd on rsync --daemon --config=/etc/rsyncd.conf echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.local 5、创建rsyncd.conf配置文件(rsync_server配置。假如需要同步多个目录,注意加多个目录;此root是rsync的认证账号,后面步骤会配置认证账号和密码) 创建配置文件: vi /etc/rsyncd.conf log file = /var/log/rsyncd.log pidfile = /var/run/rsyncd.pid lock file = /var/run/rsync.lock secrets file = /etc/rsyncd.secret motd file = /etc/rsyncd.motd [test] path = /home/xyz/ comment = test uid = root gid = root incoming chmod = Du=rwx,Dog=rx,Fu=rwx,Fgo=rx port=873 use chroot = no read only = no list = no max connections = 200 timeout = 600 auth users = root hosts allow = 192.168.1.103 hosts deny = * 目录权限(如果uid和gid都是root,这里不用操作): cd /home chown -hR root.root xyz/ #如果xyz目录是在root下新建的,默认就是root权限。 6.创建用户认证文件(rsync_server) 配置文件 vi /etc/rsyncd.passwd root:123456 保存退出 7.设置文件权限(rsync_server) 设置文件所有者读取、写入权限 chmod 600 /etc/rsyncd.conf chmod 600 /etc/rsyncd.passwd 8.启动rsync(rsync_server) /etc/init.d/xinetd start 参考指令 停止:service xinetd stop 启动:service xinetd restart 9、创建用户认证文件(inotify_rsync_client配置) 配置文件 vi /etc/rsyncd.passwd 123456 保存退出 chmod 600 /etc/rsyncd.passwd 10、从inotify_rsync_client手动rsync同步到rsync_server看下,只有成功后(查看日志),下面inotify才会成功。(inotify_rsync_client上运行此命令测试) rsync -avzrtopgL --progress /root/ [email protected]::test/ --password-file=/etc/rsyncd.passwd
三、sersync部署(inotify_rsync_client配置)
1、查看服务器内核是否支持inotify 列出文件目录 ll /proc/sys/fs/inotify 出现下面的内容、说明服务器内核支持inotify -rw-r--r-- 1 root root 0 Dec 25 12:03 max_queued_events -rw-r--r-- 1 root root 0 Dec 25 15:05 max_user_instances -rw-r--r-- 1 root root 0 Dec 25 12:03 max_user_watches 备注:centos6.9默认支持inotify 2.修改inotify默认参数(inotify默认内核参数值太小) sysctl -a | grep max_queued_events sysctl -a | grep max_user_watches sysctl -a | grep max_user_instances 修改参数 sysctl -w fs.inotify.max_queued_events="99999999" sysctl -w fs.inotify.max_user_watches="99999999" sysctl -w fs.inotify.max_user_instances="65535" 生效 sysctl -p 参数说明: max_queued_events: inotify队列最大长度,如果值太小,会出现” Event Queue Overflow “错误,导致监控文件不准确 max_user_watches: 要同步的文件包含多少目录,可以用:find /var/www/synctest -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/var/www/synctest为同步文件目录) max_user_instances: 每个用户创建inotify实例最大值 3、最好更改最大连接数、最大文件描述符。 vi /etc/pam.d/login session required /lib64/security/pam_limits.so vi /etc/security/limits.conf * soft nproc 65535 * hard nproc 65535 * soft nofile 65535 * hard nofile 65535 重启服务器 4、安装编译工具和inotify-tools [root@Monitor nginx]# yum install make gcc gcc-c++ [root@Monitor nginx]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz [root@Monitor nginx]# tar xf inotify-tools-3.14.tar.gz [root@Monitor nginx]# cd inotify-tools-3.14 [root@Monitor inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify [root@Monitor inotify-tools-3.14]#make [root@Monitor inotify-tools-3.14]#make && make install 5、命令和手动测试 一共安装了2个工具(命令),即inotifywait和inotifywatch inotifywait:在被监控的文件或目录上等待特定文件系统事件(open、close、delete等)发生,执行后处于阻塞状态,适合在shell脚本中使用。 inotifywatch:收集被监视的文件系统使用度统计数据,指定文件系统事件发生的次数统计。 1)inotify命令常用参数详解 [root@nfs-server inotify-tools]# ./bin/inotifywait --help 2)测试监控事件create: [root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data ##实行当前命令后,界面处于阻塞状态,只有在另外一个客户端测试时,才会显示监控状态 31/01/16 10:23 /data/ceshi.txt 31/01/16 10:24 /data/what 31/01/16 10:27 /data/a 31/01/16 10:27 /data/b 31/01/16 10:27 /data/c 31/01/16 10:27 /data/d 31/01/16 10:27 /data/e 31/01/16 10:27 /data/f 克隆一个NFS客户端切换到/data目录下,新建ceshi.txt、what文件,观察监控的变化 [root@nfs-server data]# touch ceshi.txt [root@nfs-server data]# mkdir /who [root@nfs-server data]# touch what [root@nfs-server data]# [root@nfs-server data]# touch {a..f} 3)测试监控事件delete: [root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete /data ##监控/data目录删除时间 监控如下: 31/01/16 10:31 /data/reew.txt 31/01/16 10:31 /data/test1.txt [root@nfs-server data]# rm -f reew.txt test1.txt ##删除这两个文件 [root@nfs-server data]# 4)测试监控事件create、delete: [root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create /data 监控输出信息: 31/01/16 10:35 /data/hero.sh 31/01/16 10:35 /data/a 31/01/16 10:35 /data/f 31/01/16 10:36 /data/b 31/01/16 10:36 /data/c 31/01/16 10:36 /data/d 31/01/16 10:36 /data/e [root@nfs-server data]# touch hero.sh [root@nfs-server data]# ll [root@nfs-server data]# rm -f [a..f] [root@nfs-server data]# rm -f {a..f} [root@nfs-server data]# 一般工作中使用到: [root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /data 6、工作环境中通过脚本实时同步 #!/bin/bash inotify=/usr/local/inotify-tools/bin/inotifywait $inotify -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /data |while read file do cd / && rsync -az /data --delete [email protected]::test --password-file=/etc/rsync.password done chmod +x inotify.sh sh -x inotify.sh 7、同步文件测试 8、后台运行脚本或者设置定时任务