inotify是在Linux 2.6.13 内核中新引入的文件系统变化通知机制。inotify 是一种文件系统的变化通知机制,如文件增加、删除等事件可以立刻让用户态得知。而rsync是unix/linux平台下的一款高效的镜像备份工具。rsync也有缺点,最大的问题就是每次执行rsync命令都会遍历目标目录。在文件非常多的情况下,每次遍历都会消耗很多资源,所以往往在备份中,inotify强大的细粒度异步文件系统事件监控机制配和rsync的强大的镜像备份能力结力是一个不错的选择。
需求:六台主机之间同步数据。其中一台为主,其他五台从第一台同步数配置数据。第二、三为windows,第一、四、五、六台为linux 。
一、rsync的安装
1、第二至六主机安装rsync服务端。windows主机上的安装配置,在此省略,具体可以参看另一篇日志——cwrsyncserver的安装 。linux下的安装也十分简单,一般发行版上自带的都有。本次以centos/redhat为例 。可通过yum安装
rpm -qa|grep rsync 查看是否安装
yum -y install rsync 安装
2、在/etc下新建rsyncd目录,在该目录下新建rsyncd.conf文件,其内容如下:
pid file = /var/run/rsyncd.pid
port = 873
#address = 192.168.1.35 有多块网卡时,此配置用于指定监听的网卡
uid = root 运行RSYNC守护进程的用户(出于安全配置可以配置为nobody)
gid = root 运行RSYNC守护进程的组
use chroot = yes
read only = yes 全局配置,所有指定的目录模块只有读的权限
#limit access to private LANs
hosts allow=192.168.1.0/255.255.255.0 多个IP或段之间使用空格或英文逗号分隔
hosts deny=*
max connections = 5
motd file = /etc/rsyncd/rsyncd.motd
#This will give you a separate log file
log file = /var/log/rsync.log
#This will log every file transferred - up to 85,000+ per user, per sync
#transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
[sdir]
read only = no
path = /App/sdir/SDIR_ROOT
list=no
ignore errors
auth users = test
secrets file = /etc/rsyncd/rsyncd.secrets
comment = sdir
3、配置密码认证文件和motd提示文件
[root@test rsyncd]# pwd
/etc/rsyncd
[root@test rsyncd]# ll
总用量 20
-rw-r--r-- 1 root root 1161 12月 11 10:12 rsyncd.conf
-rw-r--r-- 1 root root 357 6月 21 13:51 rsyncd.motd
-rw------- 1 root root 9 6月 21 13:51 rsyncd.password
-rw------- 1 root root 14 6月 21 13:51 rsyncd.secrets
4、启动并加入rc.local
/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
echo "/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf" >> /etc/rc.local
注:步骤3中,密码文件的权限一定要设成600,记得在iptables上开启rsync server使用的端口873 。
二、安装inotify-tools
1、到git上下载最新的inotify安装。其项止地址为:https://github.com/rvoicilas/inotify-tools/wiki
2、下载安装
wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/App/inotify
make && make install
3、inotify 可以监视的文件系统事件
- IN_ACCESS,即文件被访问
- IN_MODIFY,文件被 write
- IN_ATTRIB,文件属性被修改,如 chmod、chown、touch 等
- IN_CLOSE_WRITE,可写文件被 close
- IN_CLOSE_NOWRITE,不可写文件被 close
- IN_OPEN,文件被 open
- IN_MOVED_FROM,文件被移走,如 mv
- IN_MOVED_TO,文件被移来,如 mv、cp
- IN_CREATE,创建新文件
- IN_DELETE,文件被删除,如 rm
- IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己
- IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己
- IN_UNMOUNT,宿主文件系统被 umount
- IN_CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
- IN_MOVE,文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO)
- 注:上面所说的文件也包括目录。
4、inotify相关参数
inotify相关的系统参数配置有三个
/proc/sys/fs/inotify/max_queued_events
/proc/sys/fs/inotify/max_user_instances
/proc/sys/fs/inotify/max_user_watches
平时需要更改的是最后一个参数,即监控的文件上限数。如果监控的文件数目巨大,需要根据实际情况适当增加此值得大小。如:
echo 650000> /proc/sys/fs/inotify/max_user_watches
三、完成rsync+inotify整合
配置脚本sdirdata.sh,内容如下:
#!/bin/bash
host1=192.168.1.30
host2=192.168.1.31
host3=192.168.1.32
host4=192.168.1.33
host5=192.168.1.34
user=test
src=/App/sdir/SDIR_ROOT/
/App/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' -e close_write,create,move,delete,attrib $src | while read files
do
/usr/bin/rsync -rdvzP --delete --password-file=/etc/rsyncd/rsyncd.password $src $user@$host1::sdir
/usr/bin/rsync -rdvzP --delete --password-file=/etc/rsyncd/rsyncd.password $src $user@$host2::sdir
/usr/bin/rsync -avzP --delete --password-file=/etc/rsyncd/rsyncd.password $src $user@$host3::sdir
/usr/bin/rsync -avzP --delete --password-file=/etc/rsyncd/rsyncd.password $src $user@$host4::sdir
/usr/bin/rsync -avzP --delete --password-file=/etc/rsyncd/rsyncd.password $src $user@$host5::sdir
echo "${files} was rsynced " >> /tmp/rsync.log 2>&1
done
其中,30和31为windows服务器,后面三台为linux服务器,在1.35上运行sh sdirdata.sh &运行文件监控与备份同步。一旦inotify监测到文件变更,rsync会立即同步到各服务器。