centos7系统安装sersync+rsync实现服务器同步功能

简介:

一、为什么要用sersync+rsync架构?


1、sersync是基于inotify开发的,类似于inotify-tools的工具。


2、sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或者某一个目录的名字,然


后使用rsync同步的时候,只同步发生变化的文件或者目录。

 

二、rsync+inotify-tools与sersync+rsync架构的区别?


1、rsync+inotify-tools


     a、inotify只能记录下被监听的目录发生了变化(增,删,改)并没有把具体是哪个文件或者哪个目录发生了变化记录下来;

   b、rsync在同步的时候,并不知道具体是哪个文件或目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此效率很低。

    

2、sersync+rsync


     a、sersync可以记录被监听目录中发生变化的(增,删,改)具体某个文件或目录的名字;


     b、rsync在同步时,只同步发生变化的文件或目录(每次发生变化的数据相对整个同步目录数据来说很小,rsync在


遍历查找对比文件时,速度很快),因此效率很高。

 

总结: 

       1)、当同步的目录数据量不大时,建议使用rsync+inotify 


       2)、当同步的目录数据量很大时(几百G甚至1T以上)文件很多时,建议使用sersync+rsync


三、sersync+rsync架构图

wKiom1jocE3xWzbtAAD_-sJijpU267.png-wh_50

原理步骤:

1)在同步服务器(Master)上开启sersync服务,sersync负责监控配置路径中的文件系统事件变化(增删改);


2)调用rsync命令把更新的文件同步到目标服务器(S1 和 S2);


3)需要在主服务器配置sersync,在同步目标服务器配置rsync 。


       通过rsync的守护进程服务后可以发现,实际上sersync就是监控本地的数据写入或更新事件;然后,在调用rsync客


户端的命令,将写入或更新事件对应的文件通过rsync推送到目标服务器(S1 和S2)上相应的目录文件中。

四、Sersync介绍

        sersync主要用于服务器同步,web镜像等功能。基于boost1.43.0,inotify api,rsync command.开发。目前使


用的比较多的同步解决方案是inotify-tools+rsync ,另外一个是google开源项目Openduckbill(依赖于inotify- 


tools),这两个都是基于脚本语言编写的。相比较上面两个项目,本项目优点是:


        1)sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤,所以在


结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。


        2)相比较上面两个项目,sersync配置起来很简单,其中bin目录下已经有基本上静态编译的2进制文件,配


bin目录下的xml配置文件直接使用即可。


        3)另外本项目相比较其他脚本开源项目,使用多线程进行同步,尤其在同步较大文件时,能够保证多个服


务器实时保持同步状态。


    4)本项目有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则每10个小时对同步失


败的文件重新同步。


     5)本项目自带crontab功能,只需在xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次。


无需再额外配置crontab功能。


        6)本项目socket与http插件扩展,满足您二次开发的需要


五、Sersync安装配置


5.1安装环境准备



角色
系统环境 IP 名称
sersync服务器 CentOS 7.3 x86_64 192.168.10.20 Master主服务器
rsync服务器 CentOS 7.3 x86_64 192.168.10.150 S1目标服务器
rsync服务器 CentOS 7.3 x86_64 192.168.10.151 S2目标服务器


5.2 配置rsync目标服务器


1. 关闭SELINUX

 [root@S1 ~]# vi /etc/selinux/config               #编辑防火墙配置文件


# This file controls the state of SELinux on the system.

# SELINUX= can take one of these three values:

#     enforcing - SELinux security policy is enforced.

#     permissive - SELinux prints warnings instead of enforcing.

#     disabled - No SELinux policy is loaded.

SELINUX=disabled                         #编辑 SELINUX=disabled,重启后成功     

# SELINUXTYPE= can take one of three two values:

#     targeted - Targeted processes are protected,

#     minimum - Modification of targeted policy. Only selected processes are protected. 

#     mls - Multi Level Security protection.

SELINUXTYPE=targeted 

[root@S1 ~]# setenforce 0                   #立即生效


