zoukankan      html  css  js  c++  java
  • 服务器间文件实时双向同步(rsync+inotify)

      场景: 主从服务器项目文件实时双向同步(参考:http://www.xcx1024.com/ArtInfo/3026389.html)

    .rsync安装

      yum在线:
    
            yum install -y rsync
    
     
      离线:
    
            cd /usr/local
    
            wget https://download.samba.org/pub/rsync/src/rsync-3.1.2.tar.gz
    
            tar -zxvf rsync-3.1.2.tar.gz
    
            cd rsync-3.1.2
    
            ./configure --prefix=/usr/local/rsync
    
            make
    
            make install

    . 创建rsync配置文件

       yum在线:
    
            vi /etc/rsyncd.conf
    
     
    
       离线:
    
            vi /usr/local/rsync/rsyncd.conf

      配置文件内容:

            #rsync通用配置文件,配置的注释不要写在配置后面,否则会有问题
    
            uid = root
    
            gid = root
    
            use chroot = 0
    
            port = 873
    
            #允许ip访问设置,请根据实际需要进行配置,这里为了方便设为全网段 *
    
      # hosts allow = 192.168.0.1/255.255.255.0 198.162.145.1 10.0.1.0/255.255.255.0
    
            hosts allow = *
    
            max connections = 0
    
            timeout = 300
    
            pid file = /var/run/rsyncd.pid
    
            lock file = /var/run/rsyncd.lock
    
            log file = /var/log/rsyncd.log
    
            log format = %t %a %m %f %b
    
            transfer logging = yes
    
            syslog facility = local3
    
            #方括号中为模块声明,对应命名,这里master_web对应了主web机配置
    
            [master_web]
    
            #指定当前模块在rsync服务器上的同步路径,该参数是必须指定的
    
            path = /app/web/data-share/static/
    
            #注释,可以同模块名一样,从服务器可都为slave_web
    
            comment = master_web
    
            ignore errors
    
            #是否允许客户端上传文件
    
            read only = no
    
            list = no
    
            #指定由空格或逗号分隔的用户名列表,只有这些用户才允许连接该模块
    
            auth users = rsync
    
            #保存密码和用户名文件,需要自己生成
    
            secrets file = /etc/rsyncd.passwd

    . 创建用户名密码文件

         yum在线:
    
            vi /etc/rsyncd.passwd
    
            vi /root/rsyncd.passwd
    
     
    
         离线:
    
            vi /usr/local/rsync/rsyncd.passwd
    
            vi /root/rsyncd.passwd

     

            文件内容:

           

     #/etc/rsyncd.passwd中内容格式 用户名:密码 ,两台服务器设置相同的用户名密码 rsync:123456
    
    #/root/rsyncd.passwd中内容只需要填写从服务器的密码,两台服务器设置相同的密码 123456

    .给文件赋权限

       Yum在线:
    
        chmod 600 /etc/rsyncd.passwd
    
        chmod 600 /root/rsyncd.passwd
    
     
    
       离线:
    
        chmod 600 /usr/local/rsync/rsyncd.passwd
    
        chmod 600 /root/rsyncd.passwd

     

    .以守护进程方式启动rsync服务

      Yum在线:
    
        /usr/bin/rsync --daemon --config=/etc/rsyncd.conf
    
     
    
       离线:
    
        /usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsyncd.conf

     

    .添加开启自启动

       yum在线:
    
        echo "/usr/bin/rsync --daemon --config=/etc/rsyncd.conf">>/etc/rc.local
    
     
    
        离线:
    
    echo "/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsyncd.conf">>/etc/rc.local

     

     

    . 验证是否可以手动同步

     # master_web 为从web服务器的 rsyncd.conf 中配置的模块名,

      # rsync 为从web服务器的中 rsyncd.passwd 中配置的用户名

     

        78服务器给79服务器同步:

        

    rsync -vzrtopg --delete --progress /usr/local/data-share/static/ rsync@xx.xx.xx.79::master_web --password-file=/root/rsyncd.passwd

     

        定时任务:

       

     echo '*/1 * * * * rsync -vzrtopg --delete --progress /usr/local/data-share/static/ rsync@xx.xx.xx.79::master_web --    password-file=/root/rsyncd.passwd > /dev/null 2>&1 &' >> /var/spool/cron/root

     

        79服务器给78服务器同步:

        

    rsync -vzrtopg --delete --progress /usr/local/data-share/static/ rsync@xx.xx.xx.78::master_web --password-file=/root/rsyncd.passwd

        定时任务:

        

    echo '*/1 * * * * rsync -vzrtopg --delete --progress /usr/local/data-share/static/ rsync@xx.xx.xx.78::master_web --password-file=/root/rsyncd.passwd > /dev/null 2>&1 &' >> /var/spool/cron/root

     

     

    .安装inotify

    wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz --no-check-certificate
    
        tar -zxvf inotify-tools-3.14.tar.gz
    
        makedir /usr/local/inotify
    
        cd inotify-tools-3.14.tar.gz
    
        ./configure --prefix=/usr/local/inotify
    
        make && make install

     

        # 查看是否安装成功

      

      ls -alh /usr/local/inotify/bin/inotify*

     

        # 建立软连接

     

       ln -s /usr/local/inotify/bin/inotifywait /usr/bin/inotifywait
    
        ln -s /usr/local/inotify/bin/inotifywatch /usr/bin/inotifywatch

     

     

    .创建并配置inotify_back.sh

     

    vi /usr/local/inotify/inotify_back.sh

     

        配置文件内容:

     

        78服务器:

        #!/bin/bash
    
        src=/app/web/data-share/static/
    
        /usr/local/inotify/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
    
        rsync -vzrtopg --delete --progress /usr/local/data-share/static/ rsync@172.20.14.79::master_web --password-file=/root/rsyncd.passwd
    
        echo "${file} was rsynced" >> /tmp/rsync.log 2>&1
    
        done

     

        79服务器:

        #!/bin/bash
    
        src=/app/web/data-share/static/
    
        /usr/local/inotify/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
    
        rsync -vzrtopg --delete --progress /usr/local/data-share/static/ rsync@172.20.14.78::master_web --password-file=/root/rsyncd.passwd
    
        echo "${file} was rsynced" >> /tmp/rsync.log 2>&1
    
        done

     

     

    .inotify_back.sh脚本赋予可执行权限

    chmod +x inotify_back.sh

    十一.运行inotify_back.sh同步监控脚本和配置守护进程

       nohup ./inotify_back.sh &
    
        #建立守护进程运行inotify_back.sh脚本
    
        echo "nohup ./inotify_back.sh &" >> /etc/rc.local

     

     

    注意点:

    开放873端口

    创建同步用户(useradd rsync , 并配置密码passwd rsync)

  • 相关阅读:
    1046 Shortest Distance (20 分)(模拟)
    1004. Counting Leaves (30)PAT甲级真题(bfs,dfs,树的遍历,层序遍历)
    1041 Be Unique (20 分)(hash散列)
    1036 Boys vs Girls (25 分)(查找元素)
    1035 Password (20 分)(字符串处理)
    1044 Shopping in Mars (25 分)(二分查找)
    onenote使用小Tip总结^_^(不断更新中...)
    1048 Find Coins (25 分)(hash)
    三个故事
    领导者的举止
  • 原文地址:https://www.cnblogs.com/xingxia/p/linux_dir_sync_by_rsync_inotify.html
Copyright © 2011-2022 走看看