sersync基本概述

当一个目录或文件产生变化,引发一个事件,事件引发一个动作,动作就把数据进行同步传输到远程服务器

为什么使用实时同步

当大公司数据量特别大的时候,一分钟可能传输上万数据量,如果不做实时同步,nfs被数据搞挂了,可能会丢失数据,因为定时任务只能每分钟执行一次,更不可能手动执行推送脚本

保证数据的连续性

减少人力维护成本,解决nfs单点故障

实时同步工具的选择

rsync+inotify-toolsrsync+sersync

Inotify是一个通知接口,用来监控文件系统的各种变化,如果文件存取,删除,移动。可以非常方便地实现文件异动告警,增量备份,并针对目录或文件的变化及时作出响应。rsync+inotify可以实触发式实时同步增量备份

sersync是国人基于rsync+inotify-tools开发的工具,不仅保留了优点同时还强化了实时监控,文件过滤,简化配置等功能,帮助用户提高运行效率,节省时间和网络资源。

sersync项目地址

sersync实战

1.环境准备

角色 外网IP 内网IP 部署服务
web01 10.0.0.7 172.16.1.7 作业上传代码(httpd、php)
nfs服务器 10.0.0.31 172.16.1.31 rsync+inotify-tools
backup服务器 10.0.0.41 172.16.1.41 rsync服务端

注:1)你想把哪台机器的目录进行实时备份,就在哪台机器上装inotify-tools

2)装sersync的服务器一定要装rsync和inotify

2.backup服务器

1)安装rsync

[root@backup ~]# yum install -y rsync

2)配置文件

[root@backup ~]# vim /etc/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = yes
auth users = haoda
secrets file = /etc/rsync.password
log file = /var/log/rsyncd.log
######################################
[data]
path = /data

3)创建系统用户

[root@backup ~]# groupadd www -g 666
[root@backup ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

4)创建虚拟用户的密码文件

[root@backup ~]# echo 'haoda:123456' > /etc/rsync.password
#授权密码文件
[root@backup ~]# chmod 600 /etc/rsync.password

5)创建真实备份目录

[root@backup ~]# mkdir /data
[root@backup ~]# chown -R www.www /data/

6)启动rsync服务

[root@backup ~]# systemctl start rsyncd
[root@backup ~]# netstat -lntp | grep 873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      7804/rsync         
tcp6       0      0 :::873                  :::*                    LISTEN      7804/rsync         
[root@backup ~]#

3.NFS服务器

1)安装NFS

[root@nfs ~]# yum install -y rpcbind nfs-utils

2)配置NFS

[root@nfs ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync,anonuid=666,anongid=666,all_squash)

3)创建用户

[root@nfs ~]# groupadd www -g 666
[root@nfs ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

4)创建目录并授权

[root@nfs ~]# mkdir /data
[root@nfs ~]# chown -R www.www /data/

5)启动服务

[root@nfs ~]# systemctl start rpcbind nfs
#或
[root@nfs ~]# systemctl start nfs.service

6)检查配置

[root@nfs ~]# cat /var/lib/nfs/etab 
/data    172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=666,anongid=666,sec=sys,rw,secure,root_squash,all_squash)

7)安装inotify-tools

[root@nfs ~]#  yum install -y rsync inotify-tools

8)安装sersync

[root@nfs ~]# rz sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs ~]# mv GNU-Linux-x86 /usr/local/sersync

9)配置sersync

[root@nfs ~]# cd /usr/local/
[root@nfs sersync]# vim confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
                #本机ip          本机启动后端口
    <host hostip="localhost" port="8008"></host>
    #检查代码模式
    <debug start="false"/>
    #文件格式
    <fileSystem xfs="true"/>
    #是否开启文件过滤
    <filter start="false">
        <exclude expression="(.*)\.svn"></exclude>
        <exclude expression="(.*)\.gz"></exclude>
        <exclude expression="^info/*"></exclude>
        <exclude expression="^static/*"></exclude>
    </filter>
    #监控的行为 true就是开启
    <inotify>
        <delete start="true"/>
        <createFolder start="true"/>
        <createFile start="true"/>
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="true"/>
        <modify start="true"/>
    </inotify>
     <sersync>
         #要监控的目录
        <localpath watch="/data">
            #远程的服务器IP            模块名字
            <remote ip="172.16.1.41" name="data"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            #rsync执行时候的选项
            <commonParams params="-az"/>
            #是否启动认证 rsync -az /data haoda@172.16.1.41::data
            <auth start="true" users="haoda" passwordfile="/etc/rsync.password"/>
            #当服务端rsync使用的不是873端口时开启
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            #超时时间 100s
            <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-->
        #定时任务,每600分钟全备一次
        <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>

    #-------------------------------------以下为模块内容-------------------------------------
    <plugin name="command">
        <param prefix="/bin/sh" suffix="" ignoreError="true"/>  <!--prefix /opt/tongbu/mmm.sh suffix-->
        <filter start="false">
            <include expression="(.*)\.php"/>
            <include expression="(.*)\.sh"/>
        </filter>
    </plugin>

    <plugin name="socket">
        <localpath watch="/opt/tongbu">
            <deshost ip="192.168.138.20" port="8009"/>
        </localpath>
    </plugin>
    <plugin name="refreshCDN">
        <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
            <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
            <sendurl base="http://pic.xoyo.com/cms"/>
            <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
        </localpath>
    </plugin>