2. 关闭防火墙IPTABLES


[root@S1 ~]#  iptables -F


[root@S1 ~]#  iptables -L

Chain INPUT (policy ACCEPT)

target     prot opt source               destination         


Chain FORWARD (policy ACCEPT)

target     prot opt source               destination         


Chain OUTPUT (policy ACCEPT)

target     prot opt source               destination 


3. 部署rsync服务


确定S1和S2目标服务器版本是最新的,配置如下:

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


[root@S1 ~]#  vim /etc/rsyncd.conf

# Rsync configuration:

uid = root                             #设置rsync运行权限为root

gid = root                             #设置rsync运行权限为root

use chroot = no                        # 安全相关,默认为true,修改为no,增加对目录文件软连接的备份

port = 873                             # 指定rsync服务的默认端口号

max connections = 2000                 # 并发连接数

timeout = 200                          # 超时时间(秒)

log file = /var/run/rsyncd.log         # 指定日志文件位置,启动rsync后自动产生这个文件,无需提前创建

pid file = /var/run/rsyncd.pid         # 指定rsync的pid目录

lock file = /var/run/rsyncd.lock       # 指定rsync的锁文件【重要】,支持max connections参数的锁文件

log format = %t %a %m %f %b

#################################################

[rsync]                                # 模块

path = /opt/rsync/                     #rsync服务端数据目录路径

commemt = rsync

list = yes

read only = no

#read only = no                        # no客户端可上传文件,yes只读

#write only = no                       # no客户端可下载文件,yes不能下载

auth users = test                      #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开

secrets file = /etc/rsyncd.secret      #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件

ignore errors = yes

hosts allow = 192.168.10.20            #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开

hosts deny = 192.168.10.254            #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开

特别提示: S1,S2的机器同时部署上述服务;上面rsync服务的配置文件,表面允许sersync主服务器(ip:192.168.10.20)访问rsync同步模块名为[rsync],将同步过来的文件放入对应的path指定的目录/opt/rsync/下面; 如果有多台目标服务器,则每一台都需要进行类似的rsync服务配置,上面的uid、gid要换成您服务器的相应的同步用户;注意,rsync服务账户(本文用的是root)要有对被同步目录(/data/)的写入更新权限。


4. 创建rsync同步密码文件,并设置权限为600

[root@S1 ~]# echo "test:test123" >  /etc/rsyncd.secret


[root@S1 ~]# chmod 600  /etc/rsyncd.secret  


[root@S1 ~]# ll /etc/rsync.password    


-rw-------. 1 root root 13 4月   8 10:14 /etc/rsyncd.secret


[root@Master ~]#  cat  /etc/rsyncd.secret


test:test123


5. 启动rsync守护进程,并写入开机自启动

[root@S1 ~]# rsync --daemon


[root@S1 ~]#  ps -ef | grep rsync

root      5735     1  0 14:45 ?        00:00:00 rsync --daemon

root      5737  5216  0 14:45 pts/3    00:00:00 grep --color=auto rsync

[root@S1 ~]# netstat -nulpt| grep rsync


tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      5588/rsync          

tcp6       0      0 :::873                  :::*                    LISTEN      5588/rsync


设置开机自启动【写入到/etc/rc.local里面】

[root@S1 ~]# vim /etc/rc.local


# rsync server progress


/usr/bin/rsync --daemon --config=/etc/rsyncd.conf


6. 创建相关待同步的目录/opt/rsync/

[root@S1 ~]#  mkdir -p /opt/rsync/


[root@S1 ~]#  tree /opt


提示: 此步骤在S1,S2都要执行,否则rsync服务会因为没有PATH路径而无法启动。


5.3 配置sersync主服务器


1. 关闭SELINUX,关闭防火墙IPTABLES

2. 安装rsync服务器并配置rsync客户端相关权限认证

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

[root@Master ~]# echo "test123" >  /etc/rsyncd.secret


[root@Master ~]# chmod 600  /etc/rsyncd.secret  


