针对本地文件的修改, 自动同步到远程文件夹, 远程备份很方面. 研究了下大家的主流同步方案一般是 rsync+inotify 和 rsync+sersync, 本文这里使用 sersync 的方案做为实验.
基本大纲:
1. 基本原理概述
2. 实验环境准备
3. 实验的操作步骤以及结论
一: 基本原理概述
我这里的目的是为了远程同步 mysql 的备份文件, centos6.6 主机同步到另一台 centos6.6 主机
使用 rsync+sersync 做实时同步时, 用于推送文件 的服务器运行 sersync 服务, 用于接收文件的服务器则运行 rsync 守护进程, 简单来说就是 sersync 会利用 rsync 命令将文件推送到 rsync 服务器, 实际线上使用一般会把 sersync 作为主服务器, rsync 作为镜像服务器, 实现数据同步备份, web 镜像等功能
所以两台机器都需要 rsync, 然后有对应的写数据权限.
二: 实验环境准备
1. 操作系统 centos6.6 x86
2.rsync yum 源
3.sersync2.5_64bit 软件包
4. 推送源机 A 192.168.1.8(ip 均为实验伪造)
5. 接收机 B 192.168.1.35
6. 把 A 机器上的一个目录下文件的变化同步到 B 机器上, 两边文件保持一致.
7. 接收机 B selinux 与 iptables 自行设置好, rsync 的端口两边要配置一致, 默认是 873
三: 实验的操作步骤以及结论
接收机上的操作:
1, 查看 rsync 是否安装
- [root@localhost ~]# rpm -qa | grep rsync
- rsync-3.0.6-12.el6.x86_64
- # 如果没有安装 yum install -y rsync 就行了
2,rsync 配置
- [root@localhost ~]# cat /etc/rsyncd.conf
- uid = root
- gid = root
- use chroot =no
- list = no
- log file = /var/log/rsyncd.log
- [rsyncmysql]
- path=/data/backup/ywximysql
- comment=rsyncmysql
- ignore errors
- read only=no
- list=no
- max connections=200
- timeout=600
- auth users=rsync
- secrets file=/etc/rsync.passwd
- hosts allow=*
- #host deny=0.0.0.0/0
配置文件注解:
secrets file 这个是配置同步的密码文件的.
[rsynctest] 这个是配置同步模块的名称, 后面会用
path 是配置同步的目录
hosts allow 是允许同步的主机 这里是测试就没有限制
hosts deny 拒绝同步的主机
3, 创建同步的用户与密码的文件, 即上图中的 secrets file 这个配置选项中的文件./etc/rsync.passwd, 同进要设置这个文件的权限为 600
- [root@localhost ~]# echo "rsync:123456ywxi" >> /etc/rsync.passwd
- [root@localhost ~]# chmod 600 /etc/rsync.passwd
4, 创建同步的目录: 即上面中 path 配置选项中的目录
[root@localhost ~]# mkdir -p /data/backup/ywximysql
5, 启动 rsync
- [root@localhost ~]# rsync --daemon --config=/etc/rsyncd.conf
- [root@localhost ~]# ps -ef | grep rsync
- root 3358 1 0 14:37 ? 00:00:00 rsync --daemon --config=/etc/rsyncd.conf
- root 3360 3184 0 14:37 pts/1 00:00:00 grep rsync
接着重启一下 xinetd 使得配置生效
- [root@localhost ~]# /etc/init.d/xinetd restart
- Stopping xinetd: [FAILED]
Starting xinetd: [ OK ]
这里是通过 xinetd 服务来重启 rsync, 也可以不用这种方式, 如果没有可以用 yum 直接安装
6, 配置开机启动
[root@localhost ~]# echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.d/rc.local
推送源机上的操作:
1, 先到 sersync 官网的地址已经不能使用了, 我从网上找了一份放到其他地址, github 上的源码镜像
我把二进制包下载也放到了上面
- # 命令集如下:
- wget --no-check-certificate https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz
- mkdir /usr/local/sersync
- mkdir /usr/local/sersync/conf
- mkdir /usr/local/sersync/bin
- mkdir /usr/local/sersync/log
- tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
- cd GNU-Linux-x86/
- cp confxml.xml /usr/local/sersync/conf
- cp sersync2 /usr/local/sersync/bin
2, 创建密码文件, 同 B 服务器一样, 不过这个文件只要保存一个密码就行了, 不用用户名, 权限也是 600
- [root@localhost tools]# echo "123456ywxi" >> /etc/rsync.passwd
- [root@localhost tools]# chmod 600 /etc/rsync.passwd
3, 配置 sersync, 配置文件就是上第二步复制的 confxml.xml 这个文中, 路径在 / usr/local/sersync/conf 中
- [root@localhost conf]# cat confxml.xml
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <head version="2.5">
- # 设置本地 IP 和端口
- <host hostip="localhost" port="8008"></host>
- # 开启 DUBUG 模式
- <debug start="false"/>
- # 开启 xfs 文件系统
- <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="true"/>
- <closeWrite start="true"/>
- <moveFrom start="true"/>
- <moveTo start="true"/>
- <attrib start="true"/>
- <modify start="true"/>
- </inotify>
- <sersync>
- # 本地同步的目录路径
- <localpath watch="/home/data">
- # 远程 IP 和 rsync 模块名
- <remote ip="192.168.1.35" name="rsyncmysql"/>
- <!--<remote ip="192.168.8.39" name="tongbu"/>-->
- <!--<remote ip="192.168.8.40" name="tongbu"/>-->
- </localpath>
- <rsync>
- # rsync 指令参数
- <commonParams params="-auvzP"/>
- # rsync 同步认证
- <auth start="true" users="rsync" passwordfile="/etc/rsync.pas"/>
- # 设置 rsync 远程服务端口, 远程非默认端口则需打开自定义
- <userDefinedPort start="false" port="873"/><!-- port=874 -->
- # 设置超时时间
- <timeout start="true" time="100"/><!-- timeout=100 -->
- # 设置 rsync+ssh 加密传输模式, 默认关闭, 开启需设置 SSH 加密证书
- <ssh start="false"/>
- </rsync>
- # sersync 传输失败日志脚本路径, 每隔 60 会重新执行该脚本, 执行完毕会自动清空.
- <failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
- # 设置 rsync+crontab 定时传输, 默认关闭
- <crontab start="false" schedule="600"><!--600mins-->
- <crontabfilter start="false">
- <exclude expression="*.php"></exclude>
- <exclude expression="info/*"></exclude>
- </crontabfilter>
- </crontab>
- # 设置 sersync 传输后调用 name 指定的插件脚本, 默认关闭
- <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="xxx"/>
- <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>
4, 创建同步目录:
mkdir /home/data
5, 设置环境变量:
- echo "export PATH=$PATH:/usr/local/sersync/bin/" >> /etc/profile
- source /etc/profile
6, 启动 sersync
- sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml
- # 重启操作如下:
- # killall sersync2 && sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml
7, 设置开机启动
echo "sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml" >> /etc/rc.local
启动了 sersync 服务之后
推送源机操作:
- [root@localhost ~]# cd /home/data/
- [root@localhost data]# ls
- [root@localhost data]# touch backup.sql
- [root@localhost data]# touch test.sql
- [root@localhost data]# ll
- total 0
- -rw-r--r-- 1 root root 0 Jun 5 06:00 backup.sql
- -rw-r--r-- 1 root root 0 Jun 5 06:15 test.sql
- [root@localhost data]# echo "ywxitest">> test.sql
接收机操作:
- [root@localhost ywximysql]# ll
- total 0
- -rw-r--r--. 1 root root 0 Jun 5 06:00 backup.sql
- -rw-r--r--. 1 root root 0 Jun 5 06:15 test.sql
- [root@localhost ywximysql]# cat test.sql
- ywxitest
实验结论 :
可以看到文件很快的就被同步到远程机器上了, 文件创建时间一致, 实验成功.
[sersync+rsync] 文件同步部署实战
来源: http://www.bubuko.com/infodetail-2631478.html