Jast blog Jast blog
首页
  • 《Ambari自定义开发教程》笔记
  • 《CDH教程》笔记
  • 《ClickHouse教程》笔记
  • 《HDFS教程》笔记
  • 《DolphinScheduler教程》笔记
  • 《Hbase教程》笔记
  • 《Iceberg教程》笔记
  • 《Hive教程》笔记
  • 《Flume教程》笔记
  • 《Kafka教程》笔记
  • 《Impala教程》笔记
  • 《Hue教程》笔记
  • 《Spark教程》笔记
  • 《Flink教程》笔记
  • 《Phoenix教程》笔记
  • 《ElasticSearch教程》笔记
  • 《Kylin教程》笔记
  • 《Storm教程》笔记
  • 《Yarn教程》笔记
  • 《Presto教程》笔记
  • 《图数据库教程》笔记
  • 《Kerberos教程》笔记
  • 《Maxwell教程》笔记
  • 《MinIO教程》笔记
  • 《DataX教程》笔记
  • 《Superset教程》笔记
  • 《IOTDB教程》笔记
  • 《大数据相关》笔记
  • 《PaddleNLP教程》笔记
  • 《Nginx教程》笔记
  • 《Java技术文档》
  • 《Maven教程》笔记
  • 《IDEA使用教程》
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档

Jast-zsh

如果你知道你要去哪里,全世界都会给你让路。
首页
  • 《Ambari自定义开发教程》笔记
  • 《CDH教程》笔记
  • 《ClickHouse教程》笔记
  • 《HDFS教程》笔记
  • 《DolphinScheduler教程》笔记
  • 《Hbase教程》笔记
  • 《Iceberg教程》笔记
  • 《Hive教程》笔记
  • 《Flume教程》笔记
  • 《Kafka教程》笔记
  • 《Impala教程》笔记
  • 《Hue教程》笔记
  • 《Spark教程》笔记
  • 《Flink教程》笔记
  • 《Phoenix教程》笔记
  • 《ElasticSearch教程》笔记
  • 《Kylin教程》笔记
  • 《Storm教程》笔记
  • 《Yarn教程》笔记
  • 《Presto教程》笔记
  • 《图数据库教程》笔记
  • 《Kerberos教程》笔记
  • 《Maxwell教程》笔记
  • 《MinIO教程》笔记
  • 《DataX教程》笔记
  • 《Superset教程》笔记
  • 《IOTDB教程》笔记
  • 《大数据相关》笔记
  • 《PaddleNLP教程》笔记
  • 《Nginx教程》笔记
  • 《Java技术文档》
  • 《Maven教程》笔记
  • 《IDEA使用教程》
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
  • Linux网站汇总
  • Shell使用详解
  • 自动化运维脚本集合
  • 模拟占用磁盘内存CPU
  • Linux释放缓存
  • Linux监控进程磁盘邮件预警
  • 《Nginx教程》笔记
  • Linxu服务器文件双向同步-rsync+sersync
    • 基础信息
    • rsync
      • 安装
      • 配置
      • 根据配置文件创建相应的目录、文件、防火墙规则
      • 创建需要同步的目录
      • 创建同步用户
      • 防火墙
      • 启动rsyncd服务
      • 测试
      • 异常处理
    • sersync
      • 安装
      • sersync配置
      • test21配置
      • test22配置
      • sersync启动
      • test21启动
      • test22启动
      • 测试
      • 新增文件
      • 修改内容
      • 删除文件
  • Linux文件清空的几种方法
  • Linux日志自动清理方案
  • Linux执行脚本加密
  • Linux安装包制作
  • Crontab详解
  • Linux上传下载百度网盘文件
  • 内网穿透
  • 内网穿透-无需公网服务器
  • 一文搞定Jenkins自动化部署程序
  • Linux配置代理请求
  • Linux监控之夜莺
  • Proxmox安装
  • Proxmox创建CentOS虚拟机
  • Proxmox创建Windows虚拟机
  • Debian系统安装OpenVPN
  • Docker快速部署OpenVPN
  • 用户登录时执行检查脚本
  • 一键配置Linux镜像源
  • RustScan端口扫描
  • 前置机、堡垒机、跳板机区别
  • Linux添加回收站功能
  • Linux系统详细信息监控-Grafana+Prometheus
  • 再见XShell:轻量又高性能的SSH工具AI加持快人一步
  • Unbuntu安装deb文件
  • Ubuntu远程连接时分辨率问题
  • Ubuntu中Clash报错
  • Linux可视化监控
  • Vim复制自动缩进问题处理
  • 无所不能的BusyBox:如何用它打造极小的Linux环境
  • 运维
Jast-zsh
2021-11-09
目录

Linxu服务器文件双向同步-rsync+sersync