-rw-------. 1 root root 13 4月   8 10:14 /etc/rsyncd.secret

[root@Master ~]# ll /etc/rsyncd.secret 


test123


注意:Master主服务器的密码文件可以和目标服务器的密码文件名称不一样,但是在xml文件中指定的密码文件必须是

Master主服务器的密码文件。


3. Master主服务器上手工测试rsync同步情况

特别提示:此步非常关键,如果测试不成功,后面的sersync配好了也不会同步数据。

1)在Master上创建待同步数据目录/opt/tongbu/及数据

[root@Master ~]#  mkdir -p /opt/tongbu/


[root@Master ~]#  touch  /opt/tongbu/test{1,2,3}{a,b,c}


[root@Master ~]#  ls  /opt/tongbu/


test1a  test1b  test1c  test2a  test2b  test2c  test3a  test3b  test3c


2)在Master上执行同步命令

[root@Master ~]#  rsync -avzP /opt/tongbu/ test@192.168.10.150::rsync --password-file=/etc/rsyncd.secret

[root@Master ~]#  rsync -avzP /opt/tongbu/ test@192.168.10.151::rsync --password-file=/etc/rsyncd.secret


提示:在后面进行部署sersync之前,sersync主服务器上必须要确保手工可以把文件推送到S1,S2上,这样后续sersync才能调用这些命令来自动推送。


3)在Master上执行推送命令后,在S1,S2上查看同步目录/opt/rsync/中的内容,如果内容同步完成,进行sersync的配置;未完成,检查rsync配置及认证用户密码信息。


4. Mster上开始部署sersync服务

1)下载sersync

[root@Master ~]#  wget -c https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz


[root@Master ~]# tar -zxf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/


[root@Master ~]# mv GNU-Linux-x86 /usr/local/sersync


2) 配置sersync

[root@Master local]# cp sersync/confxml.xml   sersync/confxml.xml.$(date +%F)

[root@Master local]# ll sersync/confxml.xml


-rwxr-xr-x. 1 root root    2215 Apr  8 19:07 confxml.xml


[root@Master local]#  ll sersync/confxml.xml*


-rwxr-xr-x. 1 root root    2215 Apr  8 19:07 confxml.xml

-rwxr-xr-x. 1 root root    2215 Apr  8 23:19 confxml.xml.2017-04-08


3)修改配置文件confxml.xml

修改涂色部分

[root@Master local]#  vim  sersync/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="true"/>

<closeWrite start="true"/>

<moveFrom start="true"/>

<moveTo start="true"/>

<attrib start="true"/>

<modify start="true"/>

    </inotify>


    <sersync>

<localpath watch="/opt/tongbu">

   <remote ip="192.168.10.150" name="rsync"/>

   <!--<remote ip="192.168.8.39" name="tongbu"/>-->

   <!--<remote ip="192.168.8.40" name="tongbu"/>-->

</localpath>

<rsync>

   <commonParams params="-artuz"/>

   <auth start="true" users="test" passwordfile="/etc/rsyncd.secret"/>

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

            <timeout start="true" time="300"/><!-- timeout=100 -->

   <ssh start="false"/>

</rsync>

<failLog path="/usr/local/sersync/logs/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>


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


5. 开启sersync守护进程同步数据,启动命令

[root@Master ~]# /usr/local/sersync/sersync2  -d -r -o /usr/local/sersync/confxml.xml


配置sersync环境变量


[root@Master ~]# echo"PATH=$PATH:/usr/local/sersync/">>/etc/profile


[root@Master ~]# source /etc/profile


[root@Master ~]# sersync2 -d -r -o /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: -r rsync all the local files to the remote servers before the sersync work

option: -d run as a daemon

daemon thread num: 10

parse xml config file

host ip : localhosthost port: 8008

will ignore the inotify createFile event 

daemon start,sersync run behind the console 

use rsync password-file :

user istest

