Rsync+Sersync文件实时同步
rsync介绍:
rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。
基本特性
⚫ 可以镜像保存整个目录树和文件系统
⚫ 可以很容易做到保持原来文件的权限、时间、软硬链接等
⚫ 无须特殊权限即可安装
⚫ 优化的流程,文件传输效率高
⚫ 可以使用 rsh、ssh 方式来传输文件,当然也可以通过直接的 socket 连接
⚫ 支持匿名传输,以方便进行网站镜象在 windows 平台下也有相应的版本,如 cwrsync 和DeltaCopy 等。
Sersync介绍和对比 :
1、Sersync 使用 c++ 编写,对 linux 系统文件产生的临时文件和重复的文件操作会进行过滤,在本文后面会提到该点。使用sersyc和rsync结合做同步的时候,会大大减少运行时所消耗的本地以及网络资源,因此在速度方面有显著提升。
2、相比 Inotify-tools 和 Openduckbill,Sersync 配置起来更为简单方便。在谷歌 S
ersync 项目下载的安装包的 bin 目录下,放置了已经编译好的二进制文件,搭配 bin 目录下的xml文件可以直接部署使用。
3、Sersync 采用多线程(默认10)进行同步(即可以并发同步多个不同文件),尤其是针对较大文件同步的时候,它能够保证多个服务器实时保持同步状态。
4、Sersync 自带了出错处理机制。它可以通过失败队列自动对之前出错的文件进行重新同步操作。如果届时依旧失败,它会每 10 个小时对同步失败的文件再进行重新同步操作,直到文件同步为止。
5、Sersync 自带有 crontab 功能,因此不需要借助系统的 crontab ,只需在 xml 配置文件中开启该功能,即可按预先的配置,每隔一段时间自动做一次整体同步操作。
6、Sersync 还自带了 socket 与 refreshCDN 的协议扩展,可以满足有特殊需求的公司二次开发。
Sersync和inotify进行对比
inotify优点:
监控文件系统事件变化,通过同步工具实现实时数据同步
iontify缺点:
1.并发如果大于200个文件(10-100K),同步会有延迟
2.监控到事件后,调用rsync同步是单线程的(加&并发),sersync多进程同步
sersync的优点:
(1)配置文件
(2) 真正的守护进程socket
(3)可以对失败的文件定时重传
(4)第三方的http接口
(5) 默认是多线程的同步
环境介绍:
机器 Centos7.x
| 主机名 | ip | 软件包 |
|---|---|---|
| backup | 192.168.200.10 | Rsync |
| master | 192.168.200.20 | Sersync |
Rsync配置
Centos 7 自带rsync
[root@backup ~]# rpm -qa rsync
rsync-3.1.2-6.el7_6.1.x86_64
# 配置rsync配置文件 按照需求根据下面修改
[root@backup ~]# cat /etc/rsyncd.conf
#用户远端的命令使用rsync访问共享目录
uid = rsync
#安全组
gid = rsync
#安全相关
use chroot = no
#最大连接
max connections = 200
#超时时间
timeout = 300
#pid存放位置
pid file = /var/run/rsyncd.pid
#锁文件
lock file = /var/run/rsync.lock
#日志文件
log file = /var/log/rsyncd.log
#忽略错误
ignore errors
#可写
read only = false
#不能列表
list = false
#允许主机网段
hosts allow = 192.168.200.0/24
#虚拟用户
auth users = rsync_backup
#虚拟用户和密码
secrets file = /etc/rsync.password
fake super = yes
#共享模块名
[www]
#模块注释
comment = "Here's the explanation"
#共享位置
path = /data/www/
[bbs]
comment = "Here's the explanation"
path = /data/bbs/
[root@backup ~]# mkdir /data/{www,bbs} -p
[root@backup ~]# ll /data/
总用量 0
drwxr-xr-x. 2 root root 6 12月 18 15:27 bbs
drwxr-xr-x. 2 root root 6 12月 18 15:27 www
#创建rsync用户
[root@backup ~]# useradd rsync -s /sbin/nologin
#编写虚拟用户和密码 把他输出到一个文件里面
[root@backup ~]# echo "rsync_backup:pwd123" > /etc/rsync.password
#给予权限
[root@backup ~]# chmod 600 /etc/rsync.password
#后台启动rsync
[root@backup ~]# rsync --daemon
#查看是否启动
[root@backup ~]# netstat -anpt | grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 29893/rsync
tcp6 0 0 :::873 :::* LISTEN 29893/rsync
配置Sersync服务 master端
[root@master ~]# rpm -qa | grep rsync
rsync-3.1.2-6.el7_6.1.x86_64
[root@master ~]# echo "pwd123" > /etc/rsync.password
[root@master ~]# cat /etc/rsync.password
pwd123
[root@master ~]# chmod 600 /etc/rsync.password
[root@master ~]# mkdir /backup
[root@master ~]# cd /backup/
[root@localhost backup]# mkdir {1..10}.txt
[root@master backup]# ll
总用量 0
-rw-r--r--. 1 root root 0 12月 18 15:46 10.txt
-rw-r--r--. 1 root root 0 12月 18 15:46 1.txt
-rw-r--r--. 1 root root 0 12月 18 15:46 2.txt
-rw-r--r--. 1 root root 0 12月 18 15:46 3.txt
-rw-r--r--. 1 root root 0 12月 18 15:46 4.txt
-rw-r--r--. 1 root root 0 12月 18 15:46 5.txt
-rw-r--r--. 1 root root 0 12月 18 15:46 6.txt
-rw-r--r--. 1 root root 0 12月 18 15:46 7.txt
-rw-r--r--. 1 root root 0 12月 18 15:46 8.txt
-rw-r--r--. 1 root root 0 12月 18 15:46 9.txt
[root@master backup]# rsync -avz /backup/* rsync_backup@192.168.200.10::www --password-file=/etc/rsync.password
sending incremental file list
1.txt
10.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
sent 557 bytes received 214 bytes 1,542.00 bytes/sec
total size is 0 speedup is 0.00
[root@backup www]# pwd
/data/www
[root@backup www]# ll
总用量 0
-rw-r--r--. 1 rsync rsync 0 12月 18 15:46 10.txt
-rw-r--r--. 1 rsync rsync 0 12月 18 15:46 1.txt
-rw-r--r--. 1 rsync rsync 0 12月 18 15:46 2.txt
-rw-r--r--. 1 rsync rsync 0 12月 18 15:46 3.txt
-rw-r--r--. 1 rsync rsync 0 12月 18 15:46 4.txt
-rw-r--r--. 1 rsync rsync 0 12月 18 15:46 5.txt
-rw-r--r--. 1 rsync rsync 0 12月 18 15:46 6.txt
-rw-r--r--. 1 rsync rsync 0 12月 18 15:46 7.txt
-rw-r--r--. 1 rsync rsync 0 12月 18 15:46 8.txt
-rw-r--r--. 1 rsync rsync 0 12月 18 15:46 9.txt
在master安装Sersync
#下载Sersync包
[root@master ~]# wget https://raw.githubusercontent.com/wsgzao/sersync/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@master ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
[root@master ~]# cd /usr/local/
#解压改名
[root@master local]# mv GNU-Linux-x86/ sersync
[root@master local]# cd sersync/
#创建 存放配置文件和启动和日志目录
[root@master sersync]# mkdir conf bin logs
[root@master sersync]# mv confxml.xml conf/
[root@master sersync]# mv sersync2 bin/sersync
[root@master sersync]# cd /usr/local/sersync/conf/
[root@master conf]# ls
confxml.xml
[root@master conf]# cp confxml.xml confxml.xml.bak
#修改配置文件 24行,25行,31行,33行,36行日志位置
[root@master conf]# cat confxml.xml
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <head version="2.5">
3 <host hostip="localhost" port="8008"></host>
4 <debug start="false"/>
5 <fileSystem xfs="false"/>
6 <filter start="false">
7 <exclude expression="(.*)\.svn"></exclude>
8 <exclude expression="(.*)\.gz"></exclude>
9 <exclude expression="^info/*"></exclude>
10 <exclude expression="^static/*"></exclude>
11 </filter>
12 <inotify>
13 <delete start="true"/>
14 <createFolder start="true"/>
15 <createFile start="false"/>
16 <closeWrite start="true"/>
17 <moveFrom start="true"/>
18 <moveTo start="true"/>
19 <attrib start="false"/>
20 <modify start="false"/>
21 </inotify>
22
23 <sersync>
24 <localpath watch="/backup/">
25 <remote ip="192.168.200.10" name="www"/>
26 <!--<remote ip="192.168.8.39" name="tongbu"/>-->
27 <!--<remote ip="192.168.8.40" name="tongbu"/>-->
28 </localpath>
29 <rsync>
30 <commonParams params="-artuz"/>
31 <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
32 <userDefinedPort start="false" port="874"/><!-- port=874 -->
33 <timeout start="true" time="100"/><!-- timeout=100 -->
34 <ssh start="false"/>
35 </rsync>
36 <failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
37 <crontab start="false" schedule="600"><!--600mins-->
38 <crontabfilter start="false">
39 <exclude expression="*.php"></exclude>
40 <exclude expression="info/*"></exclude>
41 </crontabfilter>
42 </crontab>
43 <plugin start="false" name="command"/>
44 </sersync>
45
46 <plugin name="command">
47 <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
48 <filter start="false">
49 <include expression="(.*)\.php"/>
50 <include expression="(.*)\.sh"/>
51 </filter>
52 </plugin>
53
54 <plugin name="socket">
55 <localpath watch="/opt/tongbu">
56 <deshost ip="192.168.138.20" port="8009"/>
57 </localpath>
58 </plugin>
59 <plugin name="refreshCDN">
60 <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
61 <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
62 <sendurl base="http://pic.xoyo.com/cms"/>
63 <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
64 </localpath>
65 </plugin>
66 </head>
#添加环境变量
[root@master ~]# echo "export PATH=$PATH:/usr/local/sersync/bin">>/etc/profile
[root@master ~]# tail -n 1 /etc/profile
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/sersync/bin
[root@master ~]# source /etc/profile
#启动sersync
[root@master conf]# 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 /backup && rsync -artuz -R --delete ./ --timeout=100 rsync_backup@192.168.200.10::www --password-file=/etc/rsync.password >/dev/null 2>&1
run the sersync:
watch path is: /backup
验证一下
[root@master backup]# mkdir {1..3}.txt
[root@master backup]# ls
1.txt 2.txt 3.txt
#去那个机器查看一下查看一下
#www对应的是 /data/www目录
[root@backup ~]# cd /data/www/
[root@backup www]# pwd
/data/www
[root@backup www]# ls
1.txt 2.txt 3.txt
到这里实时同步已经完成
Rsync参数说明
q, --quiet 精简输出模式
-c, --checksum 打开校验开关,强制对文件传输进行校验
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-r, --recursive 对子目录以递归模式处理
-R, --relative 使用相对路径信息
-b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可 以使用–suffix选项来指定不同的备份文件前缀。
–backup-dir 将备份文件(如~filename)存放在在目录下。
-suffix=SUFFIX 定义备份文件前缀
-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
-l, --links 保留软链结
-L, --copy-links 想对待常规文件一样处理软链结
–copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结
–safe-links 忽略指向SRC路径目录树以外的链结
-H, --hard-links 保留硬链结
-p, --perms 保持文件权限
-o, --owner 保持文件属主信息
-g, --group 保持文件属组信息
-D, --devices 保持设备文件信息
-t, --times 保持文件时间信息
-W, --whole-file 拷贝文件,不进行增量检测
-x, --one-file-system 不要跨越文件系统边界
-B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节
-e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步
–rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息
-C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件
–existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件
–delete 删除那些DST中SRC没有的文件
–delete-excluded 同样删除接收端那些被该选项指定排除的文件
–delete-after 传输结束以后再删除
–ignore-errors 及时出现IO错误也进行删除
–max-delete=NUM 最多删除NUM个文件
–partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
–force 强制删除目录,即使不为空
–numeric-ids 不将数字的用户和组ID匹配为用户名和组名
–timeout=TIME IP超时时间,单位为秒
-I, --ignore-times 不跳过那些有同样的时间和长度的文件
–size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间
–modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0
-T --temp-dir=DIR 在DIR中创建临时文件
–compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份
-P 等同于 --partial
–progress 显示备份过程
-z, --compress 对备份的文件在传输时进行压缩处理
–exclude=PATTERN 指定排除不需要传输的文件模式
–include=PATTERN 指定不排除而需要传输的文件模式
–exclude-from=FILE 排除FILE中指定模式的文件
–include-from=FILE 不排除FILE指定模式匹配的文件
–address 绑定到特定的地址
–config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件
–port=PORT 指定其他的rsync服务端口
–blocking-io 对远程shell使用阻塞IO
-stats 给出某些文件的传输状态
–progress 在传输时现实传输过程
–log-format=formAT 指定日志文件格式
–password-file=FILE 从FILE中得到密码
–bwlimit=KBPS 限制I/O带宽,KBytes per second
参考文章:
https://blog.51cto.com/wsxxsl/1767071
4083

被折叠的 条评论
为什么被折叠?



