随笔 - 32  文章 - 0  评论 - 2  阅读 - 7374

rsync同步知识

✨rsync生产场景应用:

1. 两台服务器间或一对多定时同步

2. 企业全网备份,通过本地打包备份,然后rsync结合inotify应用把全网数据统一备份到一个固定存储服务器,然后在这台存储服务器上通过脚本检查并报警管理员备份结果

3.  rsync结合inotify的功能做实时的数据同步


 

定时同步:rsync+cron

实时同步:rsync   sersync


✨工作方式

单个主机本地之间的数据传输(类似于cp命令的功能)

借助rcp,ssh等通道来传输数据(类似于scp命令的功能)

以守护进程(socker)的方式传输数据(这个是rsync自身的重要功能)


✨本地数据传输模式(local-only mode

语法:rsync [OPTION...] SRC...  [DEST]

Rsync       为同步命令

[OPTION]  同步时的参数

SRC    为源,即待拷的分区,文件或目录等

DEST    为目的分区、文件或目录等

 

实例1:

把系统的hosts 文件同步到/opt目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@13-151 ~]# rsync /etc/hosts /opt/   #等同于cp /etc/hosts /opt
[root@13-151 ~]# ls /opt/
hosts  mha_soft删除操作:由于/null是空目录,所以/data也被同步成空目录
[root@13-151 data]# ls /data/ /null/
/data/:
asdadw  #一个文件
 
/null/:
[root@13-151 data]# rsync -r --delete /null/ /data/
[root@13-151 data]# ls /data/ /null/              
/data/:
 
/null/:
[root@13-151 data]#

✨拉复制实例:

当需要两台服务器需要目录保持一致就可以用到sync服务(C/S软件),需要一主(master) 一从(slave) 

 ip:192.168.13.150(master)

 ip:192.168.13.151(slave)

 

一般都会结合crontab来完成复制

13.150操作:

第一步:

1
2
[root@13-150 cache]# rpm -qa | grep rsync  #检查是否安装服务
rsync-3.0.6-9.el6.x86_64

 

 第二步:

mkdir /etc/rsyncd  && cd /etc/rsyncd/ &&  vim rsyncd.conf   #创建目录 进入目录 自己写配置文件

1
2
3
4
5
[root@13-150 rsyncd]# ll
总用量 12
-rw-r--r-- 1 root root 326 8月  29 09:44 rsyncd.conf   #配置文件
-rw-r--r-- 1 root root  99 8月  29 09:45 rsyncd.motd  #欢迎文件  当slave链接是 出现的提示消息
-rw------- 1 root root  13 8月  29 09:43 rsyncd.passwd  #密码文件 当slave链接是 需要的账号和密码

 

 vim sryncd.conf

1
2
3
4
[root@13-150 rsyncd]# cat rsyncd.motd
################################
#####sdasfskdsjfkjah############
################################
1
2
[root@13-150 rsyncd]# cat rsyncd.passwd
hello:123456

rsync --daemon --config=/etc/rsyncd/rsyncd.conf    #启动 查看端口

注意报错多一半是因为配置文件写错

13.151操作:

vim /etc/rsync.pw

1
2
[root@13-151 etc]# cat rsync.pw     #slave服务器上密码文件 只写密码即可 不用担心安全问题
123456
1
2
3
4
5
6
7
[root@13-151 etc]# rsync -aczP --delete --password-file=/etc/rsync.pw hello@192.168.13.150::www /cache
 
receiving incremental file list
 
sent 54 bytes  received 163 bytes  16.07 bytes/sec
total size is 2048000000  speedup is 9437788.02
[root@13-151 etc]#

 到此啦复制完成 可以配置crontab来完成定时复制。

此处提供一个小脚本

1
2
3
4
5
6
7
8
[root@13-151 ~]# cat 1.sh
#!/bin/bash
 
while :
        do
        rsync -aczP --delete --password-file=/etc/rsync.pw hello@192.168.13.150::www /cache >> /dev/null<br>     wait    #可以保证上一条命令执行完毕 再去执行 #一般针对大文件 小文件就无所谓了
        sleep 0.001
done

 ✨推复制

slave服务器不知道什么时候master什么时候会又数据

所有这时候就应该选择推复制

安装间谍软件 如果发现目录有变化 就触发推复制

缺点:高并发 文件多的时候出现问题 一点一点增加一点一点减少

优点:不怕大文件

slave服务器:

关闭master 873端口

1
2
3
4
5
6
7
8
9
[root@13-150 GNU-Linux-x86]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name  
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1153/sshd          
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1229/master        
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      3252/mysqld        
tcp        0      0 :::22                       :::*                        LISTEN      1153/sshd          
tcp        0      0 ::1:25                      :::*                        LISTEN      1229/master        
[root@13-150 GNU-Linux-x86]#

把master配置文件 scp 到slave服务器上

启动程序 # 让slave 监听873端口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@13-151 srync]# ll
总用量 12
-rw-r--r-- 1 root root 324 8月  29 12:02 rsyncd.conf
-rw-r--r-- 1 root root  99 8月  29 11:28 rsyncd.motd
-rw------- 1 root root  13 8月  29 11:28 rsyncd.passwd
[root@13-151 srync]# rsync --daemon --config=/etc/srync/rsyncd.conf
[root@13-151 srync]#
[root@13-151 srync]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name  
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1134/sshd          
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1210/master        
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      71087/rsync        
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      2907/mysqld        
tcp        0      0 :::22                       :::*                        LISTEN      1134/sshd          
tcp        0      0 ::1:25                      :::*                        LISTEN      1210/master        
tcp        0      0 :::873                      :::*                        LISTEN      71087/rsync

 

master服务器:上传文件

sersync2.5_32bit_binary_stable_final.tar.gz   解压完叫  GNU-Linux-x86

1
tar xf sersync2.5_32bit_binary_stable_final.tar.gz -C /usr/src/  #指定目录解压

 cd 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
<sersync>
       <localpath watch="/cache">   #同步目录
           <remote ip="192.168.13.151" name="www"/>  #slave服务器ip name 共享名称
           <!--<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="hello" passwordfile="/etc/rsyncd/rsyncd.passwd"/>  #密码文件
           <userDefinedPort start="false" port="874"/><!-- port=874 -->  #默认端口 如果修改端口 需要把start='false' 修成true
           <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
/tmp/rsync_fail_log.sh 日志 <br><br>报错请仔细检查配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@13-150 GNU-Linux-x86]# ./sersync2 -r
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: -r      rsync all the local files to the remote servers before the sersync work
daemon thread num: 10
parse xml config file
host ip : localhost     host port: 8008
use rsync password-file :
user is hello
passwordfile is         /etc/rsyncd/rsyncd.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 /cache && rsync -artuz -R --delete ./ hello@192.168.13.151::www --password-file=/etc/rsyncd/rsyncd.passwd >/dev/null 2>&1
run the sersync:
watch path is: /cache

 包地址:https://pan.baidu.com/s/1cvvqU-bhENws5OuviSGIeQ

  

 

 

 

posted on   I我非柠檬为何心酸I  阅读(495)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示