远程同步工具rsync!

rsync基本知识

概述:rsycn是一种开源、快速、多功能的增量及全量备份工具,支持本地复制和跨节点复制、支持远程同步,它的监听端口是873

rsunc同步源

  • 只备份操作的远程服务器,也叫备份源,可分为两部分

在这里插入图片描述

配置源的两种方法

1、用户名@主机地址::共享模块名
2、rsync://用户名@主机地址/共享模块名

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.118.100								# 监听地址
port 873                     	                        # 监听端口                   
log file = /var/log/rsyncd.log							# 日志文件位置                  
pid file = /var/run/rsyncd.pid	                		# 存放进程id的文件位置
hosts allow = 192.168.118.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						#为备份账户设置文件
whd:abc123
[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 ~]# netstat -antp | grep rsync
tcp        0      0 192.168.118.100:873     0.0.0.0:*               LISTEN      88951/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" >> test


客户端:
[root@client ~]# mkdir /aaa
[root@client ~]# cd /aaa
[root@client aaa]# rsync -avz whd@192.168.118.100::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内核参数优化
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.118.100
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.118.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 = whd
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.118.100:873     0.0.0.0:*               LISTEN      89705/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" > test2

观察结果
[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /aaa
/aaa/ CREATE test2
/aaa/ MODIFY test2
  • 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.118.100::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  test2
[root@client aaa]# rm -rf test test2
  • 开启另外一个client终端创建文件,在原终端上实时查看
[root@localhost aaa]# touch 3.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.118.100::wwwroot/
 + read DIRECTORY EVENT FILE
++ pgrep rsync
++ wc -l
 + '[' 0 -le 0 ']'
 + rsync -apzH --delete --password-file=/etc/server.pass /aaa/ whd@192.168.118.100::wwwroot/
 + read DIRECTORY EVENT FILE
  • 在服务端上查看
[root@rsync html]# ls
3.txt
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值