zoukankan      html  css  js  c++  java
  • CentOS6.5实现rsync+inotify实时同步

    参考博文:

    参考1:CentOS6.5实现rsync+inotify实时同步  

    参考2:inotify-tools+rsync实时同步文件安装和配置  

    CentOS 6.3下rsync服务器的安装与配置  对 rsync命令解释的很详细

    rsync 常见错误与解决方法整理

    参考1比较详细,但是有点问题,参考2主服务器端比较详细  但是有小问题  把二者结合培正成功

    注意:从主服务器拷贝到从服务器,千万别搞混了。

    1、首先从主服务器A开始

    需要确定你的系统是否支持inotify:

    在安装inotify-tools前请先确认你的linux内核是否达到了2.6.13,并且在编译时开启了CONFIG_INOTIFY选项,也可以通过以下命令检测,如果出现以下输出,说明支持:

    [root@localhost ~]# ls /proc/sys/fs/inotify/
    max_queued_events  max_user_instances  max_user_watches

    下载并安装inotify-tools:

    wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz 
    tar xvf inotify-tools-3.14.tar.gz  
    cd inotify-tools-3.14  
    ./configure  
    make;make install

    接下来需要写两个SH脚本,inotify_init.sh和inotify_monitor.sh:

    inotify_init.sh 用于第一次初始化,也就是运行一次完整的RSYNC同步.

    vim /root/inotify_init.sh
    内容如下:
    #!/bin/sh
    SRC=/主服务器A需要同步的目录/ #记得在最后面加/不然RYNC会自动增加一层目录
      
    DES=backup
    IP=从服务器B的IP
    USER=rsync
    #DST=/etc/rsyncd 远程rsync模块下的目录
    INWT=/usr/bin/inotifywait    #注意路径 我的路径为:/usr/local/bin/inotifywait
    RSYNC=/usr/bin/rsync $RSYNC -zahqt --password-file=/root/rsync.pwd $SRC $USER@$IP::$DES

    保存退出.

    设置可执行权限:

    chmod +x /root/inotify_init.sh

    接下是inotify_monitor.sh,用于订阅文件修改事件.注意,因为特别原因,我在这里做的是增量备份+实时同步,也就是说,当主服务器A上的图片被删除是,从服务器B是不会删除图片的.

    vi /root/inotify_monitor.sh

    内容如下:

    #!/bin/bash
      
    ###########################
    sync[0]='/主服务器需要同步的目录/,从服务器B的IP,backup,rsync' # localdir,host,rsync_module,auth_user
      
    INWT=/usr/bin/inotifywait  #注意路径 我的路径为:/usr/local/bin/inotifywait
    RSYNC=/usr/bin/rsync
    PASS=/root/rsync.pwd
    ###########################

    for item in ${sync[@]}; do

    dir=`echo $item | awk -F"," '{print $1}'`
    host=`echo $item | awk -F"," '{print $2}'`
    module=`echo $item | awk -F"," '{print $3}'`
    user=`echo $item | awk -F"," '{print $4}'`

    $INWT -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e'
    --event CLOSE_WRITE,create,move $dir | while read date time file event
    do
    #echo $event'-'$file
    case $event in
    MODIFY|CREATE|MOVE|MODIFY,ISDIR|CREATE,ISDIR|MODIFY,ISDIR)
    if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then
    cmd="$RSYNC -zahqzt --exclude='*' --password-file=$PASS
    --include=$file $dir $user@$host::$module "
    echo $cmd >> /var/log/rsyncd.log   #写入日志文件
    $cmd
    fi
    ;;

    MOVED_FROM|MOVED_FROM,ISDIR|DELETE,ISDIR)
    if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then
    cmd="$RSYNC -zahqzt --password-file=$PASS --exclude=$file
    $dir $user@$host::$module "
    echo $cmd >> /var/log/rsyncd.log
    $cmd
    fi
    ;;
    esac
    done &
    done

    加 执行权限:
    chmod +x /root/inotify_monitor.sh

    设置RSYNC自动登录验证密码,认证文件只用加入密码即可

    vi /root/rsync.pwd
    xxxxxx

    保存,退出

    设置只有ROOT才可以查看的权限.

    chmod 600 /root/rsync.pwd
     

    2、以下是备从务器B的配置:

    yum install rsync -y    #安装rsync服务

    配置RSNYD服务:

    vi /etc/rsyncd.conf

    内容如下,需要把Apache修改成你运行网站的用户名,我的是因为原来使用apache,虽然现在用Nginx,也一直没改用户名:
    uid = root
    gid = root
    use chroot = no
    max connections = 0   #没有连接限制
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    log file = /var/log/rsyncd.log
      
    [backup]
    path = /从服务器B本地用于存放备份的目录
    ignore errors
    read only = no
    list = false
    hosts allow = 主服务器A的IP
    auth users = rsync
    secrets file = /etc/rsync.pwd

    设置密码文件:

    vim /etc/rsync.pwd    #添加以下内容
    rsync:123456
    chmod 600 /etc/rsync.pwd    #修改密码文件权限为600

    注:当配置文件中参数strict modes为true时,rsync认证口令文件的权限一定是600,否则客户端将不能连接服务器。rsync认证口令文件中每一行指定一个 用户名:口令 对,格式为:username:passwd。

    启动RSYNCD

    rsync --daemon

    添加开机自动启动服务:

    添加开机自动启动服务:

     vi /etc/rc.local

    添加以下内容:

    rsync --daemon

    3、主服务器开机启动

    vi /etc/rc.local
    添加以下内容,实时开机自动同步:
    /root/inotify_init.sh
    /root/inotify_monitor.sh

    保存退出

    运行

    /root/inotify_init.sh
    /root/inotify_monitor.sh
     
  • 相关阅读:
    DataAnnotations
    使用BizTalk实现RosettaNet B2B So Easy
    biztalk rosettanet 自定义 pip code
    Debatching(Splitting) XML Message in Orchestration using DefaultPipeline
    Modifying namespace in XML document programmatically
    IIS各个版本中你需要知道的那些事儿
    关于IHttpModule的相关知识总结
    开发设计的一些思想总结
    《ASP.NET SignalR系列》第五课 在MVC中使用SignalR
    《ASP.NET SignalR系列》第四课 SignalR自托管(不用IIS)
  • 原文地址:https://www.cnblogs.com/wuling129/p/5007520.html
Copyright © 2011-2022 走看看