Warning: Undefined array key "cperpage" in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 230

Warning: Undefined variable $output in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 300

Warning: Undefined variable $fixed_tags in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 301

Warning: Undefined variable $isshowdots in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 302

Warning: Undefined variable $tag_aditional in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 305

Warning: Undefined variable $tag_aditional in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 308

Warning: Undefined variable $tag_aditional in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 311

Warning: Undefined variable $post in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 320

Warning: Attempt to read property "ID" on null in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 320

Warning: Undefined variable $post in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 320

Warning: Attempt to read property "ID" on null in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 320

Warning: Undefined variable $more_text_link in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 320
Linux下Inotify + Rsync文件实时同步 – 理想社会

Linux下Inotify + Rsync文件实时同步

L

一、使用前言*

随着公司业务的发展,单web服务器不能满足用户的访问,需要增加多台web服务器实现均衡高可用性访问。为了统一各web服务器之间的数据随时一致,人工方式肯定是不可取,考虑到需要实时同步,进而采用inotify+rsync的方案进行同步!

配置环境:centos 5.3 x86_64

rsync版本:rsync-0.3.7

inotify版本:inotify-tools-3.14

服务器端:192.168.2.79 【也是rsync+inotify服务端】

客户端:192.168.2.100 【需要同步,保持数据一致的服务器】

首先下载所需的软件:

http://blog.mgcrazy.com/download/rsync-3.0.7.tar.gz

http://blog.mgcrazy.com/download/inotify-tools-3.14.tar.gz

二、正式安装:

登陆服务端192.168.2.79,下载文件到:/usr/src 下:

cd /usr/src && wget http://blog.mgcrazy.com/download/{inotify-tools-3.14.tar.gz,rsync-3.0.7.tar.gz} && tar xzf rsync-3.0.7.tar.gz && cd rsync-3.0.7 && ./configure –prefix=/usr/local/rsync &&make &&make install & cd ../ && tar xzf inotify-tools-3.14.tar.gz && cd inotify-tools-3.14 && ./configure &&make &&make install

自此rsync和inotify-tools安装完毕!【可以直接拷贝执行】

三、配置ssh认证:

【目的是为了实时同步的时候不需要输入密码】

在服务端执行这个命令生成公钥:ssh-keygen 一路回车:如下图

然后远程拷贝公钥到客户端服务器并重命名为authorized_keys :

scp -r /root/.ssh/id_rsa.pub root@192.168.2.100:/root/.ssh/authorized_keys 即可!

四、配置实时同步脚本:

#!/bin/sh
src=/home/webapps/www
des=/home/webapps/
ip=192.168.2.100

inotifywait -mrq –timefmt ‘%d/%m/%y-%H:%M’ –format ‘%T %w%f’ -e modify,delete,create,attrib ${src} | while read file
do
for i in $ip
do
/usr/local/rsync/bin/rsync -aP –delete $src root@$ip:$des
done
done

保存为rsync.sh 并给执行权限:chmod o+x rsync.sh 这里可以根据实际情况修改需要同步的路径和客户端ip:192.168.2.100 是客户端的ip。

五、测试结果:

剩下就是在后台一直开着这个脚本了:可以用:screen 后台执行; 然后 ./rsync.sh ;crtl+a +d退出,在服务端修改、创建一个文件,相当于一个动作会触发inotify,如果没有动作,默认它是不同步的,然后系统会自动执行rsync同步到客户端,你可以去客户端查看数据!

本文出自 “【烟雨楼台】” 博客,请务必保留此出处http://wgkgood.blog.51cto.com/1192594/533695

About the author

Add comment

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

By now163

Your sidebar area is currently empty. Hurry up and add some widgets.