passwordfile is /etc/rsyncd.secret

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 /opt/tongbu && rsync -artuz -R --delete ./  --port=873  --timeout=300 test@192.168.10.150::rsync --password-file=/etc/rsyncd.secret >/dev/null 2>&1 

run the sersync: 

watch path is: /opt/tongbu

      


    以上就是我实现的Sersync服务器数据同步配置,大家可以进行相互学习借鉴······








      本文转自MQ_douer 51CTO博客,原文链接:http://blog.51cto.com/douer/1914098,如需转载请自行联系原作者


相关文章
|
25天前
|
定位技术
GPS北斗卫星同步时钟(时间同步服务器)建设施工部署方案
GPS北斗卫星同步时钟(时间同步服务器)建设施工部署方案
GPS北斗卫星同步时钟(时间同步服务器)建设施工部署方案
|
2月前
|
缓存 关系型数据库 MySQL
百度搜索:蓝易云【CentOS8服务器安装MySQL报错:no match mysql-community-server】
现在,你已经成功安装了MySQL服务器并解决了"no match mysql-community-server"的报错问题。祝你使用愉快!
42 1
|
9天前
|
网络协议 Linux 网络安全
centos7下最简单的 unison实现文件双向同步图文详解
centos7下最简单的 unison实现文件双向同步图文详解
11 0
|
1月前
|
Oracle 关系型数据库 Linux
服务器Centos7 静默安装Oracle Database 12.2
服务器Centos7 静默安装Oracle Database 12.2
71 0
|
1月前
|
消息中间件 关系型数据库 MySQL
Flink CDC产品常见问题之把flink cdc同步的数据写入到目标服务器失败如何解决
Flink CDC(Change Data Capture)是一个基于Apache Flink的实时数据变更捕获库,用于实现数据库的实时同步和变更流的处理;在本汇总中,我们组织了关于Flink CDC产品在实践中用户经常提出的问题及其解答,目的是辅助用户更好地理解和应用这一技术,优化实时数据处理流程。
|
1月前
|
存储 数据挖掘
服务器数据恢复—raid5热备盘同步失败的数据恢复案例
一台存储上有一组由多块硬盘组建的raid5阵列,该raid5阵列中的一块硬盘掉线,热备盘自动上线同步数据的过程中,raid阵列中又有一块硬盘掉线,热备盘的数据同步被中断,raid5阵列失效,卷挂载不上,存储瘫痪。 这类raid故障比较常见,服务器raid中的硬盘大多数情况下都是一个批次的同品牌同型号的硬盘,一旦有硬盘出现故障掉线,那么其他硬盘也随时有出故障掉线的可能。
|
1月前
|
存储 弹性计算 Linux
阿里云ECS(CentOS镜像)安装docker
阿里云ECS(CentOS镜像)安装docker
374 0
|
1月前
|
存储 网络协议 数据可视化
如何使用Synology Drive作为文件同步服务器实现云同步Obsidian笔记
如何使用Synology Drive作为文件同步服务器实现云同步Obsidian笔记
|
2月前
|
关系型数据库 Linux Shell
Centos系统上安装PostgreSQL和常用PostgreSQL功能
Centos系统上安装PostgreSQL和常用PostgreSQL功能
|
25天前
|
Ubuntu JavaScript 关系型数据库
在阿里云Ubuntu 20.04服务器中搭建一个 Ghost 博客
在阿里云Ubuntu 20.04服务器上部署Ghost博客的步骤包括创建新用户、安装Nginx、MySQL和Node.js 18.x。首先,通过`adduser`命令创建非root用户,然后安装Nginx和MySQL。接着,设置Node.js环境,下载Nodesource GPG密钥并安装Node.js 18.x。之后,使用`npm`安装Ghost-CLI,创建Ghost安装目录并进行安装。配置过程中需提供博客URL、数据库连接信息等。最后,测试访问前台首页和后台管理页面。确保DNS设置正确,并根据提示完成Ghost博客的配置。
在阿里云Ubuntu 20.04服务器中搭建一个 Ghost 博客