本文只会教大家配置运行环境,不会介绍Rsync和Inotify的工作原理,有兴趣深入了解的同学可以参考IBM论坛的对Rsync的解释
- 准备工作
- 两台或以上的Linux服务器且内核版本不低于2.6.13
- uname -r- 安装gcc库 [如已安装跳过此步骤]
- yum install gcc- 安装Rsync [所有参与同步的服务器都要安装]
- yum install rsync- 文件存储服务器开放873端口
- firewall-cmd --permanent --add-port=873/tcp - service firewalld restart- 节点服务器 [也可以理解成web服务器] 安装 Inotify
- wget http://repository.timesys.com/buildsources/i/inotify-tools/inotify-tools-3.14/inotify-tools-3.14.tar.gz - tar zxf inotify-tools-3.14.tar.gz - cd inotify-tools-3.14 - ./configure && make && make install - ldconfig - 配置文件存储服务器
- 修改Rsync配置文件 (/etc/rsyncd.conf)
uid = root gid = root use chroot = no max connections =4 exclude = lost+found/ transfer logging = yes timeout = 900 ignore nonreadable = yes dont compress = .gz .tgz .zip .z .Z .rpm .deb .bz2 [test] # 客户端标签,名字你随意 path = /home/ # 需要同步的目录地址 comment = test # 客户端标签 ignore errors read only = yes write only = no list = no auth user = root # 用户名 secrets file = /etc/rsyncd.passwd # 密码文件地址 hosts allow = 192.168.X.X # 客户端IP地址-
配置Rsync客户端访问密码
- vim /etc/rsyncd.password - root:123456 # 配置文件中的auth user,后面是该用户的密码 - chmod 600 /etc/rsyncd.password # 修改文件运行权限 -
启动Rsync服务
systemctl start rsyncd
- 配置节点服务器(或者叫web服务器)
- 配置Rsync密码文件
- vim /etc/rsyncd.password - 123456 # 内容是上面文件存储服务器分配的密码 - chmod 600 /etc/rsyncd.password # 修改文件运行权限 - 配置Inotify运行脚本
- vim /home/inotify.sh # 存储路径和文件名自定义就好,格式是sh就行 [#!/bin/bash /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,create,attrib /home/img/ | while read file do /usr/bin/rsync -vzrtopg --progress /home/img/ 用户名@文件服务器IP::标签 --password-file=/home/rsync.password echo "${files} was rsynced" >> /var/log/rsync.log 2>&1 done] - chmod 600 /home/inotify.sh # 修改Inotify脚本权限 - /home/inotify.sh & # 后台运行此脚本 - 配置Rsync密码文件
至此,Rsync+Inotify的运行环境就已经配置完成了!