第 1 章 关于 Sersync
1.1 什么是实时同步
什么是实时同步
实时同步是一种只要当前目录触发事件, 就马上同步到远程的目录
1.2 为什么使用实时同步
保证数据的连续性 (定时任务是以分钟为单位的)
减少人力维护成本
1.3 实时同步工具
- inotify+RSYNC(x)
- sersync+RSYNC()
- lsyncd
Inotify 是一个通知接口, 用来监控文件系统的各种变化, 如果文件存取, 删除, 移动. 可以非常方便地实现文件异动告警, 增量备份, 并针对目录或文件的变化及时作出响应. rsync+inotify 可以实触发式实时同步增量备份
sersync 是国人基于 rsync+inotify-tools 开发的工具, 不仅保留了优点同时还强化了实时监控, 文件过滤, 简化配置等功能, 帮助用户提高运行效率, 节省时间和网络资源.
第 2 章 inotify 与 sersync 对比
2.1 Inotify 实时并发:
结论: 经过测试, 每秒 200 文件并发, 数据同步几乎无延迟 (小于 1 秒)
2.1.1 inotify 优点:
监控文件系统事件变化, 通过同步工具实现实时数据同步.
2.1.2 inotify 缺点:
并发如果大于 200 个文件 (10-100k), 同步就会有延迟
监控到事件后, 调用 rsync 同步是单进程的, 而 sersync 为多进程同步.
2.2 serysync 实时同步
2.2.1 serysync 优点
serysync 功能多:(inotify+rsync 命令)
1) 支持通过配置文件管理
2) 真正的守护进程 socket
3) 可以对失败文件定时重传 (定时任务功能)
4) 第三方的 HTTP 接口 (例如: 更新 cdn 缓存)
5) 默认多进程 rsync 同步
2.3 高并发数据实时同步方案小结
1)inotify(sersync)+ rsync, 是文件级别的.
2)drbd 文件系统级别, 文件系统级别, 基于 block 块同步, 缺点: 备节点数据不可用
3) 第三方软件的同步功能: mysql 同步 (主从复制),oracle,mongodb
4) 程序双写, 直接写两台服务器.
5) 利用产品业务逻辑解决 (读写分离, 备份读不到, 读主)
第 3 章 配置 Sersync
实现 web 服务器上传视频文件, 实则是写入 NFS 存储, 当 NFS 存在新的数据则会实时的复制到备份服务器
角色 | 外网 IP(NAT) | 内网 IP(LAN) | 安装工具 |
web01 | eth0:10.0.0.7 | eth1:172.16.1.7 |
|
nfs | eth0:10.0.0.31 | eth1:172.16.1.31 | rsync+inotify+sersync |
backup | eth0:10.0.0.41 | eth1:172.16.1.41 | rsync-server |
3.1 配置 backup 服务器
3.1.1 安装 rsync
[root@backup ~]# yum install rsync -y
3.1.2 配置 rsync
- [root@backup ~]# cat /etc/rsyncd.conf
- uid = rsync
- gid = rsync
- port = 873
- fake super = yes
- use chroot = no
- max connections = 200
- timeout = 600
- ignore errors
- read only = false
- list = true
- secrets file = /etc/rsync.password
- auth users = rsync_backup
- log file = /var/log/rsyncd.log
- #####################################
- [backup]
- path = /backup
- [data]
- path = /data
3.1.3 创建数据目录
两个模块所以创建两个目录
- [root@backup ~]# mkdir /backup
- [root@backup ~]# mkdir /data
3.1.4 创建用户并授权
创建一个虚拟用户 rsync
将 backup 目录所属用户和组修改为 rsync
- [root@backup ~]# useradd -M -s /sbin/nologin rsync
- [root@backup ~]# chown -R rsync.rsync /backup//data/
- [root@backup ~]# ll -d /backup//data/
drwxr-xr-x 7 rsync rsync 256 9 月 7 08:52 /backup/
drwxr-xr-x 2 rsync rsync 6 9 月 7 11:44 /data/
3.1.5 创建链接的虚拟用户
- [root@backup ~]# echo "rsync_backup:1" /etc/rsync.password
- [root@backup ~]# chmod 600 /etc/rsync.password
3.1.6 启动 rsyncd
[root@backup ~]# systemctl restart rsyncd
3.2 配置 nfs 服务器
3.2.1 安装 nfs
[root@nfs ~]# yum install nfs-utils -y
3.2.2 配置 nfs
- [root@nfs ~]# cat /etc/exports
- /data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
3.2.3 配置 nfs 依赖环境
- [root@nfs ~]# groupadd -g 666 www
- [root@nfs ~]# useradd -u 666 -g 666 www
- [root@nfs ~]# mkdir /data
- [root@nfs ~]# chown -R www.www /data
3.2.4 启动 nfs
- [root@nfs ~]# systemctl enable rpcbind nfs-utils
- [root@nfs ~]# systemctl start rpcbind nfs-server
3.3 配置 web 服务器
3.3.1 安装工具包
- [root@web01 ~]# yum install nfs-utils -y
- [root@web01 ~]# systemctl start rpcbind
3.3.2 创建用于挂载的目录
[root@web01 ~]# mkdir /data
3.3.3 挂载 nfs 的 data 目录
- [root@web01 ~]# showmount -e 172.16.1.31
- Export list for 172.16.1.31:
- /data 172.16.1.0/24
- [root@web01 ~]# mount -t nfs 172.16.1.31:/data /data
3.4 检查以上操作
通过 windows 上传一个视频或图片至 web 服务器的 / data 目录
验证内容是否存在 nfs 服务器
- [root@nfs ~]# ls /data/
- 1111 cad88c2e57f44e93b664a48a98a47108_th.jpg tes1 test
3.5 配置实时同步
为了防止 nfs down 机后丢失数据, 希望 nfs 共享的 data 目录一旦发生变化, 则实时的同步至 backup 服务器
操作如下:
在 nfs 服务器的操作
3.5.1 安装 inotify-tools
[root@nfs ~]# yum install inotify-tools rsync -y
3.5.2 安装 sersync
[root@nfs ~]# wget https://raw.githubusercontent.com/wsgzao/sersync/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
3.5.3 解压并重命名
- [root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
- [root@nfs ~]# mv GNU-Linux-x86//usr/local/sersync
3.5.4 配置 sersync 配置文件
以下为要修改的内容
- [root@nfs01 sersync]# vim confxml.xml
- 5 <fileSystem xfs="true"/> <!-- 文件系统 -->
- 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="true"/>
- 16 <closeWrite start="true"/>
- 17 <moveFrom start="true"/>
- 18 <moveTo start="true"/>
- 19 <attrib start="false"/>
- 20 <modify start="false"/>
- 21 </inotify>
- 23 <sersync>
- 24 <localpath watch="/data"> <!-- 监控的目录 -->
- 25 <remote ip="172.16.1.41" name="data"/> <!-- backup 的 IP 以及模块 -->
- 28 </localpath>
- 29 <rsync> <!-- rsync 的选项 -->
- 30 <commonParams params="-az"/>
- 31 <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.pass"/>
- 32 <userDefinedPort start="false" port="874"/><!-- port=874 -->
- 33 <timeout start="true" time="100"/><!-- timeout=100 -->
- 34 <ssh start="false"/>
- 35 </rsync>
- <!-- 每 60 分钟执行一次同步 -->
- 36 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--def
- ault every 60mins execute once-->
- View Code
3.5.5 创建密码文件
- [root@nfs01 sersync]# echo "1"> /etc/rsync.pass
- [root@nfs ~]# chmod 600 /etc/rsync.pass
3.5.6 启动 sersync
启动 sersync
- [root@nfs ~]# /usr/local/sersync/sersync2 -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@nfs ~]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
注意: 如果发生错误, 请手动执行命令检查推送是否正常
- [root@nfs ~]# cd /data && rsync -avz -R --delete ./ --timeout=100 rsync_backup@172.16.1.41::data --password-file=/etc/rsync.pass
- View Code
3.6 nfs 服务器宕机换挂载点
如果 nfs 现在 down 机了, 希望将 web 客户端挂载至 backup 服务器上? 怎么实现?
nfs 和 backup 两台服务器应该保持一样 (nfs 配置. nfs 共享的目录. nfs 的权限)
- [root@backup ~]# yum install nfs-utils -y
- [root@backup ~]# rsync -avz root@172.16.1.31:/etc/exports /etc/
- [root@backup ~]# groupadd -g 666 www
- [root@backup ~]# useradd -u666 -g666 www
启动 nfs
- [root@backup ~]# systemctl start rpcbind
- [root@backup ~]# systemctl start nfs-server
修改 rsync 的权限 vim /etc/rsyncd.conf
- uid = www
- gid = www
修改授权
[root@backup ~]# chown -R www.www /data//backup/
重启 rsync
[root@backup ~]# systemctl restart rsyncd
模拟 nfs 故障 (挂起虚拟机)
web 强制卸载 172.16.1.31:/data
[root@web01 ~]# umount -lf /data
web 尝试挂载 172.16.1.41:/data
[root@web01 ~]# mount -t nfs 172.16.1.41:/data /data/
第 4 章 serync 扩展补充
4.1 下载 serync 的方法
serync 使用 yum 是无法下载的 serync 是一个下载解压即用的软件包
sersync 项目地址: https://github.com/wsgzao/sersync
找到下载的地方 下载后直接上传到 linux 服务器里 解压后即用
直接复制下载地址 在 linux 内下载
[root@nfs ~]# wget https://raw.githubusercontent.com/wsgzao/sersync/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
4.2 sersync 配置文件详解
confxml.xml 文件的内容
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <head version="2.5">
- #hostip 本机的 Ip 地址, 后台使用那个端口来进行实时备份
- <host hostip="localhost" port="8008"></host>
- #是否开启 debug 调试信息
- <debug start="false"/>
- #是否支持 xfs 文件系统, 如果有分区文件系统为 xfs 的时候, 需要开启这个选项
- <fileSystem xfs="false"/>
- #是否开启过滤, 排除
- <filter start="false">
- #exclude 排除这些文件名中包含这些字符的, 不推送这些
- <exclude expression="(.*)\.svn"></exclude>
- <exclude expression="(.*)\.gz"></exclude>
- <exclude expression="^info/*"></exclude>
- <exclude expression="^static/*"></exclude>
- </filter>
- #inotify 模块的配置
- <inotify>
- #是否开启完全同步, 源文件里面有什么, 目标文件里面就有什么, 特别危险, 不开启
- <delete start="false"/>
- #是否监控目录, 如果不开启他将不监控这个目录下的子文件和子目录的更改, 除非有特殊要求, 否则必须开启
- <createFolder start="true"/>
- #创建文件就监控, 我们一般把这个设为 false, 否则太消耗性能
- <createFile start="false"/>
- #文件关闭的时候就推送, 这个开启, 新建文件打开这个就会对上面的进行备份了
- <closeWrite start="true"/>
- #mv 剪切走
- <moveFrom start="true"/>
- #mv 剪切到这个文件里面
- <moveTo start="true"/>
- #更改文件隐藏属性
- <attrib start="false"/>
- #监控权限更改就会推送, 一般选择开启
- <modify start="true"/>
- </inotify>
- <sersync>
- #本地要备份的目录
- <localpath watch="/backup">
- #远程 rsync 的地址, 和使用的模块
- <remote ip="10.0.0.61" name="backup_crond"/>
- <!--<remote ip="192.168.8.39" name="tongbu"/>-->
- <!--<remote ip="192.168.8.40" name="tongbu"/>-->
- </localpath>
- <rsync>
- #rsync 推送的使用的参数
- <commonParams params="-az"/>
- #是不是开启用户认证, 使用的那个用户, 密码文件什么, 密码文件权限必须是 600
- <auth start="true" users="cron_backup" passwordfile="/etc/rsync_passwd_cron.conf"/>
- #服务端使用的那个端口, 端口地址是什么
- <userDefinedPort start="false" port="874"/><!-- port=874 -->
- #推送超时时间, 超过这个时间就认为推送不成功.
- <timeout start="true" time="300"/><!-- timeout=100 -->
- <ssh start="false"/>
- </rsync>
- <failLog path="/tmp/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>
- View Code
来源: https://www.cnblogs.com/wenrulaogou/p/9605422.html