使用Rsync+sersync实现数据实时同步

目录

一、rsync概述

二、rsync运行原理

三、rsync部署

四、备份测试

五、rsync+sersync 实现数据实时同步

5.1、数据同步原理

5.2 、部署rsync+sersync

一、rsync概述

Rsync(Remote Sync)是Linux系统下的数据镜像备份工具。该工具可以实现远程同步、不同主机之间的同步,也能实现全量备份增量备份,保持数据链接和权限,并采用优化的同步算法,传输前对数据进行压缩,故该工具非常适合架构集中式备份异地备份,也支持本地复制。

官网地址:https://rsync.samba.org/

优点:

  • scp无法备份大量数据,而rsync备份、统计、比较一起进行。

  • 可以备份整个目录树和文件系统,并保持文件原来的权限、时间、软硬链接。

  • 安装较容易,无需特殊权限。

  • 同步快速,首次同步完全备份,再次同步增量备份。

  • 可以使用scp和ssh等方式传输备份文件

  • 支持匿名传输

  • 选择性保持:符号链接、硬链接、文件属性、权限、时间等

  • 传输速度快:压缩再传输、解压再使用,减少带宽。

备份分类:

  • 完全备份:所有文件进行备份

  • 差异备份:备份自上次完全备份以来所有的修改

  • 增量备份:备份自上次备份依赖所作的修改

二、rsync运行原理

rsync采用C/S模式,即点到点的传输。通过xinetd服务监听873端口,再让xinetd服务下的rsync服务作出响应。

源主机:需要同步数据的服务器

目标主机:存放服务器同步数据的主机

数据同步方式:push 和 pull

  • 推push:主动同步,把数据发送给目标主机。服务器开销大,适合后端服务器较少的情况。【服务器备份推给rsync客户端存放,主动模式】

目的主机配置为 rsync 服务端,源主机周期性的使用 rsync 命令把要同步的目录推过去。

  • 拉pull:所有客户端主机去服务器上面拉数据,导致数据传输缓慢。【rsync客户端去服务器上拉数据,存放到客户端上,被动模式】

源主机配置为 rsync 服务端,目的主机周期性的使用 rsync 命令把要同步的目录拉过来。

三、rsync部署

  • rsync服务由xinetd服务进行管理,故也需要安装xinetd服务
  • 客户端和服务器都需要安装xinetd服务和rsync服务
[root@source-server ~]# yum install -y rsync
local                                                  3.4 MB/s | 3.5 kB     00:00    
Package rsync-3.2.7-5.oe2403sp1.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!

[root@source-server ~]# yum install -y xinetd
Last metadata expiration check: 0:02:33 ago on 2025年09月19日 星期五 14时38分43秒.
Dependencies resolved.
=======================================================================================
 Package         Architecture    Version                          Repository      Size
=======================================================================================
Installing:
 xinetd          x86_64          2:2.3.15-35.oe2403sp1            local           82 k

Transaction Summary
=======================================================================================
Install  1 Package

Total size: 82 k
Installed size: 193 k
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                               1/1 
  Installing       : xinetd-2:2.3.15-35.oe2403sp1.x86_64                           1/1 
  Running scriptlet: xinetd-2:2.3.15-35.oe2403sp1.x86_64                           1/1 
Created symlink /etc/systemd/system/multi-user.target.wants/xinetd.service → /usr/lib/systemd/system/xinetd.service.

  Verifying        : xinetd-2:2.3.15-35.oe2403sp1.x86_64                           1/1 

Installed:
  xinetd-2:2.3.15-35.oe2403sp1.x86_64                                                   

Complete!
  • 查看是否监听873号端口
[root@backup-server ~]# systemctl start rsyncd
[root@backup-server ~]# netstat -anpt | grep 873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      6229/rsync          
tcp6       0      0 :::873                  :::*                    LISTEN      6229/rsync   

用法:

--port指定端口号,默认873
--delete删除那些目标位置有的文件而备份源没有的文件,最大程度的保持一致
-avz常用:保留权限、显示同步过程、压缩传输

四、备份测试

  • 服务端(需要进行数据备份的主机) 上创建测试目录/data,并写入数据
