假设有如下需求:

假设两个服务器:

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

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

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

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

准备工作:

首先要给两台机器添加信任关系,具体方法已经在前面的文章介绍过了

详情查看: linux添加信任关系免密码登录

需要安装软件:

1.  rsync 同步软件

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

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

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

2.  inotify-tools 工具

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

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

uname -r  查看版本

返回:

1
    2.6.32-358.6.1.el6.x86_64

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

执行:

1
2
3
4
5
    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后会在相关安装目录下生成如下两个文件:

1
2
3
4
    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

1
2
3
4
5
6
7
    #!/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.1::www/
          echo "  ${file} was rsynced" >>/opt/soft/log/rsync.log 2>&1
    done

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

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

4. 关于启动

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

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

5. 测试:

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

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

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

错误解决:

/usr/local/bin/inotifywait: error while loading shared libraries: libinotifytools.so.0: cannot open shared object file: No such file or directory

这是因为找不到库文件的原因,做一个软连接就好了

1
ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0

http://www.qytang.com/
http://www.qytang.com/cn/list/28/358.htm
http://www.qytang.com/cn/list/41/
http://www.qytang.com/cn/list/37/
http://www.qytang.com/cn/list/46/
http://www.qytang.com/cn/page/19.htm
http://www.qytang.com/cn/list/32/
http://www.qytang.com/cn/list/28/
http://www.qytang.com/cn/list/25/
http://www.qytang.com/cn/list/28/625.htm
http://www.qytang.com/cn/list/28/612.htm

linux下两台服务器文件实时同步方案实现-乾颐堂的更多相关文章

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

    inux下两台服务器文件实时同步方案设计和实现 假设有如下需求: 假设两个服务器: 192.168.0.1 源服务器  有目录 /opt/test/ 192.168.0.2 目标服务器  有目录 /o ...

  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. python 生成器和生成器表达式

    1.生成器 生成器的本质就是迭代器 生成器的特点和迭代器一样.取值方式和迭代器一样(__next__(),send():给上一个yield传值) 生成器一般由生成器函数或者生成器表达式来创建 其实就是 ...

  2. Docker for windows 7 - 加载 docker images

    背景 由于之前一直是在 Linux 上面跑,所以对于docker for windows 部分不是很熟. 由于我们的合作伙伴需要在windows 上面跑我们的docker image, 所以在自己的w ...

  3. 2017百度之星初赛A-1006(HDU-6113)

    思路:在图的外面包一圈'0'字符,然后dfs统计'0'字符的个数和'1'字符的个数.结果如下(num0表示0字符的个数,num1表示1字符的个数): num0 == 1 && num1 ...

  4. 学生党成功拿到阿里技术offer:面Java开发,却是C++考官,几个意思?

    摘要: 这是我为大家分享的如何拿到阿里技术offer系列文章中的第一篇,今天分享的文章的作者是一位在2015年阿里的校招中成功得到offer的美女学姐,从学姐的这篇文章中我们能学到很多在阿里面试的宝贵 ...

  5. [知识整理]Linux系统WIFI知识的一些整理

    前段时间接触了wifi,主要是在linux系统下做预研.开发.本文根据个人收集资料及研究经验做了一些基本入门级别的引子,旨在对wifi有一个很基础的入门的认知,比如知道wifi模块硬件接口有哪些,了解 ...

  6. Windows网络编程基础知识

    1.WinSock的初始化 #include<iostream> #include<WinSock2.h> #include<MSWSock.h> #pragma ...

  7. [Cpp primer] range for (c++11)

    for (declaration : expression) statement; /* This statement will iterate through the elements in the ...

  8. spring 的xml配置使用p标签简化

    1.常见配置 比如配置数据源 读取properties <!-- 配置阿里巴巴数据源 --> <bean id="dataSource" class=" ...

  9. nginx的下载和安装

    安装 下载必要组件 nginx下载地址 http://nginx.org/en/download.html  pcre库下载地址,nginx需要[解析正则] http://sourceforge.ne ...

  10. Django-组件--用户认证Auth(auth_user增加字段)

    引入: from django.db import models from django.contrib.auth.models import AbstractBaseUser 源码 : from d ...