(转)Linux系统sersync数据实时同步
Linux系统sersync数据实时同步
原文:http://blog.csdn.net/mingongge/article/details/52985259
前面介绍了以守护进程的方式传输或同步数据rsync软件,linux系统数据同步软件很多,今天来介绍下sersync数据同步软件
一:sersync介绍
sersync其实是利用inotify和rsync两种软件技术来实现数据实时同步功能的,inotify是用于监听sersync所在服务器上的文件变化,结合rsync软件来进行数据同步,将数据实时同步给客户端服务器
二:sersync工作过程
在同步主服务器上开启sersync,负责监听文件系统的变化,然后调用rsync命令把更新的文件同步到目标服务器上,主服务器上安装sersync软件,目标服务器上安装rsync服务
三:整体环境拓扑图
四:客户端安装配置rsync服务
[root@Client ~]# cat /etc/rsyncd.conf
cat: /etc/rsyncd.conf: No such file or directory
如果有此文件,配置前要进行备份,再进行相关配置
[root@Client etc]# vi /etc/rsyncd.conf
##rsync config start
##created by root 2016-08-08 15:00
##rsync.conf config start
uid = rsync
gid = rsync
use chroot = no
max connetctions = 200
timeout = 100
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[backup]
path = /backup/
ignore errors
read only = false
list = false
hosts allow = 192.168.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
##rsync config end
"rsyncd.conf" [New] 21L, 458C written
添加用户
[root@Client ~]# useradd rsync -s /sbin/nologin -M
改变目录权限
[root@Client ~]# chown -R rsync.rsync /backup
配置密码文件
[root@Client ~]# echo "rsync_backup:rsync.conf">>/etc/rsync.password
[root@Client ~]# cat /etc/rsync.password
rsync_backup:rsync.conf
改变密码文件权限
[root@Client ~]# chmod 600 /etc/rsync.password
[root@Client ~]# ls -ld /etc/rsync.password
-rw-------. 1 root root 24 Sep 9 13:06 /etc/rsync.password
格式化文件
[root@Client ~]# dos2unix /etc/rsyncd.conf
dos2unix: converting file /etc/rsyncd.conf to UNIX format ...
开启服务后台运行
[root@Client ~]# rsync --daemon
[root@Client ~]# netstat -lntup|grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2002/rsync
tcp 0 0 :::873 :::* LISTEN 2002/rsync
五:主服务器上配置密码文件
[root@Master ~]# echo "rsync.conf">>/etc/rsync.password
[root@Master ~]# cat /etc/rsync.password
rsync.conf
[root@Master ~]# chmod 600 /etc/rsync.password
[root@Master ~]# ls -ld /etc/rsync.password
-rw-------. 1 root root 11 Sep 8 06:25 /etc/rsync.password
六:测试手工同步
[root@Master /]# rsync -avzP /etc/hosts rsync_backup@192.168.1.3::rsync --password-file=/etc/rsync.password
sending incremental file list
hosts
158 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
sent 120 bytes received 27 bytes 26.73 bytes/sec
total size is 158 speedup is 1.07
[root@Master /]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@Client ~]# cat /backup/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
经过对比两边数据一致,表明同步成功,手工同步成功之后,然后再进行后面的配置
七:安装sersync服务
首先下载好安装软件
sersync_64bit_binary_stable_final.tar.gz
[root@Master tools]# tar -zxvf sersync_64bit_binary_stable_final.tar.gz -C
/usr/local/
GNU-Linux-x86/
GNU-Linux-x86/sersync2
GNU-Linux-x86/confxml.xml
[root@Master tools]# cd /usr/local/
[root@Master local]# ls
bin games include lib64 sbin src
etc GNU-Linux-x86 lib libexec share
GNU-Linux-x86就是sersync安装软件,为了方便将它改名
[root@Master local]# mv GNU-Linux-x86 sersync
为了后续方便管理,创建几个目录用于存放各类文件
[root@Master sersync]# mkdir -p conf bin logs
[root@Master sersync]# mv confxml.xml conf
[root@Master sersync]# ls
bin conf logs sersync2
[root@Master sersync]# cd conf
[root@Master conf]# ls
confxml.xml
在配置配置文件之前备份
[root@Master conf]# cp confxml.xml confxml.xml.$(date +%F)
[root@Master conf]# ls
confxml.xml confxml.xml.2016-09-08
修改配置文件内容(confxml.xml)
1、修改24-28行
<localpath watch="/opt/tongbu">
<remote ip="127.0.0.1" name="tongbu1"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->注释内容
<!--<remote ip="192.168.8.40" name="tongbu"/>-->注释内容
</localpath>
修改后的内容为
<localpath watch="/opt/backup"> 本地数据的路径
<remote ip="192.168.1.3" name="rsync"/>远端IP与模块名称
</localpath>
<!#################################### -->注释内容
2、修改31-34行内容——认证
<commonParams params="-artuz"/>
<auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
修改后的内容为
<commonParams params="-aruz"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="true" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
3、修改36-37行
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default
every 60mins execute once-->
修改成我们刚刚创建好的logs目录
<failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecut
e="60"/><!--default every 60mins execute once-->
修改完成后最终的配置文件如下
[root@Master conf]# cat /usr/local/sersync/conf/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/backup">
<remote ip="192.168.1.3" name="rsync"/>
</localpath>
<!#################################### -->
<rsync>
<commonParams params="-aruz"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="true" time="100"/><!-- 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>
八:开启sersync守护进程
首先配置全局环境变量,使得后同可以直接调用sersync命令
[root@Master conf]# echo 'export PATH=$PATH:/usr/local/sersync/bin'>>/etc/profile
[root@Master conf]# tail -1 /etc/profile
export PATH=$PATH:/usr/local/sersync/bin
[root@Master conf]# source /etc/profile
[root@Master conf]# which sersync
/usr/local/sersync/bin/sersync
启动sersync服务
serync -r -d -o /usr/local/sersync/conf/confxml.xml
-r初始化数据
-d后台启动
-o指定路径
如果需要将命令开启动,只需将命令去掉参数-r定入/etc/rc.local
启动后结果如下
[root@Master backup]# sersync -r -d -o /usr/local/sersync/conf/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
option: -o config xml name: /usr/local/sersync/conf/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is rsync_backup
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 /opt/backup && rsync -aruz -R --delete ./ --timeout=100 rsync_backup@192.168.1.3::rsync --password-file=/etc/rsync.password >/dev/null 2>&1
run the sersync:
watch path is: /opt/backup
九:测试数据同步
[root@Master sersync]# cd /opt/backup/
[root@Master backup]# ls
[root@Master backup]# ls -ll
total 0
[root@Master backup]# touch 123
[root@Master backup]# touch 1234
[root@Master backup]# touch 1235
[root@Master backup]# touch 12333
[root@Master backup]# ls -ll
total 0
-rw-r--r--. 1 root root 0 Sep 8 09:26 123
-rw-r--r--. 1 root root 0 Sep 8 09:26 12333
-rw-r--r--. 1 root root 0 Sep 8 09:26 1234
-rw-r--r--. 1 root root 0 Sep 8 09:26 1235
目标服务器查看同步情况
[root@Client backup]# ls -ll
total 0
-rw-r--r--. 1 rsync rsync 0 Sep 8 04:26 123
-rw-r--r--. 1 rsync rsync 0 Sep 8 04:26 12333
-rw-r--r--. 1 rsync rsync 0 Sep 8 04:26 1234
-rw-r--r--. 1 rsync rsync 0 Sep 8 04:26 1235
测试结果表明数据同步正常,能够实时同步
(转)Linux系统sersync数据实时同步的更多相关文章
- Linux之sersync数据实时同步
sersync其实是利用inotify和rsync两种软件技术来实现数据实时同步功能的,inotify是用于监听sersync所在服务器上的文件变化,结合rsync软件来进行数据同步,将数据实时同步给 ...
- Rsync+Sersync数据实时同步(双向)
Rsync+Sersync数据实时同步(双向) 服务介绍 一.为什么要用rsync+sersync架构? 1.sersync是基于inotify开发的,类似于inotify-tools的工具 2.se ...
- Linux下Rsync+sersync实现数据实时同步
inotify 的同步备份机制有着缺点,于是看了sersync同步,弥补了rsync的缺点.以下转自:http://www.osyunwei.com/archives/7447.html 前言: 一. ...
- Rsync+sersync实现数据实时同步
前言: 一.为什么要用Rsync+sersync架构? 1.sersync是基于Inotify开发的,类似于Inotify-tools的工具 2.sersync可以记录下被监听目录中发生变化的(包括增 ...
- Linux下Rsync+Inotify-tools实现数据实时同步
Linux下Rsync+Inotify-tools实现数据实时同步 注意:下面的三个案例都是rsync 每次都是全量的同步(这就坑爹了),而且 file列表是循环形式触发rsync ,等于有10个文件 ...
- CentOS7下Rsync+sersync实现数据实时同步
近期公司要上线新项目,后台框架选型我选择当前较为流行的laravel,运行环境使用lnmp. 之前我这边项目tp32+apache,开发工具使用phpstorm. 新建/编辑文件通过phpstorm配 ...
- 项目实战:rsync+sersync实现数据实时同步
一.组网介绍 本次实验使用两台主机: qll251 角色:Rsync server + Sersync server qll252 角色: Rsync client 本次实验采用CentOS7.7系统 ...
- sersync实现数据实时同步
1.1 第一个里程碑:安装sersync软件 1.1.1 将软件上传到服务器当中并解压 1.上传软件到服务器上 rz -E 为了便于管理上传位置统一设置为 /server/tools 中 2.解压软件 ...
- sersync基于rsync+inotify实现数据实时同步
一.环境描述 需求:服务器A与服务器B为主备服务模式,需要保持文件一致性,现采用sersync基于rsync+inotify实现数据实时同步 主服务器A:192.168.1.23 从服务器B:192. ...
随机推荐
- 如何创建JUnit
这里拿Dynamic项目来演示,首先创建一个Dynamic项目,起名,点next, 继续点next, 将web.xml文件勾选,finish, 接下来在Java Resources->src下创 ...
- DELPHI 调用系统 ADO 配置窗体 提高软件易用性
最近DELPHI好像不太景气哦,把自己的代码拿出来晒晒.高手别喷哦. 直接上代码 implementation uses AdoConEd; var saveconnstr:string; proc ...
- Java Serializable(序列化)的理解和总结
1.序列化是干什么的? 简单说就是为了保存在内存中的各种对象的状态(也就是实例变量,不是方法),并且可以把保存的对象状态再读出来.虽然你可以用你自己的各种各样的方法来保存object st ...
- c# 解析MP3文件
不说那么多,网上有很多关于MP3文件说明的. 该C#代码是将C\C++转化过来的,可能存在问题. 如有需要的朋友可以参考. 项目中的文件路径大家自己修改. 下载地址
- linux 建议锁和强制锁
作为APUE 14.3节的参考 linux是有强制锁的,但是默认不开启.想让linux支持强制性锁,不但在mount的时候需要加上-o mand,而且对要加锁的文件也需要设置相关权限. . ...
- The method identifyUser(Arrays.asList("group001"), String, new HashMap<>()) is undefined for the type AipFace
在使用百度云的人脸识别sdk时遇到了这个错误,网上百度不到解决的方法,当我浏览百度云的时候发现了这个 于是考虑到版本可能更新,出现了新的函数代替旧的函数,于是去查文档,文档链接如下 https://c ...
- 我的java问题排查工具单
前言 平时的工作中经常碰到很多疑难问题的处理,在解决问题的同时,有一些工具起到了相当大的作用,在此书写下来,一是作为笔记,可以让自己后续忘记了可快速翻阅,二是分享,希望看到此文的同学们可以拿出自己日常 ...
- OOP2(虚函数/抽象基类/访问控制与继承)
通常情况下,如果我们不适用某个函数,则无需为该函数提供定义.但我们必须为每个虚函数都提供定义而不管它是否被用到了,这因为连编译器也无法确定到底会适用哪个虚函数 对虚函数的调用可能在运行时才被解析: 当 ...
- mybatis 学习笔记(四):mybatis 和 spring 的整合
mybatis 学习笔记(四):mybatis 和 spring 的整合 尝试一下整合 mybatis 和 spring. 思路 spring通过单例方式管理SqlSessionFactory. sp ...
- spring 学习(五):spring 事务
spring 学习(五):spring 事务 事务概要 一个数据库事务通常包含了一个序列的对数据库的读/写操作.它的存在包含有以下两个目的: 为数据库操作序列提供了一个从失败中恢复到正常状态的方法,同 ...