星空网 > 软件开发 > 操作系统

通过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 conf

vi conf

<?

<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/conf

vi /opt/sersync/down

<?

<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/conf

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

nohup  /opt/sersync/sersync2  -r -d -o /opt/sersync/down

 

 

-d:启用守护进程模式
-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
-n:指定开启守护线程的数量,默认为10个
-o:指定配置文件,默认使用conf
开机启动写入/etc/rc.local文件中
 
验证
在主服务器上的/opt/tongbu及/home目录下创建文件查看从服务器172.26.7.51上是否有同步



原标题:通过rsync+inotify实现数据实时备份同步

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

冷链物流成为疫苗存储、分发和接种的新难题?:https://www.ikjzd.com/articles/137082
台湾站点大件物流渠道卖家发货流程变更:https://www.ikjzd.com/articles/137083
加州调查亚马逊是否遵守控制新冠疫情各项规定:https://www.ikjzd.com/articles/137084
亚马逊子公司推出无人驾驶电动车:https://www.ikjzd.com/articles/137085
Lazada什么产品好卖?Lazada最新选品类目推荐!:https://www.ikjzd.com/articles/137086
速卖通:对虚拟类商品开展专项治理:https://www.ikjzd.com/articles/137087
斥资210万美元,TikTok游说美国参议院阻止法案:https://www.goluckyvip.com/news/188213.html
大家推荐一下厦门三整天一个半天的旅游攻略:https://www.vstour.cn/a/365181.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流