CentOS 7 rsync+inotify实现实时同步
测试环境如下:
inotify-slave IP : 172.16.0.222
inotify-master IP : 172.16.0.233
对两台机的要求:
安装依赖包gcc: yum install gcc –y
关闭disabled
开放端口873
一、inotify slave部署
1、 安装rsync:
$ sudo yum install rsync –y
2、 添加用户和模块目录,并更改用户和组
$ sudo useradd rsync –s /sbin/nologin –M $ sudo mkdir /home/jeff/back $ sudo chown rsync.rsync /home/jeff/back/
3、 修改配置文件/etc/rsyncd.conf,内容如下:
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 = /home/jeff/back/ ignore errors read only = no write only = no list = false hosts allow = 10.0.0.0/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password
Tips: ubuntu系统的文件路径一般为/usr/share/doc/rsync/examples/rsyncd.conf,可以创建软链接,结果都一样。创建方法如下:
$ sudo ln -s /usr/share/doc/rsync/examples/rsyncd.conf /etc/rsyncd.conf
4、 配置虚拟用户的密码文件
$ sudo -i # echo ‘rsync_backup:jeff’ >>/etc/rsync.password # exit $ sudo chmod 600 /etc/rsync.password $ sudo rsync --daemon $ sudo ss -tulpn |grep rsync
5、 加入开机启动
$ sudo -i # echo ‘/usr/bin/rsync --daemon’ >>/etc/rc.local # exit
二、部署服务器(inotify-master)
1、 查看系统是否支持inotity
ll /proc/sys/fs/inotify # 如果显示max_queued_events、max_user_instances、max_user_watches就证明支持inotify
2、 安装inotify 3.14
$ wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz $ tar zxf inotify-tools-3.14.tar.gz $ cd inotify-tools-3.14 $ sudo ./configure --prefix=/usr/local/inotify $ sudo make && sudo make install
3、 创建备份目录
$ mkdir /home/jeff/back/
4、 创建rsync服务的密码文件
$ sudo -i # echo “jeff” >/etc/rsync.password //此处只写密码即可 # chmod 600 /etc/rsync.password # exit
5、 编写执行脚本
$ sudo vim /usr/local/inotify.sh
[/usr/local/inotify.sh]
#!/bin/bash
host01=172.16.0.222 //inotify-slaver的IP地址
src=/home/jeff/back //本地监控的目录
dst=backup //inotify-slave的服务模块名
user=rsync_backup //inotify-slave的rsync虚拟用户
rsync_passfile=/etc/rsync.password //本地调用的rsync密码文件
inotify_home=/usr/local/inotify //inotify安装目录
#judge
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
# rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1
cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
exit 0
6、将脚本加入后台执行
$ sh inotify.sh &
7、 加入开机启动
$ sudo -i # echo ‘/bin/bash /home/jeff/inotify.sh &’ >>/etc/rc.local # exit
三、测试实时同步(部署前建议先运行这一步验证,然后再执行第二步)
$ sudo rsync -avz /home/jeff/back/test.txt rsync_backup@10.0.0.22::backup --password-file=/etc/rsync.password
四、改变SSH的默认端口为1234时,rsync的连接方法如下
# 此处用-e 'ssh -p 3333'来指定SSH连接端口 $ /usr/bin/rsync -av /backup/* -e 'ssh -p 3333' root@10.0.0.22:/backup/
PS:操作步骤参考自 http://jingyan.baidu.com/article/656db918ee2f13e380249c4d.html