# Linxu服务器文件双向同步 rsync+sersync

[toc]

# 基础信息

系统:Centos7.8

  • 服务器信息
服务器 IP
test21 10.8.10.21
test22 10.8.10.22
  • 软件版本信息
软件包 版本
rsync
sersync

Rsync安装包下载地址:https://github.com/WayneD/rsync/releases

sersync下载地址:https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz

# 安装

# rsync

# 安装

查看是否已经安装

rpm -qa | grep rsync
1

安装

yum -y install rsync
1

# 配置

  • 修改test21服务器文件/etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

uid = root      # 指定运行rsync daemon的用户
gid = root      # 指定运行rsync daemon的组
use chroot = no
address = 10.8.10.21 # 指定监听地址
port = 873      # 默认监听端口
max connections = 0     #最大连接数,0为无限制
pid file = /var/run/rsyncd.pid  # 指定pid文件
log file = /var/log/rsyncd.log # 指定日志文件
exclude = lost+found/   # 指定不同步的目录
ignore errors
#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
[update]        # update模块
path = /root/updatedir  # update模块需要同步目录
comment = test rsync + sersync  # update模块的简要说明
read only = no  # 是否只读
list = no       # 当用户查询该服务器上的可用模块时,是否列出该模块
auth users = rsync_daemon        # 同步文件使用到的虚拟用户
secrets file = /etc/rsync_update.passwd # 指定该虚拟用户对应的密码文件,该文件权限为(400)
hosts allow = 10.8.10.22     # 指定可以连接该模块的主机(x.x.x.x x.x.x.x/x)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  • 修改test22服务器文件/etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

uid = root      # 指定运行rsync daemon的用户
gid = root      # 指定运行rsync daemon的组
use chroot = no
address = 10.8.10.22 # 指定监听地址
port = 873      # 默认监听端口
max connections = 0     #最大连接数,0为无限制
pid file = /var/run/rsyncd.pid  # 指定pid文件
log file = /var/log/rsyncd.log # 指定日志文件
exclude = lost+found/   # 指定不同步的目录
ignore errors
#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
[update]        # update模块
path = /root/updatedir  # update模块需要同步目录
comment = test rsync + sersync  # update模块的简要说明
read only = no  # 是否只读
list = no       # 当用户查询该服务器上的可用模块时,是否列出该模块
auth users = rsync_daemon        # 同步文件使用到的虚拟用户
secrets file = /etc/rsync_update.passwd # 指定该虚拟用户对应的密码文件,该文件权限为(400)
hosts allow = 10.8.10.21     # 指定可以连接该模块的主机(x.x.x.x x.x.x.x/x)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# 根据配置文件创建相应的目录、文件、防火墙规则

# 创建需要同步的目录

在两个服务器上分别创建需要同步的目录

[root@test21 ~]# mkdir /root/updatedir
[root@test22 ~]# mkdir /root/updatedir
1
2

# 创建同步用户

创建虚拟用户rsync_daemon使用的密码文件/etc/rsync_update.passwd

该文件的权限必须为400

[root@test21 ~]# echo "rsync_daemon:123456789" > /etc/rsync_update.passwd
[root@test21 updatedir]# chmod 400 /etc/rsync_update.passwd
[root@test22 ~]# echo "rsync_daemon:123456789" > /etc/rsync_update.passwd
[root@test22 updatedir]# chmod 400 /etc/rsync_update.passwd
1
2
3
4

# 防火墙

这里我是关闭了防火墙,如果防火墙开启,防火墙规则要将873端口加入

# 启动rsyncd服务

[root@test21 ~]# systemctl start rsyncd
[root@test22 ~]# systemctl start rsyncd
1
2
  • 查看端口
[root@test21 /]# ss -antuple | grep 873
tcp    LISTEN     0      5      10.8.10.21:873                   *:*                   users:(("rsync",pid=14221,fd=3)) ino:72483 sk:ffff8800b7d75d00 <->

[root@test22 ~]# ss -antuple | grep 873
tcp    LISTEN     0      5      10.8.10.22:873                   *:*                   users:(("rsync",pid=1735,fd=3)) ino:29740 sk:ffff880226eb87c0 <->
1
2
3
4
5

# 测试

  • test21向test22同步
[root@test21 updatedir]# ls
[root@test21 updatedir]# touch file{1..5}
[root@test21 updatedir]# rsync -av /root/updatedir/ rsync_daemon@10.8.10.22::update
Password: 
sending incremental file list
./
file1
file2
file3
file4
file5

sent 314 bytes  received 114 bytes  171.20 bytes/sec
total size is 0  speedup is 0.00
1
2
3
4
5
6
7
8
9
10
11
12
13
14

test22服务器查看

[root@test22 updatedir]# ls
file1  file2  file3  file4  file5
1
2
  • test22向test21同步
