您的位置:首页 > 其它

rsync+inotify实现实时同步

2013-07-11 13:12 253 查看
soft:rsync-3.0.8.tar.gz inotify-tools-3.14.tar.gz

wget http://rsync.samba.org/ftp/rsync/rsync-3.0.8.tar.gz
wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
server:192.168.2.211

bakserver:192.168.2.67

bakserver(被同步端):

rsync安装
tar xzvf rsync-3.0.8.tar.gz
cd rsync-3.0.8
./configure
make
make install

mkdir /etc/rsyncd
vim rsyncd.conf

uid = root
gid = root
user chroot = no
max connections = 50
timeout = 180
pid file = /etc/rsyncd/rsyncd.pid
lock file= /etc/rsyncd/rsyncd.lock
log file = /var/log/rsyncd.log
transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
secrets file = /etc/rsyncd/rsyncd.password

[nagios]
path=/usr/local/nagios/
ignore errors
hosts allow = 192.168.0.0/22
hosts deny = 0.0.0.0/32
secrets file = /etc/rsyncd/rsyncd.password
read only = no
list = no
auth users = yunwei
保存退出。
echo "yunwei:123456" >/etc/rsyncd/rsyncd.password   #yunwei:123456,前者为认证用户名,后者为认证密码。
chmod 600 /etc/rsyncd/rsyncd.password   #安全考虑

启动服务:rsync --daemon --config=/etc/rsyncd/rsyncd.conf
加入自启动:ehoc "rsync --daemon --config=/etc/rsyncd/rsyncd.conf" >/etc/rc.d/rc.local

server:

rsync安装
tar xzvf rsync-3.0.8.tar.gz
cd rsync-3.0.8
./configure
make
make install

tar xvzf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install

ll /usr/local/bin/inotifywa*
-rwxr-xr-x 1 root root 37264 04-14 13:42 /usr/local/bin/inotifywait
-rwxr-xr-x 1 root root 35438 04-14 13:42 /usr/local/bin/inotifywatch
inotify-tools安装完成后,会生成inotifywait和inotifywatch两个指令,其中,
inotifywait用于等待文件或文件集上的一个特定事件,它可以监控任何文件和目录设置,并且可以递归地监控整个目录树。
inotifywatch用于收集被监控的文件系统统计数据,包括每个inotify事件发生多少次等信息。

创建认证密码:
mkdir /etc/rsyncd
echo "123456" >/etc/rsyncd/rsyncd.password     #此处只需密码即可。
chmod 600 /etc/rsyncd/rsyncd.password

客户端运行脚本:

vim nagios_rsync.sh
#!/bin/bash
src=/usr/local/nagios/
des=nagios           #此处为模块名称。
ip=192.168.2.67
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib $src | while read file
do
rsync -vzrtopg --delete --progress $src yunwei@$ip::$des --password-file=/etc/rsyncd/rsyncd.password >/dev/null 2>&1
done

保存,赋予执行权限。

后台运行:
nohup ./nagios_rsync.sh &

加入自启动:
echo "nohup ./nagios_rsync.sh &" >>/etc/rc.d/rc.local
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: