rsync+sersync实现数据文件实时同步

使用rsync+sersync,实现nfs与backup服务器间实时数据同步

1、数据同步工具介绍

rsync是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具。rsync软件适用于unix/linux/windows等多种操做系统平台。php

rsync和ssh带的scp命令比较类似,但又优于scp命令的功能,scp每次都是全量拷贝,而rsync能够进行增量拷贝。固然,rsync还能够在本地主机的不一样分区或目录之间全量及增量的复制数据,这又相似cp命令,但一样也优于cp命令,cp每次都是全量拷贝,而rsync能够增量拷贝.利用rsync还能够实现删除文件和目录功能,这又至关于rm命令。html

sersync是基于Inotify开发的,相似于Inotify-tools的工具;mysql

sersync能够记录下被监听目录中发生变化的(包括增长、删除、修改)具体某一个文件或某一个目录的名字;linux

rsync在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变化的数据相对整个同步目录数据来讲是很小的,rsync在遍历查找比对文件时,速度很快),所以,效率很高;git

:当同步的目录数据量不大时,建议使用Rsync+Inotify-tools;当数据量很大(几百G甚至1T以上)、文件不少时,建议使用Rsync+sersync。github

2、实操部分

一、rsync以守护进程(socket)的方式传输数据

这个是rsync自身的重要功能,咱们实验的机器以下:sql

服务端IP:172.16.1.41mongodb

客户端IP:172.16.1.31express

服务端配置

先新建配置文件,请注意rsyncd.conf配置文件只用在服务端新建就好。vim

vim /etc/rsyncd.conf

如下只是配置文件中最经常使用的部份,更多的请使用命令man rsyncd.conf查看。

uid = rsync       # 运行rsync的用户和组id

gid = rsync

use chroot = no       # bug信息的处理,一种安全方式

max connections = 200    # 最大的链接数

timeout = 300           # 超时时间

pid file = /var/run/rsyncd.pid            # pid进程号文件

lock file = /var/run/rsync.lock           # 锁文件

log file = /var/run/rsyncd.log            # 日志文件

[backup]           # 须要同步的模块,这是其中一个,能够有多个,对应不一样的目录,客户端能够

                                     # 选择要同步的目录

path = /backup                   # 同步的根目录

ignore errors             # 忽略错误

read only = false                # 只读falsh 表示可读可写

list = false                            # 不可列表

hosts allow = 172.16.1.0/24             # 容许访问的网段

#hosts deny = 0.0.0.0/32                            # 拒绝访问的网段,容许与拒毫不能同时设置

auth users = rsync_backup               # 用户名

secrets file = /etc/rsync.password           # 密码文件路径,密码文件权限要设置成600

以守护进程的方式启动rsync

[root@backup ~]# rsync --deamon

查看是否启动成功,rsyncd的默认端口是873

[root@backup ~]# netstat -lntup | grep 873

[linguang@backup tmp]$ ps -ef|grep rsync|grep -v grep

[linguang@backup tmp]$ lsof -i :873

-l显示监控中的服务器的Socket;-n不经过域名服务器;p显示正在使用Socket的PID和程序名

若是启动出错,咱们就须要查看一下系统日志,咱们这里日志显示正常启动

[root@backup /]# cat /var/run/rsyncd.log

2018/12/20 15:02:04 [18822] rsyncd version 3.0.6 starting, listening on port 873

建立一个不须要登陆的系统用户

[root@backup ~]# useradd rsync -s /bin/nologin -M        

[root@backup ~]# id rsync

uid=501(rsync) gid=501(rsync) 组=501(rsync)

下面咱们建立须要同步的目录,并给予相应的权限

[root@backup ~]# mkdir -p /backup

[root@backup /]# chown -R rsync.rsync backup

将用户名和密码重定义输出到咱们的密码存放文件

# rsync_backup是用户名,linguang是密码

[root@backup /]# cat /etc/rsync.password

rsync_backup:linguang

[root@backup /]# chmod 600 /etc/rsync.password