[root@source-server ~]# mkdir /data
[root@source-server ~]# cd /data/
[root@source-server data]# for i in {1..10}; do touch $i.txt && mkdir dir$i; done
[root@source-server data]# ls
10.txt  2.txt  4.txt  6.txt  8.txt  dir1   dir2  dir4  dir6  dir8
1.txt   3.txt  5.txt  7.txt  9.txt  dir10  dir3  dir5  dir7  dir9
  • 客户端(备份存储的主机) 上创建备份存放目录/backup,并编写rsync配置文件
[root@backup-server ~]# mkdir /backup
 
[root@backup-server ~]# vim /etc/rsyncd.conf 
port=873
address = 192.168.100.128     # 监听地址,一般是目标主机的IP地址
uid = root
gid = root
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
motd file = /etc/rsyncd.motd
hosts allow = 192.168.100.0/24   # 允许同步的主机
[backup]
path = /backup
comment = bakcup data
read only = false
list = yes
auth users = rsyncuser
secrets file = /etc/rsync.passwd
  •  创建欢迎信息文件
[root@backup-server ~]# vim /etc/rsyncd.motd
Welcome to Backup Server!

  • 创建密码文件,格式   用户名:密码
[root@backup-server ~]# vim /etc/rsync.passwd
rsyncuser:123456
  • 设置密码文件权限为600
[root@backup-server backup]# chmod 600 /etc/rsync.passwd

[root@backup-server etc]# ls -l rsync.passwd 
-rw-------. 1 root root 17  9月19日 16:06 rsync.passwd
  • pull拉取数据进行同步
[root@backup-server ~]# rsync root@192.168.100.253:/data/* /backup/
The authenticity of host '192.168.100.253 (192.168.100.253)' can't be established.
ED25519 key fingerprint is SHA256:31rlqlsTnd0HoJKaXEOLDXR1zPtZ2+W94bYXeJJKN3Q.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.100.253' (ED25519) to the list of known hosts.

Authorized users only. All activities may be monitored and reported.
root@192.168.100.253's password: 
skipping directory dir1
skipping directory dir10
skipping directory dir2
skipping directory dir3
skipping directory dir4
skipping directory dir5
skipping directory dir6
skipping directory dir7
skipping directory dir8
skipping directory dir9

[root@backup-server ~]# cd /backup/
[root@backup-server backup]# ls
10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt
--delete删除那些目标位置有的文件而备份源没有的文件,最大程度的保持一致
-avz常用:保留权限、显示同步过程、压缩传输
[root@backup-server backup]# touch 12124234
[root@backup-server backup]# touch fgwegqeh
[root@backup-server backup]# ls
10.txt  12124234  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt  fgwegqeh

[root@backup-server backup]# rsync -avz --delete root@192.168.100.253:/data/ /backup/

Authorized users only. All activities may be monitored and reported.
root@192.168.100.253's password: 
receiving incremental file list
deleting fgwegqeh
deleting 12124234
./
1.txt
10.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
dir1/
dir10/
dir2/
dir3/
dir4/
dir5/
dir6/
dir7/
dir8/
dir9/

sent 261 bytes  received 833 bytes  128.71 bytes/sec
total size is 0  speedup is 0.00
[root@backup-server backup]# ls
10.txt  2.txt  4.txt  6.txt  8.txt  dir1   dir2  dir4  dir6  dir8
1.txt   3.txt  5.txt  7.txt  9.txt  dir10  dir3  dir5  dir7  dir9
  • push推送数据进行同步
[root@source-server ~]# rsync -avz --delete /data/ rsyncuser@192.168.100.128::backup
Welcome to Backup Server!

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
dir1/
dir10/
dir2/
dir3/
dir4/
dir5/
dir6/
dir7/
dir8/
dir9/

sent 845 bytes  received 253 bytes  313.71 bytes/sec
total size is 0  speedup is 0.00

[root@backup-server backup]# rm -rf *
[root@backup-server backup]# ls
10.txt  2.txt  4.txt  6.txt  8.txt  dir1   dir2  dir4  dir6  dir8
1.txt   3.txt  5.txt  7.txt  9.txt  dir10  dir3  dir5  dir7  dir9