</head>

10)配置密码文件

[root@nfs sersync]# echo '123456' > /etc/rsync.password
[root@nfs sersync]# chmod 600 /etc/rsync.password

11)查看sersync帮助

[root@nfs sersync]# ./sersync2 -h
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
_______________________________________________________
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
________________________________________________________________
[root@nfs sersync]#

12)启动sersync

[root@nfs sersync]# /usr/local/sersync/sersync2 -dr
#或
[root@nfs sersync]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d     run as a daemon
option: -r     rsync all the local files to the remote servers before the sersync work
option: -o     config xml name:  /usr/local/sersync/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost    host port: 8008
will ignore the inotify createFile event 
WARNING XFS FILE SYSTEM WORK
daemon start,sersync run behind the console 
use rsync password-file :
user is    haoda
passwordfile is     /etc/rsync.password
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) 
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data && rsync -az -R --delete ./  --timeout=100 haoda@172.16.1.41::data --password-file=/etc/rsync.password >/dev/null 2>&1 
run the sersync: 
watch path is: /data
[root@nfs sersync]#

4.web01服务器

1)安装httpd和php

[root@web01 ~]# yum install -y httpd php

2)修改httpd用户

[root@web01 ~]# vim /etc/httpd/conf/httpd.conf
User www
Group www

3)创建用户

[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

4)上传作业代码

[root@web01 ~]# rz kaoshi.zip
[root@web01 ~]# unzip kaoshi.zip
[root@web01 html]# vim upload_file.php
$wen="/var/www/html/pic";

5)创建目录

[root@web01 html]# mkdir pic
[root@web01 html]# chown -R www.www /var/www/html/

6)启动服务

[root@web01 html]# systemctl start httpd
[root@web01 html]# ps -ef | grep httpd
root      12092      1  0 11:28 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
www       12093  12092  0 11:28 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
www       12094  12092  0 11:28 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
www       12095  12092  0 11:28 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
www       12096  12092  0 11:28 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
www       12097  12092  0 11:28 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root      12099  10887  0 11:28 pts/2    00:00:00 grep --color=auto httpd
[root@web01 html]#

7)查看挂载点

[root@web01 html]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24
[root@web01 html]#

8)挂载

[root@web01 html]# mount -t nfs 172.16.1.31:/data /var/www/html/pic

5.检验文件挂载与同步

1)web服务器

[root@web01 html]# pwd
/var/www/html
[root@web01 html]# ll pic
total 16
-rw-r--r-- 1 www www 7677 Dec  4 11:35 11_linuxday12.txt
-rw-r--r-- 1 www www 7677 Dec  4 11:35 22_linuxday12.txt
[root@web01 html]#

2)nfs服务器

[root@nfs ~]# cd /data/
[root@nfs data]# ll
total 16
-rw-r--r-- 1 www www 7677 Dec  4 11:35 11_linuxday12.txt
-rw-r--r-- 1 www www 7677 Dec  4 11:35 22_linuxday12.txt
[root@nfs data]#

3)backup服务器

[root@backup ~]# cd /data/
[root@backup data]# ll
total 16
-rw-r--r-- 1 www www 7677 Dec  4 11:35 11_linuxday12.txt
-rw-r--r-- 1 www www 7677 Dec  4 11:35 22_linuxday12.txt
[root@backup data]#
Copyright © 高程程 all right reserved,powered by Gitbook修订于: 2021-05-18 21:14:35

results matching ""

    No results matching ""