52

使用rsync+sersync实现文件同步实时备份

 5 years ago
source link: https://www.helloweba.net/server/589.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

为什么选用Rsync+sersync

1.sersync是基于Inotify开发的,类似于Inotify-tools的工具。

sersync可以记录被监听目录中发生变化的(包括增删改)具体某一个文件或某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的这个文件或者这个目录。

2.rsync在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变化的数据相对整个同步目录数据来说是很小的,rsync在遍历查找比对文件时,速度很快),因此,效率很高。

当同步的目录数据量不大时,建议使用Rsync+Inotify-tools;当数据量很大(几百G甚至1T以上)、文件很多时,建议使用Rsync+sersync。

准备环境

服务器A:192.168.11.31 源服务器。

服务器B: 192.168.11.34 目标服务器。

我们要实现的就是把A服务器上的文件同步到B服务器上,从而实现备份。我们主要是在B服务器上安装配置rsync,在A服务器上安装配置sersync,通过sersync把文件推送到B服务器上。

服务器系统环境为CentOS7.4。

系统基础配置

1.修改inotify默认参数

inotify默认内核参数值太小,我们需要手动修改参数:

sysctl -w fs.inotify.max_queued_events="99999999"
sysctl -w fs.inotify.max_user_watches="99999999"
sysctl -w fs.inotify.max_user_instances="65535"

vi /etc/sysctl.conf #添加以下代码

fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
:wq! #保存退出

参数说明:

max_queued_events:inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确

max_user_watches:要同步的文件包含多少目录,可以用:find /data/file -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/data/file为同步文件目录)

max_user_instances:每个用户创建inotify实例最大值

2.关闭selinux

在/etc/sysconfig/selinux 这个文件,设置 SELINUX=disable

3.防火墙开通873端口

firewall-cmd --zone=public --add-port=873/tcp --permanent
firewall-cmd --reload

以上A、B两台服务器都需要配置。

部署rsync

我们需要在源服务器A和目标服务器B上都要部署rsync。

安装rsync:

yum install -y rsync

配置rsync:

[root@localhost ~]# vim /etc/rsyncd.conf
uid = root
gid = root
use chroot =no
list = no
log file = /var/log/rsyncd.log

[helloweba]
path=/data/file
comment=helloweba file backup
ignore errors=yes
read only=no
list=no
max connections=2000
timeout=200
auth users=rsync
secrets file=/etc/rsync.pas
hosts allow=*
#host deny=0.0.0.0/0

auth users 配置同步验证的用户名。

secrets file 这个是配置同步的密码文件的。

[helloweba] 这个是配置同步模块的名称。

path 是配置同步的目录。

hosts allow 是允许同步的主机。

hosts deny 拒绝同步的主机。

创建同步的用户与密码的文件

将用户名和密码写入密码文件中,同样要设置这个文件的权限为600。

echo "rsync:123456" >> /etc/rsync.pas
chmod 600 /etc/rsync.pas

创建同步的目录

按照配置文件中的 path 创建对应的目录:

mkdir /data/file

启动和管理rsync

启动rsync:

systemctl start rsyncd

将rsync设置为开机自启动:

systemctl enable rsyncd

如果要停止和重启rsync可以使用以下命令:

systemctl stop|restart rsyncd

启动好rsync服务后,我们可以使用 netstat -tunpl | grep :873 查看服务端口873是否正常。

源服务器A部署sersync

安装和配置rsync

源服务器A也需要安装rsync,方法同上述目标服务器B部署rsync一样。

下载sersync

在google code ( http://code.google.com/p/sersync/downloads/list )下载sersync的可执行文件版本,里面有配置文件和可执行文件

不能google的同学可以到http://sersync.sourceforge.net/ 下载旧版本。

安装sersync

tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
mv GNU-Linux-x86 /usr/local/sersync

我们下载的是二进制版,无需编译安装,直接就可使用。

我们可以规范下目录结构:

cd /usr/local/sersync/
mkdir conf bin logs
mv confxml.xml conf/
mv sersync2 bin/

最后目录结构应该是这样的:

.
|-- bin
|   `-- sersync2
|-- conf
|   `-- confxml.xml
`-- logs

创建密码文件

和目标服务器一样,不过这个文件只要保存密码即可,不需要保存用户名,权限也是600

echo "123456" >> /etc/rsync-client.pas
chmod 600 /etc/rsync-client.pas

配置sersync

直接编辑sersync的配置文件:

vim /usr/local/sersync/conf/confxml.xml

要注意几处需要修改的地方:

1.定义同步目录和目标服务器ip及模块名称。

<localpath watch="/data/file">   # 定义本地要同步的目录
    <remote ip="192.168.11.34" name="helloweba"/>  # 定义要同步的服务器IP和模块名
    <!--<remote ip="192.168.8.39" name="tongbu"/>-->
    <!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>

2.配置rsync同步指令及同步认证。

<rsync>
    <commonParams params="-artuzP"/> # 定义同步指令
    <auth start="true" users="rsync" passwordfile="/etc/rsync-client.pas"/> #启用同步认证,指定用户名及密码文件
    <userDefinedPort start="false" port="873"/><!-- port=874 --> #定义远程端口
    <timeout start="false" time="100"/><!-- timeout=100 -->
    <ssh start="false"/>
</rsync>

启动和管理sersync服务

我们需要自己制作一个服务管理脚本,新建sersync.service文件: vim /usr/lib/systemd/system/sersync.service ,添加以下代码:

[Unit]
Description=sersync service
After=network.target

[Service]
Type=forking
LimitNOFILE=65535
ExecStart=/usr/local/sersync/bin/sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml
ExecReload=/bin/kill -USR1 $MAINPID
Restart=always

[Install]
WantedBy=multi-user.target graphical.target

保存,然后启动sersync服务:

systemctl start sersync.service

加入到启动项随系统启动:

systemctl enable sersync.service

可以使用以下命令停止和重启sersync服务:

systemctl stop|restart sersync.service

验证

服务都搭建好了,现在最后一步就是验证测试,我们在源服务器A的/data/file目录下(如果不存在则手动创建目录),执行新建文件、删除文件、新建文件夹等操作后,再到目标服务器B的/data/file目录下看看是否也存在相同的文件和文件夹。如果一切正常,那么恭喜实时同步配置成功了。

以上我们就做好了文件双机自动同步备份,作者亲测并应用在实际项目中,这样就不怕文件丢失了。

后记

有的项目中,我们希望做到双机双向自动同步备份,就像 《MySQL/MariaDB主主数据同步配置》 一样,两台服务器既是源服务器又是目标服务器,这个时候只需在两台服务器上都部署rsync和sersync服务,配置和本文配置一毛一样。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK