当前位置: 首页 > news >正文

小菜狗的云计算之旅,学习了解rsync+sersync实现数据实时同步(详细操作步骤)

Rsync+sersync实现数据实时同步

目录

Rsync+sersync实现数据实时同步

一、rsync概述

二、rsync运行原理

三、rsync部署

四、备份测试

五、使用非系统用户备份数据

5.1 rsync的配置文件介绍

5.2 配置备份目录

5.3 使用rsync用户备份测试

5.4 pull拉取数据

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

6.1 数据同步原理

6.2 部署rsync+sersync

6.3 设置rsync+sersync开机自启

七、总结


一、rsync概述

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

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

优点:

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

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

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

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

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

  • 支持匿名传输

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

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

# 查看版本信息
[root@server ~]# rsync --version
rsync  version 3.1.2  protocol version 31
Copyright (C) 1996-2015 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,append, ACLs, xattrs, iconv, symtimes, prealloc
​
rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you
are welcome to redistribute it under certain conditions.  See the GNU
General Public Licence for details.

备份分类:

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

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

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

二、rsync运行原理

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

源主机:需要同步数据的服务器 (以数据为参照物)

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

数据同步方式:push 和 pull

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

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

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

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

[root@targetpc ~]# systemctl start rsyncd
[root@targetpc ~]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemonLoaded: loaded (/usr/lib/systemd/system/rsyncd.service; disabled; vendor preset: disabled)Active: active (running) since 六 2025-06-21 21:53:40 CST; 16min agoMain PID: 5256 (rsync)Tasks: 1CGroup: /system.slice/rsyncd.service└─5256 /usr/bin/rsync --daemon --no-detach
​
6月 21 21:53:40 targetpc systemd[1]: Started fast remote file cop....
Hint: Some lines were ellipsized, use -l to show in full.
​
#查看端口号, 通过xinetd服务监听端口号 /etc/xinetd.d/
[root@targetpc xinetd.d]# netstat -antup | grep 873
tcp        0      0 192.168.157.130:873     0.0.0.0:*               LISTEN      5256/rsync          
udp        0      0 0.0.0.0:55276           0.0.0.0:*                           873/avahi-daemon: r 
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           873/avahi-daemon: r 

三、rsync部署

# rsync服务由xinetd服务进行管理,故也需要安装xinetd服务
# 客户端和服务器都需要安装xinetd服务和rsync服务
[root@server ~]# yum install xinetd rsync -y
[root@client ~]# yum install xinetd rsync -y
​
# rsync运行在daemon模式
[root@server ~]# rsync --daemon
[root@client ~]# rsync --daemon
​
# 查看是否监听873端口号
[root@server ~]# netstat -antup | grep 873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      18927/rsync         
tcp6       0      0 :::873                  :::*                    LISTEN      18927/rsync 
[root@client ~]# netstat -antup | grep 873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      9367/rsync          
tcp6       0      0 :::873                  :::*                    LISTEN      9367/rsync  
参数作用
-a–archive archive mode权限保存模式【递归、保持属性】(包含以下所有属性)
-r递归处理
-p保留文件原有属性
-t保留文件原有时间
-g保留属组
-o保留档案所有者
-D保留device咨询
-l复制所有的连接
-z压缩传输
-H保留硬链接文件
-A保留文件的ACL属性
-P–progress
--version输出rsync版本信息
-v显示输出过程
-u仅进行更新
--port指定端口号,默认873
--delete删除那些目标位置有的文件而备份源没有的文件,最大程度的保持一致
--password-file指定密码文件
--bwlimit限制I/O带宽
--filter需要过滤的文件
--exclude需要过滤的文件
--progress显示备份过程
–avz常用:保留权限、显示同步过程、压缩传输

四、备份测试

