最近一直在看高性能Linux服务器这本书,要趁学会了这个热乎劲写个博文整理一下。
rsync是Linux系统下的文件同步和数据传输工具,但是单独用rsync做文件同步有个缺点就是不能实现实时同步,这里就要用到inotify了,他是Linux2.6.13版本起开始支持的强大的、细粒度的、异步的文件系统时间监控机制。
开始正题,安装rsync,基本就是./configure ,然后 make && make install,inotify-tools的安装也是类似,这里就不赘述了。
这里的环境是cent os 6.2(10.0.0.16作为服务器端),centos6.0(10.0.0.7作为客户端)
下面是客户端/etc/rsyncd.conf的配置内容
- uid = nobody
- gid = nobody
- use chroot = no
- max connections = 10
- strict modes = yes
- pid file = /var/run/rsyncd.pid
- lock file = /var/run/rsyncd.lock
- log file = /var/run/rsyncd.log
- [web]
- path = /opt/web/
- comment = nagios document
- ignore errors
- read only = no
- write only = no
- hosts allow = 10.0.0.16
- hosts deny = *
- list = false
- uid = root
- gid = root
- auth users = shanker
- secrets file = /etc/web.pass
- #!/bin/bash
- host=10.0.0.7
- src=/opt/web/
- dst=web
- user=shanker
- /usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' \
- -e modify,delete,create,attrib \
- $src \
- | while read files
- do
- /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user@$host::$dst
- echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
- done