重启rsync服务:

[root@backup ~]# lsof -i:873

COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

rsync   19553 root    4u  IPv4  62838      0t0  TCP *:rsync (LISTEN)

rsync   19553 root    5u  IPv6  62839      0t0  TCP *:rsync (LISTEN)

[root@backup ~]# pkill rsync

[root@backup ~]# lsof -i:873

[root@backup ~]# rsync --daemon

rsync服务添加到开机启动脚本中:

[root@backup ~]# echo 'rsync --daemon'>>/etc/rc.local

[root@backup ~]# tail -1 /etc/rc.local

rsync --daemon

同时咱们须要关闭防火樯

[root@backup date]# /etc/init.d/iptables stop    /status查看状态

客户端配置

将密码保存在密码配置文件,同是为了与服务端统一,咱们使用至关的文件名,注意这里咱们只须要放入密码便可

[root@vagrant-centos65 ~]# echo "linguang" >/etc/rsync.password

[root@vagrant-centos65 ~]# chmod 600 /etc/rsync.password

[root@vagrant-centos65 ~]# cat /etc/rsync.password

linguang

rsync经过daemon方式远程传输的语法为:

Access via rsync daemon:

Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]

rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]

Push: rsync [OPTION...] SRC... [USER@]HOST::DEST

rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

在客户端进行拉取实例:

先看看server端的目录结构

[root@backup backup]# pwd

/backup

[root@backup backup]# mkdir date/{run,log}/linguang -p

[root@backup backup]# tree

.

`-- date

    |-- log

    |   `-- linguang

    `-- run

        `-- linguang

 

5 directories, 0 files

开始提取数据

##backup(模块名) 即服务端/backup

 [root@nfs01 tmp]# rsync -avzP rsync_backup@172.16.1.41::backup /tmp --password-file=/etc/rsync.password     ##自动读取密码,参数--password-file=/etc/rsync.password

receiving incremental file list

./

date/

date/log/

date/log/linguang/

date/run/

date/run/linguang/

sent 87 bytes  received 221 bytes  616.00 bytes/sec

total size is 0  speedup is 0.00

[root@nfs01 tmp]# tree

.

└── date

    ├── log

    │   └── linguang

    └── run

        └── linguang

 

5 directories, 0 files

在客户端进行推送实例

[linguang@nfs01 tmp]$ sudo touch teach{1..20}

[linguang@nfs01 tmp]$ rsync -avzP /tmp/ rsync_backup@172.16.1.41::backup/nfs01back/tmp --password-file=/etc/rsync.password

rsync: could not open password file "/etc/rsync.password": Permission denied (13)

Password:

@ERROR: auth failed on module backup

rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]

[linguang@nfs01 tmp]$ sudo rsync -avzP /tmp/ rsync_backup@172.16.1.41::backup/nfs01back/tmp --password-file=/etc/rsync.password

sending incremental file list

created directory nfs01back/tmp

./

teach1

           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=25/27)

。。。。。。。。。

date/run/

date/run/linguang/

sent 1068 bytes  received 415 bytes  2966.00 bytes/sec

total size is 0  speedup is 0.00

 其它语法

排除连续的文件,推送文件到服务端

[root@nfs01 ~]# rsync -avz /root/ rsync://rsync_backup@172.16.1.41:873/backup/nfs01 --exclude=student{10..15} --password-file=/etc/rsync.password  

sending incremental file list

created directory nfs01

./

.bash_history

.bash_logout

.bash_profile

.bashrc

提取服务端文件到本地目录下

[root@nfs01 ~]# rsync -avzP rsync://rsync_backup@172.16.1.41:873/backup/test/ /tmp --password-file=/etc/rsync.password  

receiving incremental file list

./

a/

a/girld/

b/

b/girld/

sent 83 bytes  received 169 bytes  504.00 bytes/sec

total size is 0  speedup is 0.00

限速传输:

[root@nfs01 ~]# dd if=/dev/zero of=logfile.tar bs=1M count=128

