zoukankan      html  css  js  c++  java
  • 通过rsync+inotify实现数据实时备份同步

    一.环境描述

     测试环境

    需求:服务器A与服务器B为主备服务模式,需要保持文件一致性,现采用sersync基于rsync+inotify实现数据实时同步

    环境描述:

    主服务器172.26.7.50 ,从服务器172.26.7.51

    实时同步/home/ 及/download 目录到从服务器

    二.实施方法

    1.从服务器172.26.7.51 rsync服务搭建

    1.1下载软件包至从服务器

    下载地址:http://rsync.samba.org/ftp/rsync/src

    可根据环境需求下载相应的软件版本,本实验下载版本为rsync-3.1.1.tar.gz

    1.2安装软件包

    cd /usr/src

    wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz
    解压
    tar xf rsync-3.1.1.tar.gz –C /opt/
    mv /opt/rsync-3.1.1 /opt/rsync
    cd /opt/rsync
    ./configure
    make && make install
    创建rsyncd.conf文件
    vi /etc/rsyncd.conf
     
    uid = root    #以root用户运行rsync服务
    gid = root    #以root用户运行rsync服务
    use chroot = no  #增加对目录文件软连接的备份
    max connections = 1200  #最大连接数
    timeout = 800    #超时时间
    pid file = /var/run/rsyncd.pid   #PID存放位置
    lockfile = /var/run/rsyncd.lock  #锁文件存放位置
    log file = /var/log/rsyncd.log  #日志存放位置
    [tongbu]  #认证模块名
    path = /opt/tongbu   #同步A服务器的文件路径
    ignore errors = yes  #忽略无关错误信息
    hosts allow = 172.26.7.50   #允许访问IP
    hosts deny = * #除了172.26.7.50主机外拒绝所有
    read only = no #允许上传 
    write only = no #允许下载
    list = yes   #允许列出同步目录
    auth users = root   #同步的用户
    secrets file = /etc/rsync.pass  #存放用户密码的文件
    ###########可以配置多个同步模块
    [download]
    path = /download
    ignore errors = yes
    hosts allow = 172.26.7.50
    hosts deny = *
    read only = no
    list = yes
    auth users = root
    secrets file = /etc/rsync.pass
    注:/etc/rsync.pass 文件格式为username:password
    文件权限必须为600否则服务不正常
     
    hosts allow 定义可为单独IP也可为网段,网段格式为172.26.7.0/24
    也可为172.26.7.0/255.255.255.0
    创建rsync.pass文件
    echo “root:password”>>/etc/rsync.pass
    chmod 600 /etc/rsync.pass

    1.3启动服务

    rsync --daemon –v

    echo “rsync --daemon –v”>>/etc/rc.local

    开机自启动

    2.主服务器172.26.7.50配置

    2.1下载软件包

    所需软件包为:

    rsync-3.1.1.tar.gz    inotify-tools-3.14.tar.gz   sersync2.5.4_64bit_binary_stable_final.tar.gz

    rysnc下载地址:wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz
    inotify-tools下载:wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

    sersync下载:https://code.google.com/p/sersync/downloads

    2.2软件安装

    软件下载好之后解压安装

    cd /usr/src

    wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz
    wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
    wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz

    解压

    tar xf rsync-3.1.1.tar.gz –C /opt

    tar xf inotify-tools-3.14.tar.gz –C /opt

    tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz –C /opt

    cd /opt

    mv rsync-3.1.1 rsync

    mv inotify-tools-3.14 inotify-tools

    mv GNU-Linux-x86 sersync
     
    编译安装rsync
    cd rsync
    ./configure && make && make install 
    编译安装inotify-tools
    cd inotify-tools
    ./configure && make && make install
     

    2.3服务配置

    配置sersync

    cd sersync

    cp confxml.xml confxml.xml_bak

    vi confxml.xml

    <?xml version="1.0" encoding="ISO-8859-1"?>

    <head version="2.5">

        <host hostip="localhost" port="8008"></host>

        <debug start="false"/>

        <fileSystem xfs="false"/>

        <filter start="false">

            <exclude expression="(.*).svn"></exclude>

            <exclude expression="(.*).gz"></exclude>

            <exclude expression="^info/*"></exclude>

            <exclude expression="^static/*"></exclude>

        </filter>

        <inotify>

            <delete start="true"/>

            <createFolder start="true"/>

            <createFile start="false"/>

            <closeWrite start="true"/>

            <moveFrom start="true"/>

            <moveTo start="true"/>

            <attrib start="false"/>

            <modify start="false"/>

        </inotify>

        <sersync>

            <localpath watch="/opt/tongbu"><!—与从服务器同步的文件路径-->

                <remote ip="172.26.7.51" name="tongbu"/><!—与从服务器的模块名必须相同-->

            </localpath>

            <rsync>

                <commonParams params="-artuzlpog"/><!—rsync参数设置-->

                <auth start="true" users="root" passwordfile="/opt/sersync/user.pass"/>

                <userDefinedPort start="false" port="873"/><!-- port=874 -->

                <timeout start="false" time="100"/><!-- timeout=100 -->

                <ssh start="false"/>

            </rsync>

            <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->

            <crontab start="false" schedule="600"><!--600mins-->

                <crontabfilter start="false">

                    <exclude expression="*.php"></exclude>

                    <exclude expression="info/*"></exclude>

                </crontabfilter>

            </crontab>

            <plugin start="false" name="command"/>

        </sersync>

    </head>

    ########如果是多个目录同步需要起两个sersync进程同时需要两个启动配置文件

    cp /opt/sersync/confxml.xml /opt/sersync/downxml.xml

    vi /opt/sersync/downxml.xml

    <?xml version="1.0" encoding="ISO-8859-1"?>

    <head version="2.5">

        <host hostip="localhost" port="8008"></host>

        <debug start="false"/>

        <fileSystem xfs="false"/>

        <filter start="false">

            <exclude expression="(.*).svn"></exclude>

            <exclude expression="(.*).gz"></exclude>

            <exclude expression="^info/*"></exclude>

            <exclude expression="^static/*"></exclude>

        </filter>

        <inotify>

            <delete start="true"/>

            <createFolder start="true"/>

            <createFile start="false"/>

            <closeWrite start="true"/>

            <moveFrom start="true"/>

            <moveTo start="true"/>

            <attrib start="false"/>

            <modify start="false"/>

        </inotify>

        <sersync>

            <localpath watch="/download"><!—与从服务器同步的文件路径-->

                <remote ip="172.26.7.51" name="download"/><!—与从服务器的模块名必须相同-->

            </localpath>

            <rsync>

                <commonParams params="-artuzlpog"/><!—rsync参数设置-->

                <auth start="true" users="root" passwordfile="/opt/sersync/user.pass"/>

                <userDefinedPort start="false" port="873"/><!-- port=874 -->

                <timeout start="false" time="100"/><!-- timeout=100 -->

                <ssh start="false"/>

            </rsync>

            <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->

            <crontab start="false" schedule="600"><!--600mins-->

                <crontabfilter start="false">

                    <exclude expression="*.php"></exclude>

                    <exclude expression="info/*"></exclude>

                </crontabfilter>

            </crontab>

            <plugin start="false" name="command"/>

        </sersync>

    </head>

    配置密码文件

    echo “password”>>/opt/sersync/user.pass

    注:文件权限必须为600否则启动异常

    2.4服务启动

    nohup  /opt/sersync/sersync2  -r -d -o /opt/sersync/confxml.xml >/opt/sersync/rsync.log  2>&1 &

    #######如果是多个目录同步需要启动两个进程

    nohup  /opt/sersync/sersync2  -r -d -o /opt/sersync/downxml.xml >/opt/sersync/downrsync.log  2>&1 &

    -d:启用守护进程模式
    -r:在监控前,将监控目录与远程主机用rsync命令推送一遍
    -n:指定开启守护线程的数量,默认为10个
    -o:指定配置文件,默认使用confxml.xml文件
    开机启动写入/etc/rc.local文件中
     
    验证
    在主服务器上的/opt/tongbu及/home目录下创建文件查看从服务器172.26.7.51上是否有同步
  • 相关阅读:
    【leetcode】Binary Search Tree Iterator
    【leetcode】Palindrome Partitioning II
    【leetcode】Best Time to Buy and Sell Stock III
    【leetcode】Best Time to Buy and Sell Stock II
    【leetcode】Longest Consecutive Sequence
    【leetcode】Factorial Trailing Zeroes
    【leetcode】Simplify Path
    【leetcode】Generate Parentheses
    【leetcode】Combination Sum II
    【leetcode】Combination Sum
  • 原文地址:https://www.cnblogs.com/jl-bai/p/5828088.html
Copyright © 2011-2022 走看看