# 创建file6 - file10 空文件
[root@test22 updatedir]# touch file{6..10}
# 文件同步
[root@test22 updatedir]# rsync -av /root/updatedir/ rsync_daemon@10.8.10.21::update
Password: 
sending incremental file list
./
file10
file6
file7
file8
file9

sent 388 bytes  received 114 bytes  111.56 bytes/sec
total size is 0  speedup is 0.00
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

test21服务器查看

[root@test21 updatedir]# ls
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9
1
2

rsync数据同步成功,我们可以设置linxu定时任务crontab每隔一段时间自动双向同步文件。

# 异常处理

  • 提示权限不足

test22发送到test21可以正常发送,test21发送到test22无法发送

[root@test21 updatedir]# rsync -av /root/updatedir/ rsync_daemon@10.8.10.22::update
Password: 
sending incremental file list
rsync: failed to set times on "." (in update): Permission denied (13)
./
file1
file2
file3
file4
file5
rsync: mkstemp ".file1.PRynLL" (in update) failed: Permission denied (13)
rsync: mkstemp ".file2.L3Zgd2" (in update) failed: Permission denied (13)
rsync: mkstemp ".file3.t4KaFi" (in update) failed: Permission denied (13)
rsync: mkstemp ".file4.PZF46y" (in update) failed: Permission denied (13)
rsync: mkstemp ".file5.NOJYyP" (in update) failed: Permission denied (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

报错后查看网上各种资料描述,说是目录权限改为755等,试了之后都不行

随后又增加了一台服务器做测试,发现新增的也不行

[root@test22 etc]# rsync -av /root/updatedir/ root@10.8.10.20::update
Password: 
sending incremental file list
rsync: failed to set times on "." (in update): Permission denied (13)
rsync: recv_generator: failed to stat "123" (in update): Permission denied (13)
./

sent 59 bytes  received 173 bytes  51.56 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]
1
2
3
4
5
6
7
8
9
10

后来在stackoverflow发现有大佬提示说selinux问题导致,我们查看服务器配置

参考文献:https://stackoverflow.com/questions/11039559/rsync-mkstemp-failed-permission-denied-13

[root@test21 etc]# getenforce 
Permissive
[root@test20 updatedir]# getenforce
Enforcing
[root@test22 etc]# getenforce
Enforcing
1
2
3
4
5
6

发现的确21和22,20配置不同

修改22,20配置

[root@test20 updatedir]# setenforce 0
[root@test20 updatedir]# getenforce
Permissive

[root@test22 etc]# setenforce 0
[root@test22 etc]# getenforce
Permissive
1
2
3
4
5
6
7

setenforce 0为临时修改,永久修改vim /etc/sysconfig/selinux

SELINUX=enforcing
修改为
SELINUX=disable 或者 SELINUX=permissive
1
2
3

再次执行发现可以同步成功

[root@test22 etc]# rsync -av /root/updatedir/ root@10.8.10.20::update
Password: 
sending incremental file list
./
123

sent 102 bytes  received 38 bytes  40.00 bytes/sec
total size is 0  speedup is 0.00

[root@test22 etc]# rsync -av /root/updatedir/ root@10.8.10.20::update
Password: 
sending incremental file list
./
123

sent 102 bytes  received 38 bytes  40.00 bytes/sec
total size is 0  speedup is 0.00
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# sersync

上面安装完成rsync,我们只能通过定时任务触发文件同步,sersync可以对文件的变更进行实时监听,并触发rsync对变更的文件进行实时同步。

# 安装

下载:[点击下载] (opens new window)

将文件放在两台服务器中,并解压

[root@test22 sersync]# ll
总用量 712
-rw-------. 1 root root 727290 11月 10 13:11 sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@test22 sersync]# pwd
/opt/sersync
[root@test22 sersync]# tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz 
GNU-Linux-x86/
GNU-Linux-x86/sersync2
GNU-Linux-x86/confxml.xml
[root@test22 sersync]# ls GNU-Linux-x86/
confxml.xml  sersync2


[root@test21 sersync]# ll
总用量 712
-rw-------. 1 root root 727290 11月 10 13:11 sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@test21 sersync]# pwd
/opt/sersync
[root@test21 sersync]# tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz 
GNU-Linux-x86/
GNU-Linux-x86/sersync2
GNU-Linux-x86/confxml.xml
[root@test21 sersync]# ls GNU-Linux-x86/
confxml.xml  sersync2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# sersync配置

# test21配置

内容过多只显示了修改的位置,其他都是默认的

  1. <remote ip="10.8.10.22" name="update"/>

  2. <auth start="true" users="rsync_daemon" passwordfile="/etc/rsync.pas"/>