记录了128+0 的读入

记录了128+0 的写出

134217728字节(134 MB)已复制,5.06455 秒,26.5 MB/秒

[root@nfs01 ~]# rsync -avzP /root/logfile.tar rsync_backup@172.16.1.41::backup/logfile --bwlimit=100 --password-file=/etc/rsync.password 

sending incremental file list

logfile.tar

   134217728 100%   93.20MB/s    0:00:01 (xfer#1, to-check=0/1)

sent 130594 bytes  received 27 bytes  29026.89 bytes/sec

total size is 134217728  speedup is 1027.54

[root@nfs01 ~]# rsync -avzP /root/logfile.tar rsync_backup@172.16.1.41::backup/logfile --bwlimit=10 --password-file=/etc/rsync.password

sending incremental file list

logfile.tar

   134217728 100%    9.12MB/s    0:00:14 (xfer#1, to-check=0/1)

sent 130594 bytes  received 27 bytes  8427.16 bytes/sec

total size is 134217728  speedup is 1027.54

二、下载sersync压缩包

[root@nfs01 /]# mkdir application

[root@nfs01 /]# cd application

[root@nfs01 application]# wget https://github.com/wsgzao/sersync/raw/master/sersync2.5.4_64bit_binary_stable_final.tar.gz

[root@nfs01 sersync]# tree

.

├── bin

│   └── sersync

├── conf

│   └── confxml.xml

└── logs

 

1.1.1.2  配置文件介绍

[root@nfs01 data]# cat /application/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监控事件状况设置

   <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="/data">

    #修改为备份服务器ip、模块名称,而不是本机ip地址,能够有多台备份服务器

    <remote ip="192.168.90.41" name="nfsbackup"/>

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

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

</localpath>

<rsync>

    #下面的是rsync使用的参数,可自定义,也能够根扩展--delete等,来实现想要的备份方式

    <commonParams params="-avz"/>

    #下面是是否要启动,改为true便可,users为用于rsync鉴权的用户名,在rsync服务器上设置

    <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="/application/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命令:

[root@nfs01 data]# /application/sersync/bin/sersync -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命令推送一遍

参数-n: 指定开启守护线程的数量,默认为10个

参数-o:指定配置文件,默认使用confxml.xml文件

参数-m:单独启用其余模块,使用 -m refreshCDN 开启刷新CDN模块

参数-m:单独启用其余模块,使用 -m socket 开启socket模块

参数-m:单独启用其余模块,使用 -m http 开启http模块

不加-m参数,则默认执行同步程序

[root@nfs01 data]# /application/sersync/bin/sersync -d -r -n 8 -o /application/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: -d      run as a daemon

option: -r      rsync all the local files to the remote servers before the sersync work

option: -n      thread num is:  8

option: -o      config xml name:  /application/sersync/conf/confxml.xml

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 10  = 1(primary thread) + 1(fail retry thread) + 8(daemon sub threads)

Max threads numbers is: 18 = 10(Thread pool nums) + 8(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 -avz -R --delete ./ rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password >/dev/null 2>&1

run the sersync:

watch path is: /data

[root@nfs01 data]# ps -ef |grep sersync

[root@nfs01 data]# pkill sersync

添加到开机启动项:

[root@nfs01 data]# tail -1 /etc/rc.local

#/application/sersync/bin/sersync -d -r -n 8 -o /application/sersync/conf/confxml.xml

 

三、高并发数据实时同步方案小结及拓展

1)inotify(sersync)+rsync,是文件级别的。

2)drbd文件系统级别,文件系统级别,基于block块同步,缺点:备节点数据不可用。

3)第三方软件的同步功能:mysql同步,oracle,mongodb。

4)程序双写,直接写两台服务器。

5)利用产品业务逻辑解决(读写分离,备读不到,读主)

6)NFS集群:1,4,5方案整合,双写主存储,备存储用inotify(sersync)+rsync,备没有找主解决延迟问题)

相关文章
相关标签/搜索