CentOS   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Centos 7.0 通过rsync和inotify实现实时同步大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

工具/原料
@H_801_4@
  • centos 7.0
  • rsync
  • inotify

第一步: 准备工作
@H_801_4@
  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
@H_801_4@
  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

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

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

    @L_874_23@
  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

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

    @H_90_65@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)
@H_801_4@
  1. 1

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

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

    ll /proc/sys/fs/inotify/

    如果@L_384_55@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

    @H_90_65@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 #文件或目录被读取。

    @H_90_65@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 #文件或目录被打开。

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

    @H_90_65@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. 创建本地监控目录

    @H_90_65@mkdir /jhonse

    @H_90_65@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

第四步: 测试实时同步
@H_801_4@
  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

大佬总结

以上是大佬教程为你收集整理的Centos 7.0 通过rsync和inotify实现实时同步全部内容,希望文章能够帮你解决Centos 7.0 通过rsync和inotify实现实时同步所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。