五、rsync+sersync 实现数据实时同步

5.1、数据同步原理

1、为什么要是用rsync+sersync?

sersync是基于inotify开发的,可以记录目录中发生变化的内容,具体到某个文件或者目录,在使用rsync同步的时候,只同步发生变化的文件或者目录。【增量同步】

2、rsync+inotify-tools 与 rsync+sersync 架构的区别?

①inotify-tools只能记录目录发生了变化,但是不能具体到某个文件或者目录。

②rsync同步不清楚哪些文件或目录发生了变化,就会整个目录都同步,效率低。

3、同步过程:

①源服务器上开启sersync记录指定路径的文件系统变化情况。

②源服务器上使用rsync命令把变化的数据同步到目标服务器上。

③源服务器上配置sersync服务,目标服务器安装rsync服务。

5.2 、部署rsync+sersync

sersync服务端【同步服务器】:192.168.100.253

rsync客户端【存放备份,目标服务器】:192.168.100.128

  • 开始部署sersync守护进程

下载并解压到指定路径

[root@source-server ~]# rz
rz waiting to receive.**[root@source-server ~]# 
[root@source-server ~]# ls
anaconda-ks.cfg  fdisk.sh        if.sh                read.sh                                        testfile
case.sh          first_shell.sh  nginx-1.29.1         sersync2.5.4_64bit_binary_stable_final.tar.gz  test.sh
chpasswd.sh      for.sh          nginx-1.29.1.tar.gz  sshd_check_status.sh                           while.sh
expect.exp       if1.sh          passwd.txt           test.exp
[root@source-server ~]# tar -xf sersync2.5.4_64bit_binary_stable_final.tar.gz                                                                                                                                                                             
                                                
[root@source-server ~]# ls
anaconda-ks.cfg  fdisk.sh        if1.sh               passwd.txt                                     test.exp
case.sh          first_shell.sh  if.sh                read.sh                                        testfile
chpasswd.sh      for.sh          nginx-1.29.1         sersync2.5.4_64bit_binary_stable_final.tar.gz  test.sh
expect.exp       GNU-Linux-x86   nginx-1.29.1.tar.gz  sshd_check_status.sh           
[root@source-server ~]# mv sersync2.5.4_64bit_binary_stable_final.tar.gz /opt

[root@source-server opt]# cd GNU-Linux-x86/
[root@source-server GNU-Linux-x86]# ls
confxml.xml  sersync2

 修改sersync的配置文件

 [root@source-server GNU-Linux-x86]# vim confxml.xml 
<sersync>
        <localpath watch="/data">
            <remote ip="192.168.100.128" name="backup"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="rsyncuser" passwordfile="/etc/rsync.passwd"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>

开启sersync守护进程同步数据,即实时同步。

[root@source-server opt]# mv GNU-Linux-x86/ sersync
[root@source-server opt]# ls
nginx-1.29.1  nginx-1.29.1.tar.gz  patch_workspace  sersync  sersync2.5.4_64bit_binary_stable_final.tar.gz

[root@source-server opt]# export PATH=$PATH:/opt/sersync  #保留系统原有的路径配置,避免被覆盖

[root@source-server opt]# sersync2 -d -r -o ./sersync/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: -d      run as a daemon
option: -r      rsync all the local files to the remote servers before the sersync work
option: -o      config xml name:  ./sersync/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 rsyncuser
passwordfile is         /etc/rsync.passwd
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 /data && rsync -artuz -R --delete ./ rsyncuser@192.168.100.128::backup --password-file=/etc/rsync.passwd >/dev/null 2>&1    #同步指令
run the sersync: 
watch path is: /data
[root@source-server data]# mkdir dir20
[root@source-server data]# touch 20.txt
[root@source-server data]# ls
10.txt  20.txt  3.txt  5.txt  7.txt  9.txt  dir10  dir20  dir4  dir6  dir8
1.txt   2.txt   4.txt  6.txt  8.txt  dir1   dir2   dir3   dir5  dir7  dir9

[root@backup-server backup]# ls
20.txt  data  dir20

从以上测试可以看出,rsync+sersync已经部署完成,可以正常的同步数据到目标主机上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值