rsync 数据同步和sersync数据实时同步服务器
基于三台服务器,部署数据实时同步服务器,一台作为主服务器sersync(推送数据),其余两台作为备用服务器rsync(拉取数据)。
rsync 数据同步
环境:
服务器:172.20.26.167
服务器:172.20.26.198
服务器:172.20.26.24
实现有权限的地址可同步
两台服务器均需安装有rsync服务。
在172.20.26.198上检查是否有安装rsync 服务
[root@localhost ~]# rpm -qc rsync #查询rsync服务的配置文件
未安装软件包 rsync
[root@localhost ~]# yum install rsync -y

[root@localhost ~]# rpm -qc rsync #查询rsync 服务的配置文件
/etc/rsyncd.conf
/etc/sysconfig/rsyncd
[root@localhost ~]# vim /etc/rsyncd.conf #守护模式的配置文件rsyncd.conf,在后台运行的进程,不影响前端的shell

把光标移动到uid行首,ctrl+v 进入可视化模式,按向右键,再按向下键,再按x键,去掉#号注释,保留有效参数
修改相关配置参数:
uid = root rsync #进程管理用户
gid = root rsync #进程管理用户
use chroot = yes #安全设置,限制软连接,如果设置为no,则文件同步到远程服务器后,会在链接文件前面增加一个目录,使链接文件失效,设置为yes则表示不加目录。
max connections = 10 #最大链接数
ignore nonreadable = yes #忽略不可读的错误
read only = false #关闭只读权限
hosts allow = 172.20.26.24 #允许同步的服务器
hosts deny = * #不允许同步的服务器,* 代表所有
auth users = rsync #创建同步时使用的虚拟用户
secrets file = /etc/rsync.passwd #将密码写入rsync.passwd 文件,使用该虚拟用户同时时不需要再输入密码
编辑【web】模块、【sql】模块
[web]
path = /data/web
comment = nginx data backup
[sql]
path = /data/sql
comment = sql data backup

[root@localhost ~]# systemctl start rsyncd
[root@localhost ~]# systemctl status rsyncd

创建配置文件设置的目录路径
[root@localhost ~]# mkdir -p /data/web
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# mkdir -p /data/sql
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]#
[root@localhost /]# cp /etc/fstab /data/web #将/etc/fstab 文件拷贝到/data/web 目录下

在172.20.26.24服务器上也安装rsync 服务

[root@bogon ~]# systemctl start rsyncd #启动rsync服务
从172.20.26.198服务器上拉取web 模块中的文件到当前目录下
[root@bogon ~]# rsync -av 172.20.26.198::web .

也可以将本机上的文件(/etc/passwd)推送给172.20.26.198上的web模块里,因在172.20.26.198上的rsync.conf文件中配置了"read only = false 关闭只读权限",否则会报错。

在172.20.26.198上查询推送过来的passwd文件

如果需要开启认证的话,需要定义虚拟用户和密码,配置到rsync.conf 文件中
[root@localhost /]# vim /etc/rsyncd.conf

如果使用的用户名和密码一样,可以写在全局里,也可以写到模块里,如果不一样,分别写到自己的模块里即可。

[root@localhost /]# echo "rsync:123456" >/etc/rsync.passwd
[root@localhost /]# chmod 600 /etc/rsync.passwd #修改rsync.passwd 文件的权限,其他人和组不可读写

重启rsync 服务
[root@localhost /]# systemctl restart rsyncd
[root@bogon ~]# rsync -av /etc/yum.conf rsync@172.20.26.198::web
输入rsync 的密码 123456

在172.20.26.198上查询/data/web 目录下已经有yum.conf 文件

也可以将密码写进文件里,使用这个用户同步数据时不再需要输入密码。
[root@bogon ~]# echo "123456" > /etc/rsync.passwd #将密码写入rsync.passwd 文件中
[root@bogon ~]# chmod 600 /etc/rsync.passwd #取消其他人和组对rsync.passwd 文件读取权限

[root@bogon ~]# rsync -av /etc/virc rsync@172.20.26.198::web --password-file=/etc/rsync.passwd

在172.20.26.198上查询/data/web 目录下已经有virc 文件。

也可以设置允许某些服务器连接同步,拒绝其他的服务器连接同步。
目前172.20.26.167 服务器是可以将文件推送到172.20.26.198服务器上的,而我们可以设置只允许172.20.26.24 连接同步,拒绝172.20.26.167连接同步。

[root@localhost ~]# vim /etc/rsyncd.conf

[root@localhost ~]# systemctl restart rsyncd
在172.20.26.167上推送sysctl.conf文件到172.20.26.198上,被拒绝
[root@bogon ~]# rsync -av /etc/sysctl.conf rsync@172.20.26.198::web