# 由于rsync备份是会保持目录及其文件的权限、时间、软硬连接不变的,那要求备份在ssh传输过程中一路顺畅,不会因权限不足而备份失败。
# 这里创建test用户用于测试
[root@server ~]# useradd test && echo 123456 | passwd --stdin test
更改用户 test 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@client ~]# useradd test && echo 123456 | passwd --stdin test
更改用户 test 的密码 。
passwd:所有的身份验证令牌已经成功更新。
# 服务端上创建测试目录/data
# g+s权限的作用在于/data目录下新增的文件的所属组都会是test
# setfacl为test用户创建在/data的rwx权限
[root@server ~]# mkdir /data
[root@server ~]# chown -R test:test /data
[root@server ~]# chmod g+s /data
[root@server ~]# setfacl -m u:test:rwx /data
[root@server ~]# cp -r  /boot/* /data
​
# 客户端上创建备份存放目录
[root@client ~]# mkdir  -p /data/backup/
[root@client ~]# chown test:test /data/backup/
[root@client ~]# ll -d /data/backup/
drwxr-xr-x. 2 test test 6 8月   6 23:52 /data/backup/
# 备份/data目录下的数据到客户端上
[root@server ~]# rsync -avz /data/ test@192.168.115.112:/data/backup
test@192.168.115.112's password: 
sending incremental file list
./
。。。省略备份信息。。。
sent 138,094,794 bytes  received 6,296 bytes  5,211,361.89 bytes/sec
total size is 152,364,618  speedup is 1.10
 
# 客户端上验证备份内容,可以看到有数据传输进去
[root@client ~]# du -sh /data/backup/
146M    /data/backup/
​
# 如果同步的时候,使用的不是ssh的22号端口,而是其他端口,比如222,要加-e参数指定端口号
rsync -avz /data/  -e "ssh -p 222" test@192.168.115.112:/data/backup 

五、使用非系统用户备份数据

5.1 rsync的配置文件介绍

rsync的配置文件:/etc/rsync.conf

# /etc/rsyncd.conf
#全局参数:对rsync服务器生效,优先级较低
port     # rsync占用端口号,默认是873
address  # 监听地址,一般是目标主机的IP地址
uid      # 运行进程的用户 
gid      # 运行进程的用户组
max connections  # 最大连接数
lock file   # 最大连接数的锁文件
motd file   # 同步登录后的提示语,填写欢迎同步信息,自行创建
log file    # 日志文件
pid file    # 进程PID文件,自动生成
hosts allow  # 允许同步的主机
​
#模块参数:针对某一个目录定义的参数,优先级较高
[mod_name]    # 同步目录名
comment       # 描述信息
path          # 同步目录
read only     # 同步目录的读写权限
exclude       
exclude from
include
include from
auth users   # 备份的用户,自动创建,与系统用户无关
secrets file  # 存放rsync用户的密码文件
hosts allow
hosts deny
list
timeout
​
# 启动rsync就会生成一个对应的pid
[root@server ~]# ll /var/run/rsyncd.pid
-rw-r--r--. 1 root root 6 8月   8 09:46 /var/run/rsyncd.pid
[root@server ~]# cat /var/run/rsyncd.pid
18933

5.2 配置备份目录

# 查看下/etc/rsyncd.conf的默认内容
# 可以使用man rsyncd.conf 命令查看更多选项信息
[root@server ~]# ll /etc/rsyncd.conf
-rw-r--r--. 1 root root 458 4月  11 2018 /etc/rsyncd.conf
[root@server ~]# cat /etc/rsyncd.conf 
# /etc/rsyncd: configuration file for rsync daemon mode
​
# See rsyncd.conf man page for more options.
​
# configuration example:
​
# uid = nobody  (运行账户)(生产环境下要注意)
# gid = nobody   (运行组) 
# use chroot = yes  (锁定当前组)
# max connections = 4  (最多连接四个账户)
# pid file = /var/run/rsyncd.pid  
# exclude = lost+found/
# transfer logging = yes  (传输登录)
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2   (同步过程中包含这些文件)
​
# [ftp]
#        path = /home/ftp
#        comment = ftp export area
  • 服务器:serverpc 192.168.157.129 (需要进行数据备份的主机)

  • 客户端:targetpc 192.168.157.130 (备份存储的主机)

一、客户端

1、编写客户端/etc/rsyncd.conf文件,并且创建目录/data/backup

push
# 在客户端上编写rsync配置文件,创建一个存放备份的同步目录
[root@targetcp ~]# vim /etc/rsyncd.conf
port=873
address = 192.168.157.130  #本机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.157.0/24    #目标机的IP
[data]    #(逻辑名 网络名)
path = /data/backup
comment = bakcup data
read only = false
list = yes   #是否列出
auth users = rsyncuser
secrets file = /etc/rsync.passwd  #独立系统用户的验证
~                                 
mkdir -P /

2、创建文件rsync.passwd写入用户和密码,并将密码文件赋权

