sersync+rsync数据同步

什么是Rsync?
Rsync(Remote Synchronize)是一款开源的、快速的、多功能的、可以实现全量及增量的本地或远程数据同步备份的优秀工具,并且支持多种操作系统平台运行。
官网文档:https://rsync.samba.org/ftp/rsync/rsync.html

Rsync简介
Rsync具有本地与远程两台主机之间的数据快速复制同步镜像、远程备份等功能,该功能类似scp,但是优于scp功能,还具有本地不同分区目录之间全量及增量复制数据。

Rsync同步数据镜像时,通过“quick check”算法,仅同步大小或最后修改时间发生变化的文件或目录,当然也可以根据权限,属主等属性变化的同步,所以可以实现快速同步。

rsync 具有如下的基本特性:
1 可以镜像保存整个目录树和文件系统
2 可以很容易做到保持原来文件的权限、时间、软硬链接等
3 无须特殊权限即可安装
4 优化的流程,文件传输效率高
5 可以使用 rsh、ssh 方式来传输文件,当然也可以通过直接的 socket 连接
6 支持匿名传输,以方便进行网站镜象

Sersync 项目简介:
本项目利用inotify与rsync对服务器进行实时同步,其中inotify用于监控文件系统事件,rsync是目前广泛使用的同步算法,其优点是只对文件不同的部分进行操作,所以其优势大大超过使用挂接文件系统的方式进行镜像同步。目前使用的比较多的同步程序版本是inotify-tools,另外一个是google开源项目Openduckbill(依赖于inotify-tools),这两个都是基于脚本语言编写的,其设计思路同样是采用inotify与rsync命令。 相比较上面两个项目,本项目优点是:
1.sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤,所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。
2.相比较上面两个项目,sersync配置起来很简单,二进制文件+XML配置文件。
3.使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。
4.自带出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则每N个小时对同步失败的文件重新同步。
5.自带crontab功能,只需在xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次。
6.自带socket与http协议扩展,满足您二次开发的需要。

系统资源:
[root@test ~]# cat /etc/redhat-release
CentOS release 6.8 (Final)

10.125.192.10 sersync 分发服务
10.125.192.12 rsync 同步服务

检查是否安装rsync
[root@test ~]# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64

如未安装使用yum安装
yum -y install rsync

rsync 同步服务器操作
vim /etc/rsyncd.conf (默认没有该文件,手工创建)
uid = root //运行RSYNC守护进程的用户
gid = root //运行RSYNC守护进程的组
use chroot = no //不使用chroot
max connections = 2000 //最大连接数
strict modes = yes //是否检查口令文件的权限
timeout = 600 //超时时间
port = 873 //默认端口873
pid file = /var/run/rsyncd.pid //pid文件的存放位置
lock file = /var/run/rsync.lock //锁文件的存放位置
log file = /var/run/rsyncd.log //日志记录文件的存放位置
ignore errors //可以忽略一些无关的IO错误
hosts allow = 10.125.192.10 //允许主机
hosts deny = 0.0.0.0/32 //禁止主机
read only = no //是否只读
dont compress = *.gz *.bz2 *.zip *.tgz //不压缩哪些格式
auth users = test //认证用户
secrets file = /etc/rsyncd.pass //账户文件位置

[www] //定义一个项目
path = /data //要同步备份的路径

创建备份目录
mkdir /data

创建认证用户文件
vim /etc/rsyncd.pass
更改文件权限 必须为600
chmod 600 /etc/rsyncd.pass

启动rsync
rsync --daemon
如果手动创建的配置文件的名字不是rsyncd.conf启动的时候需要加参数
rsync --daemon --config=/配置文件的绝对路径

配置开机启动
vim /etc/rc.d/rc.local
/usr/bin/rsync --daemon

启动后验证
[root@test ~]# lsof -i :873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 19887 root 3u IPv4 135575 0t0 TCP *:rsync (LISTEN)
rsync 19887 root 5u IPv6 135576 0t0 TCP *:rsync (LISTEN)

关闭rsync服务
pkill rsync

kill -9 `cat /var/run/rsyncd.pid`

sersync分发服务器配置
分发服务器上rsync配置
vim /etc/rsyncd.pass
新建一个密码文件,里面只写跟用户名对应的密码即可

配置文件权限
chmod 600 /etc/rsyncd.pass

