rsync+inotify实现数据实时同步

主服务器192.168.222.4   CentOS7
从服务器192.168.222.5   CentOS6
实时同步/home/data1目录到从服务器
从服务器192.168.222.5操作
# yum -y install rsync*
创建rsyncd.conf文件
vim /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  #日志存放位置
[synchronization]  #认证模块名
path = /home/data1   #同步A服务器的文件路径
ignore errors = yes  #忽略无关错误信息
hosts allow = 192.168.222.5   #允许访问IP
hosts deny = * #除了从主机外拒绝所有
read only = no  #允许上传
write only = no #允许下载
list = yes   #允许列出同步目录
auth users = root   #同步的用户
secrets file = /etc/rsync.pass  #存放用户密码的文件
###########可以配置多个同步模块
[download]
path = /home/download
ignore errors = yes
hosts allow = 192.168.222.5
hosts deny = *
read only = no
list = yes
auth users = root
secrets file = /etc/rsync.pass
注:/etc/rsync.pass 文件格式为username:password文件权限必须为600否则服务不正常,hosts allow 定义可为单独IP也可为网段,网段格式为192.168.222.0/24也可为192.168.222.0/255.255.255.0
创建rsync.pass文件
# echo “root:password” >> /etc/rsync.pass
# chmod 600 /etc/rsync.pass
启动服务
# rsync --daemon –v
开机自启动
# echo “rsync --daemon –v” >> /etc/rc.local
主服务器192.168.222.4操作
下载软件包
inotify-tools下载:wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
sersync下载:wget https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz
解压
tar xf inotify-tools-3.14.tar.gz –C /usr/local
tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz–C /usr/local
cd /usr/local
mv inotify-tools-3.14inotify-tools
mv GNU-Linux-x86 sersync
编译安装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="/usr/local/tongbu"><!—与从服务器同步的文件路径-->
           <remote ip="192.168.222.5(从服务器地址)" name="data1"/><!—与从服务器的模块名必须相同-->
       </localpath>
       <rsync>
           <commonParams params="-artuzlpog"/><!—rsync参数设置-->
           <auth start="true"users="root" passwordfile="/usr/local/sersync/user.pass"/>
           <userDefinedPort start="false"port="873"/><!-- port=874 -->
           <timeout start="false" time="100"/><!--timeout=100 -->
           <ssh start="false"/>
       </rsync>
       <failLogpath="/tmp/rsync_fail_log.sh"timeToExecute="60"/><!--default every 60mins execute once-->
       <crontab start="false"schedule="600"><!--600mins-->
           <crontabfilter start="false">
                <excludeexpression="*.php"></exclude>
                <excludeexpression="info/*"></exclude>
           </crontabfilter>
       </crontab>
       <plugin start="false" name="command"/>
   </sersync>

</head>

########如果是多个目录同步需要起两个sersync进程同时需要两个启动配置文件
cp /usr/local/sersync/confxml.xml /usr/local/sersync/downxml.xml
vi /usr/local/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="192.168.222.5(从服务器地址)"name="download"/><!—与从服务器的模块名必须相同-->
       </localpath>
       <rsync>
           <commonParams params="-artuzlpog"/><!—rsync参数设置-->
           <auth start="true"users="root" passwordfile="/usr/local/sersync/user.pass"/>
           <userDefinedPort start="false"port="873"/><!-- port=874 -->
           <timeout start="false" time="100"/><!--timeout=100 -->
           <ssh start="false"/>
       </rsync>
       <failLogpath="/tmp/rsync_fail_log.sh"timeToExecute="60"/><!--default every 60mins execute once-->
       <crontab start="false"schedule="600"><!--600mins-->
            <crontabfilterstart="false">
                <excludeexpression="*.php"></exclude>
                <excludeexpression="info/*"></exclude>
           </crontabfilter>
       </crontab>
       <plugin start="false" name="command"/>
   </sersync>

</head>


配置密码文件
echo “password” > /usr/local/sersync/user.pass
注:文件权限必须为600否则启动异常
chmod 600 /usr/local/sersync/user.pass
2.4服务启动
nohup /usr/local/sersync/sersync2 -r -d -o /usr/local/sersync/confxml.xml > /usr/local/sersync/rsync.log 2>1 
#######如果是多个目录同步需要启动两个进程
nohup /usr/local/sersync/sersync2 -r -d -o /usr/local/sersync/downxml.xml > /usr/local/sersync/rsync.log 2>1 

-d:启用守护进程模式
-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
-n:指定开启守护线程的数量,默认为10个
-o:指定配置文件,默认使用confxml.xml文件
开机启动写入/etc/rc.local文件中

猜你喜欢

转载自blog.csdn.net/qq_35590198/article/details/81356664