inux下两台服务器文件实时同步方案设计和实现

假设有如下需求:

假设两个服务器:

192.168.0.1 源服务器  有目录 /opt/test/

192.168.0.2 目标服务器  有目录 /opt/bak/test/

实现的目的就是保持这两个服务器某个文件目录保持实时同步

实现方式: 通过rsync+inotify-tools结合来实现

需要安装软件:

1.  rsync 同步软件

在 源服务器 和 目标服务器 都需要安装

源服务器: 是rsync客户端,不需要配置

目标服务器: 是rsync服务器端,需要配置/etc/rsyncd.conf里的内容

安装后需要新建配置文件:/etc/rsyncd.conf

配置文件在: /etc/

文件内容如下:

uid = root
gid = root
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

[www]
path= /opt/bak/test
comment= analyse
read only = false
hosts allow = *

2.  inotify-tools 工具

该工具为文件实时监控工具,需要linux操作系统内核支持,内核支持需要至少版本为2.6.13

检查操作系统是否支持,执行如下:

uname -r  查看版本

返回:

2.6.32-220.4.1.el6.x86_64

则表示版本2.6.32 大于2.6.13,则支持。

执行:

ll /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Oct 18 12:18 max_queued_events
-rw-r--r-- 1 root root 0 Oct 18 12:18 max_user_instances
-rw-r--r-- 1 root root 0 Oct 18 12:18 max_user_watches

有三项输出,则表示默认支持inotify,可以安装inotify-tools工具.

如果不支持,需要采用新版本的linux操作系统

版本达到要求,就可以安装了。

安装inotify-tools后会在相关安装目录下生成如下两个文件:

ll /usr/local/bin/
total 88
-rwxr-xr-x 1 root root 44327 Oct 10 15:32 inotifywait
-rwxr-xr-x 1 root root 41417 Oct 10 15:32 inotifywatch

则表示安装成功。

注意: 在 源服务器上需要安装,目标服务器上不需要安装inotify。

3. 相关脚本:

在源服务器上新建脚本:

inotify_bak.sh

#!/bin/bash
src=/opt/test/
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T
%w%f%e' -e close_write,delete,create,attrib $src |  while read file
do
   
      /usr/bin/rsync -arzuq $src 192.168.0.2::www/

echo "  ${file} was rsynced" >>/opt/soft/log/rsync.log 2>&1
done

注意: 这里的 www 是在目标服务器/etc/rsyncd.conf里配置的模块名称:[www]

赋予执行权限: chmod +x  inotify_bak.sh

然后执行: inotify_bak.sh &  放入后台执行

4. 关于启动

目标服务器:先启动rsync后台服务: /usr/bin/rsync --daemon

来源服务器: 执行 inotify_bak.sh &

5. 测试:

在来源服务器目录中新建目录和文件,inotify_bak.sh脚本会检测到,然后同步到目标服务器的相关目录下

可以查看日志文件: /opt/soft/log/rsync.log 命令如下:观察实时同步的情况。

tail -f  /opt/soft/log/rsync.log

转自:http://blog.csdn.net/5iasp/article/details/13630927

linux下两台服务器文件实时同步方案设计和实现的更多相关文章

  1. linux下两台服务器文件实时同步方案实现-乾颐堂

    假设有如下需求: 假设两个服务器: 192.168.0.1 源服务器  有目录 /opt/test/ 192.168.0.2 目标服务器  有目录 /opt/bak/test/ 实现的目的就是保持这两 ...

  2. Centos 6.5 rsync+inotify 两台服务器文件实时同步

    rsync和inotify是什么我这里就不在介绍了,有专门的文章介绍这两个工具. 1.两台服务器IP地址分别为: 源服务器:192.168.1.2 目标服务器:192.168.1.3 @todo:从源 ...

  3. Linux下 两台机器文件/文件夹 相互拷贝

    Linux下 两台机器文件/文件夹 相互拷贝 设有两台机器 :A:*.101及 B:*.102. 把A下的.temp/var/a.txt拷贝到B机器的/text/目录下: 进入B机器:scp root ...

  4. linux使用rsync、inotify-tools实现多台服务器文件实时同步

    需求:将本地192.168.1.10上的/data/wwwroot目录同步到 1.来源服务器上安装rsync.inotify-tools yum -y install rsync yum -y ins ...

  5. Linux下Rsync+sersync实现数据实时同步

    inotify 的同步备份机制有着缺点,于是看了sersync同步,弥补了rsync的缺点.以下转自:http://www.osyunwei.com/archives/7447.html 前言: 一. ...

  6. Linux下Rsync+Inotify-tools实现数据实时同步

    Linux下Rsync+Inotify-tools实现数据实时同步 注意:下面的三个案例都是rsync 每次都是全量的同步(这就坑爹了),而且 file列表是循环形式触发rsync ,等于有10个文件 ...

  7. sersync+rsync实现服务器文件实时同步

    sersync+rsync实现服务器文件实时同步 一.为什么要用rsync+sersync架构? 1.sersync是基于inotify开发的,类似于inotify-tools的工具 2.sersyn ...

  8. linux实现多台服务器文件同步

    inotify-tools+rsync实时同步文件安装和配置 Linux+Nginx+PHP+MySQL+MemCached+eaccelerator安装优化记录(见 http://www.linux ...

  9. 【Linux】两台服务器ssh免密登录

    背景: 有些场景可能用到两台服务器ssh免密登录.比如服务器自动化部署 开始准备:  服务器A  linux   ip: 192.168.1.1 服务器B  linux  ip: 192.168.1. ...

随机推荐

  1. Android] Android XML解析学习——方式比较

     [Android] Android XML解析学习——方式比较 (ZT)  分类: 嵌入式 (From:http://blog.csdn.net/ichliebephone/article/deta ...

  2. Android PopupWindow的使用技巧(转)

    Android PopupWindow的使用技巧 PopupWindow是Android上自定义弹出窗口,使用起来很方便. PopupWindow的构造函数为 public PopupWindow(V ...

  3. ural2062 Ambitious Experiment

    Ambitious Experiment Time limit: 3.0 secondMemory limit: 128 MB During several decades, scientists f ...

  4. ural1613 For Fans of Statistics

    For Fans of Statistics Time limit: 1.0 secondMemory limit: 64 MB Have you ever thought about how man ...

  5. PAT (Advanced Level) 1081. Rational Sum (20)

    简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  6. iOS之UITableView的上拉刷新

    #import "ViewController.h" #import "UITableView+PullRefresh.h" @interface ViewCo ...

  7. JAVA中字符串函数subString的用法小结

    本篇文章主要是对JAVA中字符串函数subString的用法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 String str; str=str.substring(int begi ...

  8. Cocos2dx 学习笔记整理----开发环境搭建

    最近在学习cocos2dx,预备将学习过程整理成笔记. 需要的工具和环境整理一下: 使用的版本 cocos2dx目前已经出到了v3.1.1,学习和项目的话还是用2.2.3为宜,毕竟不大想做小白鼠,并且 ...

  9. 9、手把手教你Extjs5(九)使用MVVM特性控制菜单样式

    菜单的样式多了,怎么可以灵活的切换是个问题. 在使用标准菜单的时候,在菜单最前面有二个按钮,可以切换到树状菜单和按钮菜单. 在树状菜单的显示区,可以切换换到标准菜单,以及折叠式菜单. 切换到按钮菜单之 ...

  10. arm-linux-gnueabi和arm-linux-gnueabihf 的区别

    转载整理自:http://www.cnblogs.com/xiaotlili/p/3306100.html 一. 什么是ABI和EABI1 .ABI ABI(二进制应用程序接口-Application ...