# 创建密码文件,格式   用户名:密码
# 设置密码文件权限为600或者700
[root@targetpc ~]# vim /etc/rsync.passwd
[root@targetpc ~]# cat /etc/rsync.passwd 
rsyncuser:123456
[root@targetpc ~]#chmod 600 /etc/rsync.passwd

3、写入欢迎信息

[root@targetpc ~]# echo "Welcome to Backup Server" > /etc/rsyncd.motd

4、创建备份目录

[root@targetpc ~]#mkdir -p /data/backup

5、启动rsync服务

systemctl start rsyncd##前提关闭防火墙和安全上下文#查看监听端口号
[root@targetpc ~]# ss -anlt
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      5      192.168.157.130:873                 *:*      

二、服务器

1、回到数据源,也就是服务器serverpc手动push,创建目录/data自定义该目录的文件

[root@sourcepc data]# ls
aaa  ccc  f2  GNU-Linux-x86
bbb  f1   f3  sersync2.5.4_64bit_binary_stable_final.tar.gz

2、然后开始同步文件

[root@sourcepc data]# rsync -avz /data/* rsyncuser@192.168.157.130::data
Welcome to Backup ServerPassword: 
sending incremental file list
f1
f2
f3
aaa/
bbb/
ccc/sent 439 bytes  received 90 bytes  36.48 bytes/sec
total size is 2,541,385  speedup is 4,804.13

3、去客户端查看验证,并在客户机上创建一个文件file1

[root@targetpc backup]# ls
aaa  ccc  f2  GNU-Linux-x86
bbb  f1   f3  sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@targetpc backup]# touch file1
[root@targetpc backup]# ls
aaa  f1  file1
bbb  f2  GNU-Linux-x86
ccc  f3  sersync2.5.4_64bit_binary_stable_final.tar.gz

4、在服务机上删除file1,删除只会将服务机备份源没有的,而客户端目标位置有的文件删除,最大程度保持一致

[root@sourcepc data]# rsync -avz --delete /data/ rsyncuser@192.168.157.130::data
Welcome to Backup ServerPassword: 
sending incremental file list
deleting file1
./sent 344 bytes  received 32 bytes  27.85 bytes/sec
total size is 2,541,385  speedup is 6,759.00

5、在客户端验证文件是否存在

#此时发现文件file1并不存在,说明同步成功
[root@targetpc backup]# ls
aaa  ccc  f2  GNU-Linux-x86
bbb  f1   f3  sersync2.5.4_64bit_binary_stable_final.tar.gz

三、可以不用输密码直接同步

1、在服务器上创建与客户端密码相同的文件,并赋予相同的权限

[root@sourcepc ~]# vim /etc/rsync.passwd
[root@sourcepc ~]# cat /etc/rsync.passwd
123456
[root@sourcepc ~]# chmod 600 /etc/reync.passwd

2、在服务器上验证免密传输

#固定格式 --password-file=密码文件
[root@sourcepc data]# rsync -avz /data rsyncuser@192.168.157.130::data --password-file=/etc/rsync.passwd
Welcome to Backup Serversending incremental file list
data/
data/f3
data/sersync2.5.4_64bit_binary_stable_final.tar.gz
data/GNU-Linux-x86/
data/GNU-Linux-x86/confxml.xml
data/GNU-Linux-x86/nohup.out
data/GNU-Linux-x86/sersync2
data/aaa/sent 1,454,725 bytes  received 131 bytes  138,557.71 bytes/sec
total size is 2,541,385  speedup is 1.75
# 启动xinetd服务,并加入到开机自启中
[root@client ~]# systemctl start xinetd
[root@client ~]# systemctl enable xinetd
# 如果已经启动了rsync服务,要使用非系统用户登录,则必须先把原先的rsync停下来。
[root@client ~]# systemctl stop rsyncd
[root@client ~]# rsync --daemon --config=/etc/rsyncd.conf# 开放873端口号,允许通过873端口号同步数据  (防火墙已关闭,可以不用做)
[root@client ~]# firewall-cmd --permanent --zone=public --add-port=873/tcp
success
[root@client ~]# firewall-cmd --reload 
success

5.3 使用rsync用户备份测试

#
 rsync用户一般是第一个普通用户,即UID为1001,故备份的所有者和所属组都为1001
