第七十集 rsync远程同步✈(╯▽╰ )好香~~

本文详细介绍了rsync工具的使用,包括其特点、命令选项以及本地复制示例。接着,讲解了如何配置rsync源服务器,并展示了关闭服务的方法。此外,还探讨了Inotify这一文件系统事件监控机制,解释了其工作原理和内核参数优化。最后,通过实验展示了如何结合rsync和Inotify实现文件变化的实时同步,包括客户端的脚本编写和内核参数调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


rsync 同步概述:

Remote Sync ----- 远程同步,支持本地复制,或者与其他SSH 、rsync主机同步,功能类似于scp,但是要比scp丰富。
官方网站:http://rsync.samba.org

rsync 同步特点:

1、可以镜像保存整个目录树和文件系统。

2、可以很容易做到保持原来文件的权限、时间、软硬链接等等,无须特殊权限即可安装。

3、快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件。rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。

4、安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。
5、支持匿名传输,以方便进行网站镜像。

rsync 命令

rsync 选项 原位置 目标位置
选项:
-r				递归模式,包含目录及子目录中的所有文件
-l				对于符号链接文件仍然复制为符号链接文件
-v				显示同步过程的详细信息
-z				在传输文件时进行压缩
-a				归档模式,递归并保留对象属性,等同于-rlptgoD
-p				保留文件的权限标记
-t				保留文件的时间标记
-g				保留文件的属组标记(仅超级用户使用)
-o				保留文件的属主标记(仅超级用户使用)
-H				保留硬链接文件
-A				保留ACL属性信息
-D				保留设备文件及其他特殊文件
--delete		删除目标位置有而原始位置没有的文件
--checksum		根据对象的校验和来决定是否跳过文件

rsync本地复制

[root@rsync ~]# mkdir /www
[root@rsync ~]# cd /www
[root@rsync www]# touch test test1
[root@rsync www]# mkdir /hhh
[root@rsync www]# rsync -avz /www /hhh				#将/www带目录复制到/hhh下
sending incremental file list
www/
www/test
www/test1

sent 160 bytes  received 58 bytes  436.00 bytes/sec
total size is 0  speedup is 0.00
[root@rsync www]# ls /hhh							#查看结果显示联通目录一起复制
www
[root@rsync www]# rsync -avz /www/ /hhh				#原目录后加上/表示仅复制目录下的文件
sending incremental file list
./
test
test1

sent 148 bytes  received 57 bytes  410.00 bytes/sec		
total size is 0  speedup is 0.00
[root@rsync www]# ls /hhh							#查看结果
test  test1  www

配置rsync源服务器

[root@rsync www]# yum install -y rsync					#下载或安装rsync工具
[root@rsync www]# vim /etc/rsyncd.conf 					#修改配置文件
##删除原本内容,添加以下内容
uid = root											# 表示管理rsync的用户,默认是nobody
gid = root										
use chroot = yes										# 表示是否禁锢在源目录                                                
address = 192.168.8.151								# 监听地址
port 873                     	                        # 监听端口                   
log file = /var/log/rsyncd.log							# 日志文件位置                  
pid file = /var/run/rsyncd.pid	                		# 存放进程id的文件位置
hosts allow = 192.168.8.0/24							# 允许访问的客户及地址
[wwwroot]   					                                                         
path = /var/www/html     		                  		# 源目录的实际路径             
comment = Document Root of www.ljm.com	
read only = yes             	 						#是否为只读                              
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z  		#同步时不再压缩的文件类型
auth users = backuper                                   #授权账户,多个账户以空格分隔
secrets file = /etc/users.db 							#存放账户信息的数据文件
[root@rsync ~]# vim /etc/user.db						#为备份账户设置文件
backuper:123456
[root@rsync ~]# chmod 600 /etc/user.db 				#为数据文件设置权限,保证安全性
[root@rsync ~]# mkdir -p /var/www/html				#创建源目录并给予权限
[root@rsync ~]# chmod +r /var/www/html/
[root@rsync ~]# ls -ld /var/www/html/
drwxr-xr-x. 2 root root 6 八月 11 05:27 /var/www/html/
[root@rsync ~]# rsync --daemon						#开启服务
[root@rsync html]# netstat -antp | grep rsync
tcp        0      0 192.168.8.151:873       0.0.0.0:*               LISTEN      26771/rsync 

关闭服务

kill $(cat /var/run/rsyncd.pid)
rm -rf /var/run/rsyncd.pid

查看同步

服务端:
[root@rsync ~]# cd /var/www/html
[root@rsync html]# echo "hello world" >> 1.txt


客户端:
[root@client ~]# mkdir /aaa
[root@client ~]# cd /aaa
[root@client aaa]# rsync -avz whd@192.168.8.151::wwwroot /aaa/
Password: 
receiving incremental file list
./
test

sent 46 bytes  received 122 bytes  11.59 bytes/sec
total size is 12  speedup is 0.07
[root@client aaa]# ls
test

