微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Centos 7.0 通过rsync和inotify实现实时同步

工具/原料

  • centos 7.0
  • rsync
  • inotify

第一步: 准备工作

  1. 1

    1. inotify介绍

    Inotify 是一个 Linux特性,它监控文件系统操作,比如读取、写入和创建。Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多。学习如何将 inotify 集成到您的应用程序中,并发现一组可用来进一步自动化系统治理的命令行工具。(来自百度百科)

  2. 2

    2. rsync介绍

    rsync是类unix系统下的数据镜像备份工具——remote sync。一款快速增量备份工具 Remote Sync,远程同步 支持本地复制,或者与其他SSH、rsync主机同步。(来自百度百科)

  3. 3

    3. rsync和inotify实时同步原理图

  4. 4

    4. 环境部署

    (1)数据库服务器(inotify-master) IP: 192.168.221.131

    (2) 备份服务器(inotify-slave) IP: 192.168.221.136

    END

第二步: 部署备份服务器inotify-slave

  1. 1

    这里是部署inotify-slave环境,配置rsync daemon工作方式

    1. 检查是否安装rsync

    rpm -qa|grep rsync

  2. 2

    2. 添加rsync用户以及模块目录并更改其用户

    useradd rsync -s /sbin/nologin -M#添加rsync用户

    grep rsync /etc/passwd

    mkdir /jhonse/back#创建rsync daemon工作模式的模块目录

    chown rsync.rsync /jhonse/back#更改模块目录的用户

  3. 3

    3. 配置rsync的配置文件/etc/rsyncd.conf

    # /etc/rsyncd: configuration file for rsync daemon mode

    # See rsyncd.conf man page for more options.

    # configuration example:

    # uid = nobody

    # gid = nobody

    # use chroot = yes

    # max connections = 4

    # pid file = /var/run/rsyncd.pid

    # exclude = lost+found/

    # transfer logging = yes

    # timeout = 900

    # ignore nonreadable = yes

    # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

    # [ftp]

    # path = /home/ftp

    # comment = ftp export area

    #工作中指定用户(需要指定用户)

    uid = rsync

    gid = rsync

    #相当于黑洞.出错定位

    use chroot = no

    #有多少个客户端同时传文件

    max connections = 200

    #超时时间

    timeout = 300

    #进程号文件

    pid file = /var/run/rsyncd.pid

    #日志文件

    lock file = /var/run/rsync.lock

    #日志文件

    log file = /var/log/rsyncd.log

    #模块开始

    #这个模块对应的是推送目录

    #模块名称随便起

    [backup]

    #需要同步的目录

    path = /jhonse/back/

    #表示出现错误忽略错误

    ignore errors

    #表示网络权限可写(本地控制真正可写)

    read only = false

    #这里设置IP或让不让同步

    list = false

    #指定允许的网段

    hosts allow = 192.168.221.0/255

    #拒绝链接的地址,以下表示没有拒绝的链接

    hosts deny = 0.0.0.0/32

    #不要动的东西(认情况)

    #虚拟用户

    auth users = rsync_backup

    #虚拟用户的密码文件

    secrets file = /etc/rsync.password

  4. 4

    4.配置虚拟用户的密码文件

    在/etc目录下创建rsync.password文件,并添加虚拟账号和密码

    格式: 账号:密码

    rsync_backup:jhonse

  5. 5

    5.为密码文件rsync.password提权,增加安全性

    chmod 600 /etc/rsync.password

  6. 6

    6.启动rsync服务

    rsync --daemon

    ps -ef |grep rsync

    netstat -lnutp |grep rsync

    END