# 服务端推送数据到共享目录位置
#rsync 选项 需要备份的目录 rsync用户名@存放备份的服务器IP::共享模块名 --password-file=密码文件
[root@server ~]# rsync -avz --delete /data/ rsyncuser@192.168.115.112::data
Welcome to Backup ServerPassword: 
sending incremental file list
。。。省略同步信息。。。
sent 138,094,811 bytes  received 6,297 bytes  6,137,827.02 bytes/sec
total size is 152,364,618  speedup is 1.10
# 如果想不输入密码,可以使用--password-file参数指定密码
[root@server ~]# cat /etc/rsync.passwd
123456
[root@server ~]# chmod 600 /etc/rsync.passwd
[root@server ~]# rsync -avz /data rsyncuser@192.168.157.130::data --password-file=/etc/rsync.passwd
# 如果想定时同步数据,可以设置定时任务。
[root@server ~]# vim autobackup.sh
[root@server ~]# cat autobackup.sh
#!/bin/bash
rsync -avz /data rsyncuser@192.168.115.112::data --password-file=/etc/rsync.passwd
[root@server ~]# chmod +x autobackup.sh
[root@server ~]# crontab -e
50 19 * * * bash /root/autobackup.sh

5.4 pull拉取数据

以上的同步过程都是服务端主动推送数据给目标主机,这里演示下目标主机主动拉取数据进行同步。

# 服务端上配置/etc/rsyncd.conf文件
[root@server ~]# vim /etc/rsyncd.conf 
port=873
address = 192.168.115.111
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.43.0/24
[data]
path = /data
comment = master data
read only = false
list = yes
auth users = rsyncuser
secrets file = /etc/rsync.passwd
# 认证文件
[root@server ~]# cat /etc/rsync.passwd 
rsyncuser:123456
# 服务端上启动rsync
[root@server ~]# rsync --daemon --config=/etc/rsyncd.conf
[root@server ~]# netstat -antup | grep 873
tcp        0      0 192.168.115.111:873      0.0.0.0:*               LISTEN      11040/rsync   
# 看到以上结果,说明服务端的rsync服务已经配置完成。
# 开放873端口号,允许通过873端口号拉取数据
[root@server ~]# firewall-cmd --permanent --zone=public --add-port=873/tcp
success
[root@server ~]# firewall-cmd --reload 
success
# 目标主机上拉取数据
[root@client ~]# rsync -avz rsyncuser@192.168.115.111::data /data/backup/
Welcome to Backup ServerPassword: 
receiving incremental file list
。。。省略拉取过程。。。
sent 6,342 bytes  received 138,094,913 bytes  11,048,100.40 bytes/sec
total size is 152,364,627  speedup is 1.10

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

6.1 数据同步原理

1、为什么要是用rsync+sersync

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

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

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

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

3、同步过程:

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

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

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

6.2 部署rsync+sersync

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

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

由于sersync只能监控到差异变化,同步还是需要rsync去做,所以先确保rsync运行正常。可以测试一遍,看能否做完全同步。

[root@client ~]# rsync --daemon --config=/etc/rsyncd.conf
[root@client ~]# netstat -antup | grep rsync
tcp        0      0 192.168.115.112:873      0.0.0.0:*               LISTEN      9298/rsync   
# 添加到开机自启
[root@client ~]# vim /etc/rc.d/rc.local
rsync --daemon --config=/etc/rsyncd.conf
###备份源上测试
[root@server ~]# rsync -avz /data rsyncuser@192.168.115.112::data --password-file=/etc/rsync.passwd

开始部署sersync守护进程

# 解压并重命名为sersync
[root@server ~]# cd /opt
[root@server opt]# ll
总用量 712
-rw-r--r--. 1 root root 727290 8月   9 09:52 sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@server opt]# tar xzf sersync2.5.4_64bit_binary_stable_final.tar.gz 
[root@server opt]# ll
总用量 712
drwxr-xr-x. 2 root root     41 10月 26 2011 GNU-Linux-x86
-rw-r--r--. 1 root root 727290 8月   9 09:52 sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@server opt]# mv GNU-Linux-x86 sersync
[root@server opt]# ll
总用量 712
drwxr-xr-x. 2 root root     41 10月 26 2011 sersync
-rw-r--r--. 1 root root 727290 8月   9 09:52 sersync2.5.4_64bit_binary_stable_final.tar.gz
# 修改sersync的配置文件
# sersync服务总共就两个文件,一个配置文件,一个启动文件
[root@server ~]# cd /opt/sersync/
[root@server sersync]# ll
总用量 1772
-rwxr-xr-x. 1 root root    2109 8月   9 09:59 confxml.xml
-rwxr-xr-x. 1 root root 1810128 10月 26 2011 sersync2
[root@server ~]# vim /opt/sersync/confxml.xml
# 定义监控路径、同步的目标主机、共享模块
23     <sersync>
24         <localpath watch="/data">
25             <remote ip="192.168.115.112" name="data"/>
26         </localpath>
# 开启用户认证,并设置账号和密码 
29             <auth start="true" users="rsyncuser" passwordfile="/etc/rsync.passwd"/>
# 开启sersync守护进程同步数据,即实时同步。
# -r参数,先做一次完全同步,再做差异同步。
[root@server ~]# /opt/sersync/sersync2 -d -r -o /opt/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:  /opt/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.115.112::data --password-file=/etc/rsync.passwd >/dev/null 2>&1 
[root@server ~]# run the sersync: 
watch path is: /data   # 可以看到监控的是/data
# 打开监控,查看实时同步情况
# 当目录/data下的文件或者目录有任何的变化,都会被监控到,并同步到目标主机上。
[root@server data]# watch ls -l

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

