rsync和inotify是什么我这里就不在介绍了,有专门的文章介绍这两个工具。

1、两台服务器IP地址分别为:

源服务器:192.168.1.2

目标服务器:192.168.1.3

@todo:从源服务器(192.168.1.2)的/www/目录下的所有的文件实时同步到目标服务器(192.168.1.3)的/www_bak/目录下

源服务器下需要安装rsync和inotify,源服务器做为server端,实时的向目标服务器client端发送数据

2、安装 rsync

一般centos6.5下都已经安装了rsync,所以就不必安装了,可以用以下命令检查一下是否已安装:

rpm -qa |grep rsync

上图显示了我的机器上安装的是rsync-3.0.6-12。

如果没有安装请往下看,如果已安装,那就跳过下面的部分:

cd /usr/local/src

wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz

tar zxvf rsync-3.0.9.tar.gz

cd rsync-3.0.9

./configure --prefix=/usr/local/rsync

make

make install

rsync已安装完毕

3、创建同步文件所需要的密码文件,这样做是为了安全

touch /etc/rsyncd.secrets

echo 'newpassword' > /etc/rsyncd.secrets

注:这里的newpassword可以是任意字符

出于安全考虑要把此文件的权限改成600:

chmod 600 /etc/rsyncd.secrets

4、安装inotify

先查看服务器是否支持inotify

ll /proc/sys/fs/inotify

会有三个文件,这说明此服务器是支持 inotify的。

下面安装inotify:

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=/usr/local/inotify
make
make install

5、创建rsync复制脚本,用户shell来实现,其功能就是:从源服务器(192.168.1.2)的/www/目录下的所有的文件无论是添加、修改、删除文件,能够通过inotify监控到,并通过rsync实时同步到目标服务器(192.168.1.3)的/www_bak/目录下

vim /usr/bin/rsync.sh

#!/bin/bash
host=192.168.1.3
src=/www/
des=web
user=webuser
/usr/local/inotify/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 -zrtopg --delete --progress --password-file=/etc/rsyncd.secrets $src $user@$host::$des
echo "${files} was rsynced" > /var/log/rsyncd.log 2>&1
done

其中host是目标服务器的ip,src是源服务器要同步的目录,des是认证模块名,需要与目标服务器一致,user是建立密码文件里的认证用户。

修改rsync.sh的权限

chmod +x /usr/bin/rsync.sh

到此为止,源服务器的所有操作就完成了。下面配置目标服务器。

1、目标服务器也要安装 rsync,安装方式跟源服务器一样,这里就不在赘述了。

2、建立密码文件:

touch /etc/rsyncd.secrets

echo "webuser:newpassword" > /etc/rsyncd.secrets

同样要给此文件一个600的权限

chmod 600 /etc/rsyncd.secrets

注:在源服务器建立的密码文件,只有密码,没有用户名;而在目标服务器里建立的密码文件,用户名与密码都有。

3、写rsync的配置文件:

vim /etc/rsyncd.conf

uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
#log format = %t %a %m %f %b # 日志记录格式
[web]
path = /www_bak/
comment = web file
ignore errors
read only = no
write only = no
hosts allow = 192.168.1.2
hosts deny = *
list = false
uid = root
gid = root
auth users = webuser
secrets file = /etc/rsyncd.secrets

4、目标服务器启动 rsync

/usr/bin/rsync --daemon --config=/etc/rsyncd.conf

5、源服务器启动同步:

/usr/bin/rsync.sh &

到这里,所有的都已完成。可以到源服务器下的/www目录下建一个文件,然后再看一下目标服务器下的/www_bak/下是否有?

最新文章

  1. 无法解析指定对象的 TargetProperty (UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)“的异常解决
  2. tomcat启动的了,但是加载项目失败
  3. 反射在ADO.NET中的运用(你还在每个项目中循环遍历DataTable吗)
  4. ionic react-native和native开发移动app那个好
  5. codevs 1835 魔法猪学院 A*寻k短路做了一个月卡死在spfa那了/(ㄒoㄒ)/~~
  6. malloc/free vs new/delete
  7. IDoc
  8. mysql 表日常变化前几
  9. ubuntu系统下安装gstreamer的ffmpeg支持
  10. BLK-MD-BC04-B蓝牙模块的资料
  11. sqlserver 2008 R2 分区表测试
  12. Linux进程间通信——使用匿名管道
  13. Windows Phone开发(33):路径之其它Geometry
  14. jdb
  15. 分红包算法Java实现
  16. Azure CosmosDB (6) 冲突类型和解决策略
  17. java.util.Stack类中的peek()方法
  18. Spring框架学习02——Spring IOC 详解
  19. 浏览器初始页面设置及被hao123劫持解决办法
  20. Linux使用技巧(一):vim中选中多行、复制和粘贴

热门文章

  1. php+js实现分页
  2. Web测试中常见分享问题
  3. lumia520刷机注意事项
  4. SQL注入的分类
  5. NOIP 考前 队列复习
  6. 实验一、熟悉DOS命令
  7. POJ1584 判断多边形是否为凸多边形,并判断点到直线的距离
  8. LinuxMint17.1 Rebecca中安装设置输入法
  9. SQLite数据库在多线程写锁文件的解决办法
  10. 。net新人报道