[root@test21 GNU-Linux-x86]# vim confxml.xml
...
<sersync>
        <localpath watch="/opt/updatedir">
            <remote ip="10.8.10.22" name="update"/>
            <!--<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="rsync_daemon" passwordfile="/etc/rsync.pas"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
        <crontab start="false" schedule="600"><!--600mins-->
            <crontabfilter start="false">
                <exclude expression="*.php"></exclude>
                <exclude expression="info/*"></exclude>
            </crontabfilter>
        </crontab>
        <plugin start="false" name="command"/>
    </sersync>
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  • watch 指定了监控的文件目录
  • remote ip 指定远程rsync服务监听的ip,
  • name 指定了远程rsync服务开启的模块
  • start=“true” 说明远程rsync服务开启了用户密码验证
  • users 指定了远程rsync服务用户密码验证指定的虚拟用户
  • passwordfile 指定了连接远程rsync服务虚拟用户的密码文件,权限需为400,用户自己手动创建

创建密码文件

[root@test21 GNU-Linux-x86]# echo 123456789 > /etc/rsync.pas
[root@test21 GNU-Linux-x86]# chmod 400 /etc/rsync.pas
1
2

# test22配置

[root@test22 GNU-Linux-x86]# vim confxml.xml
...
<sersync>
        <localpath watch="/opt/updatedir">
            <remote ip="10.8.10.21" name="update"/>
            <!--<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="rsync_daemon" passwordfile="/etc/rsync.pas"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
        <crontab start="false" schedule="600"><!--600mins-->
            <crontabfilter start="false">
                <exclude expression="*.php"></exclude>
                <exclude expression="info/*"></exclude>
            </crontabfilter>
        </crontab>
        <plugin start="false" name="command"/>
    </sersync>
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

创建密码文件

[root@test22 GNU-Linux-x86]# echo 123456789 > /etc/rsync.pas
[root@test22 GNU-Linux-x86]# chmod 400 /etc/rsync.pas
1
2

# sersync启动

# test21启动

[root@test21 updatedir]# /opt/sersync/GNU-Linux-x86/sersync2 -d -r -o /opt/sersync/GNU-Linux-x86/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/GNU-Linux-x86/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	rsync_daemon
passwordfile is 	/etc/rsync.pas
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 /root/updatedir && rsync -artuz -R --delete ./ rsync_daemon@10.8.10.22::update --password-file=/etc/rsync.pas >/dev/null 2>&1 
run the sersync: 
watch path is: /root/updatedir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  • -d 以后台daemon的方式运行
  • -r 第一次启动时,使用rsync将本地文件全部同步至远程服务器。
  • -o 加载配置文件

# test22启动

[root@test22 updatedir]# /opt/sersync/GNU-Linux-x86/sersync2 -d -r -o /opt/sersync/GNU-Linux-x86/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/GNU-Linux-x86/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	rsync_daemon
passwordfile is 	/etc/rsync.pas
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 /root/updatedir && rsync -artuz -R --delete ./ rsync_daemon@10.8.10.21::tongbu1 --password-file=/etc/rsync.pas >/dev/null 2>&1 
run the sersync: 
watch path is: /root/updatedir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# 测试

# 新增文件

在test21上传一个文件,在test22查看也同时存在

[root@test21 updatedir]# ll
总用量 4
-rw-------. 1 root root 253 11月 10 11:12 1.sh

[root@test22 updatedir]# ll
总用量 4
-rw-------. 1 root root 253 11月 10 11:12 1.sh
1
2
3
4
5
6
7

# 修改内容

修改test22

[root@test22 updatedir]# ll
总用量 4
-rw-------. 1 root root 253 11月 10 11:12 1.sh
[root@test22 updatedir]# echo 1 > 1.sh 
[root@test22 updatedir]# ll
总用量 4
-rw-------. 1 root root 2 11月 10 13:59 1.sh
[root@test22 updatedir]# cat 1.sh 
1
1
2
3
4
5
6
7
8
9

查看test21发现修改的内容同步成功

[root@test21 updatedir]# cat 1.sh 
1
1
2

# 删除文件

删除test21文件

[root@test21 updatedir]# rm 1.sh 
rm:是否删除普通文件 "1.sh"?y
[root@test21 updatedir]# ll
总用量 0
1
2
3
4

在test22 查看发现文件也同步删除

[root@test22 updatedir]# ll
总用量 0
1
2
上次更新: 2023/10/08, 09:28:24
《Nginx教程》笔记
Linux文件清空的几种方法

← 《Nginx教程》笔记 Linux文件清空的几种方法→

最近更新
01
Linux可视化监控
02-26
02
Maven私服搭建
02-26
03
当ElasticSearch时间字段设置多个格式到底是用的哪个?
01-19
更多文章>
Theme by Vdoing | Copyright © 2016-2025 Jast-zsh | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式