6.3 设置rsync+sersync开机自启

# 设置开机自启
[root@server ~]# vim /etc/rc.d/rc.local
/opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml
# 如sersync正常运行,通过ps命令可以看到进程已经开启。
[root@server ~]# ps aux | grep sersync2 | grep -v 'grep'| wc -l
1
# 检查sersync是否正常运行
[root@server ~]# vim /opt/check_sersync.sh
#!/bin/sh
while true
do
sersync="/opt/sersync/sersync2"
confxml="/opt/sersync/confxml.xml"
status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)
if [ $status -eq 0 ] ; thenecho "sersync未开启,现在开启"echo "==============================="$sersync -d -r -o $confxml echo "==============================="bash $0
elseecho "sersync已正常开启"exit 0;
fi
sleep 1
done
[root@server ~]# chmod  +x /opt/check_sersync.sh
[root@server ~]# bash /opt/check_sersync.sh
# 设置两分钟检查一次sersync是否正常运行
[root@server ~]# crontab -e
*/2 * * * * sh /opt/check_sersync.sh &> /dev/null

一、部署rsync+sersync

  • serverpc服务端【同步服务器】:serverpc 192.168.157.129

  • targetpc客户端【存放备份,目标服务器】:targetpc:192.168.157.130

1、由于sersync只能监控到差异变化,同步还是需要rsync去做,所以先确保rsync运行正常。可以测试一遍,看能否完全同步

[root@sourcepc data]# rz   #导入软件包
[root@sourcepc data]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@sourcepc data]# ls
aaa  f3  GNU-Linux-x86  sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@sourcepc data]# cd GNU-Linux-x86/
[root@sourcepc GNU-Linux-x86]# ls
confxml.xml  nohup.out  sersync2##将所有的false改为true触发
[root@sourcepc GNU-Linux-x86]# <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="/data">         #监视的目录<remote ip="192.168.157.130" name="data"/>   #监视到的内容同步到谁,同步名data<!--<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"/>#users=改为rsyncuser#passwordfile=改为密码文件<userDefinedPort start="false" port="874"/><!-- port=874 --> #不自定义端口号<timeout start="false" time="100"/><!-- timeout=100 --><ssh start="false"/></rsync>                                   

2、启动服务(因为在当前目录,直接./执行)

[root@sourcepc GNU-Linux-x86]# ./sersync2 -d -r -o ./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:  ./confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost     host port: 8008
will ignore the inotify createFile event 
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.157.130::data --password-file=/etc/rsync.passwd >/dev/null 2>&1 

3、创建一个文件或目录观察监控