手工测试rsync同步
[root@test data]# rsync -avz /data test@10.125.192.12::www --password-file=/etc/rsyncd.pass
sending incremental file list
data/
data/1.txt
data/10.txt
data/2.txt
data/3.txt
data/4.txt
data/5.txt
data/6.txt
data/7.txt
data/8.txt
data/9.txt

sent 512 bytes received 202 bytes 1428.00 bytes/sec
total size is 0 speedup is 0.00

注:通过rsync 把当前主机root目录推送到10.125.192.12上
test用户是rsync(同步端)定义的认证用户
www是rsync(同步端)定义的项目 向项目定义的路径中同步数据
–password-file=/etc/rsyncd.pass sersync分发服务器上定义的用户认证文件路径

部署sersync服务
tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
cd /usr/local/
mv GNU-Linux-x86 sersync
cd sersync/
mkdir conf bin logs
mv confxml.xml conf/
mv sersync2 bin/sersync
cp conf/confxml.xml conf/confxml.xml.bak

编辑配置文件
vim /usr/local/sersync/conf/confxml.xml
首先修改源文件24-28行内容
源文件内容

 24         <localpath watch="/opt/tongbu">                            #本地目录
 25             <remote ip="127.0.0.1" name="tongbu1"/>                #远程rsync路径与共享名
 26             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
 27             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 28         </localpath>

修改后内容

 24         <localpath watch="/data/">
 25             <remote ip="10.125.192.12" name="www"/>
 26             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
 27             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 28         </localpath>

修改源文件35-41行

 35          <rsync>
 36             <commonParams params="-artuz"/>
 37             <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>  #账号与本地密码文件路径
 38             <userDefinedPort start="false" port="874"/><!-- port=874 -->
 39             <timeout start="false" time="100"/><!-- timeout=100 -->
 40             <ssh start="false"/>
 41         </rsync>

修改后

 35          <rsync>
 36             <commonParams params="-az"/>
 37             <auth start="true" users="test" passwordfile="/etc/rsyncd.pass"/> 
 38             <userDefinedPort start="false" port="874"/><!-- port=874 -->
 39             <timeout start="true" time="100"/><!-- timeout=100 -->
 40             <ssh start="false"/>
 41         </rsync>

改源文件42行 错误日志保存位置
失败后60分钟后重试

 <failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->

配置sersync环境变量
echo ‘export PATH=$PATH:/usr/local/sersync/bin’>>/etc/profile
source /etc/profile

启动sersync
sersync -r -d -o /usr/local/sersync/conf/confxml.xml
注: -r 首次同步需要加的参数 同步所有内容
-d 后台运行
-o 指定配置文件
-n 指定线程数 默认为10个

检查启动结果
[root@test data]# ps aux|grep sersync
root 3906 0.0 0.0 145536 872 ? Ssl 13:14 0:00 sersync -r -d -o /usr/local/sersync/conf/confxml.xml

设置开机自启
vim /etc/rc.d/rc.local
/usr/local/sersync/bin/sersync -d -o /usr/local/sersync/conf/confxml.xml

sersync多实例
如果需要对多个目录进行同步,把多个目录配置写到一个配置文件中 只有第一个配置生效,其余配置不生效,所以采取了创建多配置文件的方法来实现 多个目录同步。

cp confxml.xml www_confxml.xml
cp confxml.xml logs_confxml.xml
cp confxml.xml tools_confxml.xml

vim /usr/local/sersync/conf/www_confxml.xml #修改一个单实例为例

<sersync>
        <localpath watch="/data/www">                  
            <remote ip="10.125.192.2" name="www"/>      
            <remote ip="10.125.192.3" name="www"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-avz --delete"/>
            <auth start="true" users="gao" passwordfile="/root/rsync.pass"/>  
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="true" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        <failLog path="/usr/local/sersync/logs/www_fail_log.sh" timeToExecute=" 
60"/><!--default every 60mins execute once-->
<crontab start="true" schedule="600"><!--600mins--> 

启动方法
sersync -r -d -o /usr/local/sersync/conf/www_confxml.xml
sersync -r -d -o /usr/local/sersync/conf/logs_confxml.xml
sersync -r -d -o /usr/local/sersync/conf/tools_confxml.xml

加入自启动
vim /etc/rc.d/rc.local
/usr/local/sersync/bin/sersync -d -o /usr/local/sersync/conf/confxml.xml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值