第三步: 部署数据库服务器(inotify-master)

  1. 1

    说明:inotify是rsync客户端安装和执行的

    1.查看当前系统是否支持inotify

    ll /proc/sys/fs/inotify/

    如果显示max_queued_events、max_user_instances、max_user_watches就证明支持inotify

    /proc/sys/fs/inotify/max_queued_evnets

    表示调用inotify_init时分配给inotify instance中可排队的event的数目的最大值,超出这个值的事件被丢弃,但会触发IN_Q_OVERFLOW事件。

    /proc/sys/fs/inotify/max_user_instances

    表示每一个real user ID可创建的inotify instatnces的数量上限。

    /proc/sys/fs/inotify/max_user_watches

    表示每个inotify instatnces可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小。

    例如: echo 30000000 > /proc/sys/fs/inotify/max_user_watches

  2. 2

    2.下载inotify源码包并编译安装

    wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz #下载inotify源码包

    ll inotify-tools-3.14.tar.gz

    tar -zxf inotify-tools-3.14.tar.gz

    cd inotify-tools-3.14

    ./configure --prefix=/usr/local/inotify-tools-3.14 #配置inotify,并指定安装路径为/usr/local/inotify-3.14

    make && make install

  3. 3

    3.inotify之inotifywait命令常用参数详解

    cd /usr/local/inotify-tools-3.14/

    ./bin/inotifywait --help

    -r|--recursive Watch directories recursively. #递归查询目录

    -q|--quiet Print less (only print events). #打印监控事件的信息

    -m|--monitor Keep listening for events forever. Without this option,inotifywait will exit after one event is received. #始终保持事件监听状态

    --excludei <pattern> Like --exclude but case insensitive. #排除文件或目录时,不区分大小写。

    --timefmt <fmt> strftime-compatible format string for use with %T in --format string. #指定时间输出的格式

    --format <fmt> Print using a specified printf-like format string; read the man page for more details.

    #打印使用指定的输出类似格式字符串

    -e|--event <event1> [ -e|--event <event2> ... ] Listen for specific event(s). If omitted,all events are listened for. #通过此参数可以指定需要监控的事件,如下所示:

    Events:

    access file or directory contents were read #文件或目录被读取。

    modify file or directory contents were written #文件或目录内容修改

    attrib file or directory attributes changed #文件或目录属性被改变。

    close file or directory closed,regardless of read/write mode #文件或目录封闭,无论读/写模式。

    open file or directory opened #文件或目录被打开。

    moved_to file or directory moved to watched directory #文件或目录被移动至另外一个目录。

    move file or directory moved to or from watched directory #文件或目录被移动另一个目录或从另一个目录移动至当前目录。

    create file or directory created within watched directory #文件或目录被创建在当前目录

    delete file or directory deleted within watched directory #文件或目录被删除

    unmount file system containing file or directory unmounted #文件系统被卸载

  4. 4

    4. 参照以上步骤安装rsync

  5. 5

    5. 创建本地监控目录

    mkdir /jhonse

    mkdir /jhonse/back

  6. 6

    6. 创建rsync服务的密码文件

    在/etc目录下创建/etc/rsync.password文件

    这里只要写密码即可,切记。

  7. 7

    7.编写监控脚本并加载到后台执行

    vi inotify.sh

    #!/bin/bash

    #para

    host01=192.168.221.136 #inotify-slave的ip地址

    src=/jhonse/back #本地监控的目录

    dst=backup #inotify-slave的rsync服务的模块名

    user=rsync_backup #inotify-slave的rsync服务的虚拟用户

    rsync_passfile=/etc/rsync.password #本地调用rsync服务的密码文件

    inotify_home=/usr/local/inotify-tools-3.14 #inotify的安装目录

    #judge

    if [ ! -e "$src" ] \

    || [ ! -e "${rsync_passfile}" ] \

    || [ ! -e "${inotify_home}/bin/inotifywait" ] \

    || [ ! -e "/usr/bin/rsync" ];

    then

    echo "Check File and Folder"

    exit 9

    fi

    ${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \

    | while read file

    do

    # rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1

    cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1

    done

    exit 0

    sh inotify.sh &#将脚本加入后台执行

    END

第四步: 测试实时同步

  1. 1. 建议先通过inotify-master测试推送,然后在测试实时同步。

    rsync -avz index.txt rsync_backup@192.168.221.136::backup --password-file=/etc/rsync.password

    如果出现问题可以查看:/var/log/rsyncd.log日志文件

  2. 2. 实时同步

    END

原文地址:http://jingyan.baidu.com/article/656db918ee2f13e380249c4d.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native
centos6.5下postgres-XC集群安装与配置
CentOS 6使用openssl搭建根CA
CentOS6.6中安装VNC server
CentOS下更新Python最新版本