[root@sourcepc data]# mkdir abc##在客户端上传输成功
[root@targetpc backup]# ls
aaa  f3             sersync2.5.4_64bit_binary_stable_final.tar.gz
abc  GNU-Linux-x86##在服务器上监测  (-d  放在后台)
[root@sourcepc GNU-Linux-x86]# strace ./sersync2  -r -o ./confxml.xml
inotify_add_watch(3, "/data/aaa", IN_MODIFY|IN_ATTRIB|IN_CLOSE_WRITE|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE) = 3
open("/data/aaa", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 5
getdents(5, /* 2 entries */, 32768)     = 48
getdents(5, /* 0 entries */, 32768)     = 0
close(5)                                = 0
inotify_add_watch(3, "/data/abc", IN_MODIFY|IN_ATTRIB|IN_CLOSE_WRITE|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE) = 4
open("/data/abc", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 5
getdents(5, /* 2 entries */, 32768)     = 48
getdents(5, /* 0 entries */, 32768)     = 0
close(5)                                = 0
inotify_add_watch(3, "/data/123", IN_MODIFY|IN_ATTRIB|IN_CLOSE_WRITE|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE) = 5
open("/data/123", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 5
getdents(5, /* 2 entries */, 32768)     = 48
getdents(5, /* 0 entries */, 32768)     = 0
close(5)                                = 0
getdents(4, /* 0 entries */, 32768)     = 0
close(4)                                = 0
fcntl(3, F_GETFL)                       = 0 (flags O_RDONLY)
read(3, 
##更改名称 
[root@sourcepc data]# mv abc ABC
[root@targetpc backup]# ls
123  ABC  GNU-Linux-x86
aaa  f3   sersync2.5.4_64bit_binary_stable_final.tar.gz

4、脚本编写

#!/bin/bash
systemctl status rsyncd &> /dev/null
if [ $? -ne 0 ];then
systemctl start rsyncd 
firead -p "请输入ip地址:" ip
read -p "请输入创建的文件名称:" file
read -p "请输入跨机使用的密码:" passwd
mask=`echo $(echo $ip | cut -d. -f1,2,3).0/24`
echo "port=873
address = $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 = $mask
[$file]
path = /$file/backup
comment = bakcup data
read only = false
list = yes
auth users = rsyncuser
secrets file = /etc/rsync.passwd">/etc/rsyncd.conf
touch /etc/rsync.passwd
chmod 600 /etc/rsync.passwd
echo "rsyncuser:$passwd">/etc/rsync.passwd
mkdir -p /$file/backup
echo "Welcome to Backup server">/etc/rsyncd.motdsystemctl restart rsyncd

七、总结

  • rsync可以进行数据的同步,可以使用推和拉两种方式。推即源主机推数据到目标主机,拉即目标主机从源主机上拉数据。

  • push:服务器向客户端推送数据,要在目标主机上配置一个共享目录,在服务端上使用rsync命令推送数据给目标主机。

  • pull:客户端向服务器拉去数据,需要把服务器上的同步目录配置成一个共享目录,然后客户端去这个共享目录上拉去数据。

  • 仅使用rsync同步数据,不会记录数据的变化,每次同步都是同步整个目录,不适合大量数据的同步。

  • 结合使用rsync+sersync,sersync负责监控源主机上同步目录的数据变化,rsync负责同步变化的部分,极大地提高了同步的效率。

http://www.lryc.cn/news/581557.html

相关文章:

  • 【Linux网络编程】Socket - UDP
  • 儿童趣味记忆配对游戏
  • 【CSS-15】深入理解CSS transition-duration:掌握过渡动画的时长控制
  • Java的各种各样的生命周期——思考历程
  • 字符函数和字符串函数(下)- 暴力匹配算法
  • ASP.NET Web Pages 安装使用教程
  • 随机森林算法详解:Bagging思想的代表算法
  • 【大模型入门】访问GPT_API实战案例
  • 8.2.1+8.2.2插入排序
  • 企业智脑:智能营销新纪元——自动化品牌建设与智能化营销的技术革命
  • 【Linux操作系统 | 第12篇】Linux磁盘分区
  • Dubbo 3.x源码(31)—Dubbo消息的编码解码
  • 我的LeetCode刷题指南:链表部分
  • 微服务基础:Spring Cloud Alibaba 组件有哪些?
  • 云原生 Serverless 架构下的智能弹性伸缩与成本优化实践
  • java easyExce 动态表头列数不固定
  • vue3 当前页面方法暴露
  • 0704-0706上海,又聚上了
  • 《前端路由重构:解锁多语言交互的底层逻辑》
  • 【Zotero】Zotero无法正常启动解决方案
  • 深度解析命令模式:将请求封装为对象的设计智慧
  • Flink ClickHouse 连接器数据写入源码深度解析
  • Gin Web 层集成 Viper 配置文件和 Zap 日志文件指南(下)
  • LoRaWAN的设备类型有哪几种?
  • 条件渲染 v-show与v-if
  • CICD[软件安装]:ubuntu安装jenkins
  • QtConcurrent入门
  • #渗透测试#批量漏洞挖掘#HSC Mailinspector 任意文件读取漏洞(CVE-2024-34470)
  • 2025.7.6总结
  • 智能网盘检测软件,一键识别失效链接