我们修改172.20.26.198上的/etc/rsyncd.conf文件,把172.20.26.167 写到允许的hosts 里,即可让172.20.26.167也能向172.20.26.198服务器推送文件
[root@localhost ~]# vim /etc/rsyncd.conf

保存退出,重启rsync服务
[root@localhost ~]# systemctl restart rsyncd
回到172.20.27.167服务器上执行rsync命令,
[root@bogon ~]# rsync -av /etc/sysctl.conf rsync@172.20.26.198::web

将syssctl.conf 推送到172.20.26.198服务器上的web模块中

我们在/etc/rsyncd.conf 里也配置了sql 模块,也创建了/data/sql 目录,我们也可以用这个模块进行实验。
rsync -av /etc/virc rsync@172.20.26.198::sql --password-file=/etc/rsync.passwd


sersync 实时数据同步
环境:
sersync服务器:172.20.26.24 配置sersync 把数据推给167、198
rsync服务器:172.20.26.167、172.20.26.198 配置rsync 接收数据
先在172.20.26.24服务器上配置sersync 服务
[root@bogon ~]# cd /usr/src
[root@bogon src]# rz
将sersync2.5.4_64bit_binary_stable_final.tar上传到172.20.26.24上,配置sersync服务

[root@bogon src]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
将解压出来的GNU-Linux-x86目录移到/usr/local/sersync
[root@bogon src]# mv GNU-Linux-x86/ /usr/local/sersync
[root@bogon src]# cd /usr/local/sersync
[root@bogon sersync]# ls
confxml.xml sersync2

/usr/local/sersync下只有confxml.xml、sersync2两个文件,一个是配置文件、一个可执行文件
[root@bogon sersync]# vim /usr/local/sersync/confxml.xml
将<fileSystem xfs="false"/> 改为 <fileSystem xfs="true"/>
Inotify 模块定义通知开始同步的条件:
<inotify>
<delete start="true"/> #开始删除的时候,默认为true
<createFolder start="true"/> #创建目录的时候,默认为true
<createFile start="false"/> #创建文件的时候,默认为false
<closeWrite start="true"/> #关闭写的时候,默认为true
<moveFrom start="true"/> #从---移动到的时候,默认为true
<moveTo start="true"/> #开始移动的时候,默认为true
<attrib start="false"/> #查询开始,默认为false
<modify start="false"/> #修改开始的时候,默认为false
</inotify>

配置监控的目录,远程服务器IP地址,要监控的模块,我们之前定义设定的是web

将超时时间改为true
<timeout start="true" time="100"/><!-- timeout=100 -->
计划任务默认是false ,如果改为true,默认是600分钟做一次全量同步
<crontab start="false" schedule="600"><!--600mins-->
开启认证,配置使用rsync 和密码文件
<auth start="true" users="rsync" passwordfile="/etc/rsync.passwd"/>
保存退出
[root@bogon sersync]# mkdir -p /data/jfedu #创建jfedu目录
[root@bogon sersync]# ./sersync2 -d -r -o /usr/local/sersync/confxml.xml #启动sersync 服务
检查172.20.26.167、172.20.26.198 rsync服务器的服务和目录是否都正常
由于172.20.26.167服务器上没有部署rsync ,可以从172.20.26.198上将rsync的文件拷贝过来
[root@bogon etc]# scp 172.20.26.198:/etc/rsync* /etc/

拷贝过来后检查一下rsyncd.conf 没有问题,重启服务
[root@bogon /]# chmod 600 /etc/rsync.passwd #取消其他人和组对rsync.passwd 文件读取权限
创建/data/web 目录
172.20.26.198服务器上rsync 服务和目录都正常
回到172.20.26.24服务器上开始更新/data/jfedu里的数据
[root@bogon sersync]# cp /etc/fstab /data/jfedu/

172.20.26.167服务器上查看data/web 目录下已经有了fstab 文件

172.20.26.198服务器上查看data/web 目录下也已经有了fstab 文件

我们把/etc/passwd 也拷贝到/data/jfedu ,查看同步情况



对passwd文件内容进行修改,查看同步情况
删除 root:x:0:0:root:/root:/bin/bash 这行信息


保存退出
在172.20.26.167和172.20.26.198上查看passwd文件里是已经没有刚才删除的信息:root:x:0:0:root:/root:/bin/bash

[root@bogon etc]# vim /data/web/passwd

[root@bogon sersync]# tail /tmp/rsync_fail_log.sh #可以查看运行日志
查看sersync 进程
