CentOS 7 rsync+inotify实现实时同步
环境准备
测试环境如下:
inotify-slave IP : 172.31.15.10
inotify-master IP : 172.31.15.11
对两台机的要求:
- 安装依赖包gcc: yum install gcc –y
- 关闭selinux
- 开放端口873
inotify slave部署
安装rsync:
1
| [root@opstrip opt]# yum install rsync –y
|
添加用户和模块目录,并更改用户和组
1 2 3
| [root@opstrip opt]# useradd rsync –s /sbin/nologin –M [root@opstrip opt]# mkdir /data/back [root@opstrip opt]# chown rsync.rsync /data/back/
|
修改配置文件/etc/rsyncd.conf
,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| uid = rsync gid = rsync 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 [backup] path = /data/back/ ignore errors read only = no write only = no list = false hosts allow = 172.31.15.0/24 hosts deny = 0.0.0.0/0 auth users = rsync_backup secrets file = /etc/rsync.password
|
**注:ubuntu系统的文件路径一般为/usr/share/doc/rsync/examples/rsyncd.conf
,可以创建软链接,结果都一样。**创建方法如下:
1
| [root@opstrip opt]# ln -s /usr/share/doc/rsync/examples/rsyncd.conf /etc/rsyncd.conf
|
配置虚拟用户的密码文件
1 2 3 4
| [root@opstrip opt]# echo 'rsync_backup:jeff' >>/etc/rsync.password [root@opstrip opt]# chmod 600 /etc/rsync.password [root@opstrip opt]# rsync --daemon [root@opstrip opt]# ss -tulpn |grep rsync
|
加入开机启动
1
| [root@opstrip opt]# echo '/usr/bin/rsync --daemon' >>/etc/rc.local
|
部署服务器(inotify-master)
查看系统是否支持inotity
1 2 3 4 5
| [root@opstrip opt]# ll /proc/sys/fs/inotify total 0 -rw-r--r-- 1 root root 0 Jan 24 17:15 max_queued_events -rw-r--r-- 1 root root 0 Jan 24 17:15 max_user_instances -rw-r--r-- 1 root root 0 Jan 24 17:15 max_user_watches
|
Tips:如果显示max_queued_events
、max_user_instances
、max_user_watches
就证明支持inotify
。
安装inotify 3.14
1 2 3 4 5
| [root@opstrip opt]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz [root@opstrip opt]# tar zxf inotify-tools-3.14.tar.gz [root@opstrip inotify-tools-3.14]# cd inotify-tools-3.14 [root@opstrip inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify [root@opstrip inotify-tools-3.14]# make && sudo make install
|
创建备份目录
1
| [root@opstrip opt]# mkdir /opt/back/
|
创建rsync服务的密码文件
1 2
| [root@opstrip opt]# echo "jeff" >/etc/rsync.password //此处只写密码即可 [root@opstrip opt]# chmod 600 /etc/rsync.password
|
编写执行脚本
1
| [root@opstrip opt]# vim /usr/local/inotify.sh
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #!/bin/bash host01=172.31.15.10 //inotify-slaver的IP地址 src=/opt/back //本地监控的目录 dst=backup //inotify-slave的服务模块名 user=rsync_backup //inotify-slave的rsync虚拟用户 rsync_passfile=/etc/rsync.password //本地调用的rsync密码文件 inotify_home=/usr/local/inotify //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
|
将脚本加入后台执行
1
| [root@opstrip opt]# sh inotify.sh &
|
加入开机启动
1
| [root@opstrip opt]# echo '/bin/bash /opt/inotify.sh &' >>/etc/rc.local
|
测试实时同步
部署前建议先运行这一步验证,然后再执行第二步
1
| [root@opstrip opt]# rsync -avz /opt/back/test.txt rsync_backup@172.31.15.10::backup --password-file=/etc/rsync.password
|
改变SSH的默认端口为3333时,rsync的连接方法如下
1 2
| [root@opstrip opt]# /usr/bin/rsync -av /backup/* -e 'ssh -p 3333' root@172.31.15.10:/backup/
|