客户端面交互同步

[root@client aaa]# echo "abc123" > /etc/server.pass
[root@client aaa]# chmod 600 /etc/server.pass
[root@client aaa]# rm -rf test
[root@client aaa]# rsync -avz --password-file=/etc/server.pass whd@192.168.118.100::wwwroot /aaa/
receiving incremental file list
./
test

sent 46 bytes  received 122 bytes  16.00 bytes/sec
total size is 12  speedup is 0.07
[root@client aaa]# ls
test

Inotify介绍

Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了inotify支持,通过Inotify可以监控文件系统添加、删除、移动、修改等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况。而inotify-tools正是实施这样监控的软件
Inotify实际是始终事件驱动机制,它为应用程序监控文件系统事件提供了实时响应事件的机制,而无需通过诸如cron等轮询机制来获取事件。Cron等机制不仅无法做到实时性,而且耗费大量系统资源。相比之下,inotify基于事件驱动,可以做到对事件处理的实时响应,也没有轮询造成的系统资源消耗,是非常自然的事件通知接口,也与自然世界的事件机制相符合。

inotify内核参数优化

inotifywait :			#用于持续监控,实时输出结果
inotifywatch :			#用于短期监控,任务完成后再出结果
max_queue_events    	#监控事件队列大小
max_user_instances  	#最多监控实例数
max_user_watches    	#每个实例最多监控文件数  

inotifywait参数选项:

-m 持续进行监控
-r 递归监控所有子对象
-q 简化输出信息
-e 指定要监控那些事件类型

rsync+inotify实验

设置rsync配置文件
[root@rsync html]# vim /etc/rsyncd.conf 
uid = root								#修改属主
gid = root								#修改属组
use chroot = yes
address = 192.168.8.151
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.8.0/24
[wwwroot]
path = /var/www/html
comment = Document Root of www.ljm.com
read only = no							#关闭只读权限
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z
auth users = zsx
secrets file = /etc/user.db
重启服务
[root@rsync html]# kill `cat /var/run/rsyncd.pid`
[root@rsync html]# rsync --daemon
[root@rsync html]# netstat -antp | grep rsync
tcp        0      0 192.168.8.151:873       0.0.0.0:*               LISTEN      26771/rsync    

客户端配置内核参数
[root@client aaa]# vim /etc/sysctl.conf 				#添加以下内容
fs.inotify.max_queued_events = 32768					#设置监控时间队列,默认为16384
fs.inotify.max_user_instances = 1024					#设置监控实例数,默认为128
fs.inotify.max_user_watches = 1048576					#每个实例最多监控文件数,默认为8192
[root@client aaa]# sysctl -p
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

客户端安装部署inotify-tools

[root@client opt]# tar xzf inotify-tools-3.14.tar.gz
[root@client opt]# yum install -y gcc gcc-c++
[root@client opt]# cd inotify-tools-3.14/
[root@client inotify-tools-3.14]# ./configure
[root@client inotify-tools-3.14]# make && make install
[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /aaa		#实时监控,并在客户端添加文件,移动文件,观察监控
重启一个client端口,在/aaa下创建文件观察
root@client ~]# cd /aaa
[root@client aaa]# ls
test
[root@client aaa]# echo "this ccc" > test

观察结果

[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /aaa
/aaa/ CREATE test
/aaa/ MODIFY test

client编写触发同步脚本

[root@client inotify-tools-3.14]# vim /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /aaa/" 		#设置实时监控变量
RSYNC_CMD="rsync -apzH --delete --password-file=/etc/server.pass /aaa/ whd@192.168.8.151::wwwroot/ "
#设置变量监控本地变化,发生变化给推送至服务端                                      
$INOTIFY_CMD | while read DIRECTORY EVENT FILE	/aaa/
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
        $RSYNC_CMD
	fi
done
[root@client inotify-tools-3.14]# chmod +x /opt/inotify.sh 
[root@client aaa]# chmod +x /etc/rc.d/rc.local
[root@client opt]# echo "/opt/inotify.sh" >> /etc/rc.d/rc.local
[root@client opt]# cd /aaa
[root@client aaa]# ls
test  test
[root@client aaa]# rm -rf test test

开启另外一个client终端创建文件,在原终端上实时查看

[root@localhost aaa]# touch 1.txt
[root@localhost aaa]# cd /opt
[root@localhost opt]# sh -x inotify.sh
 + rsync -apzH --delete --password-file=/etc/server.pass /aaa/ whd@192.168.8.151::wwwroot/
 + read DIRECTORY EVENT FILE
++ pgrep rsync
++ wc -l
 + '[' 0 -le 0 ']'
 + rsync -apzH --delete --password-file=/etc/server.pass /aaa/ whd@192.168.8.151::wwwroot/
 + read DIRECTORY EVENT FILE

在服务端上查看

[root@rsync html]# ls
1.txt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值