关于rsync远程同步

rsync的定义

rsync是什么呢,作为初学者,既然不了解的咱就搜一搜呗🔮🔮🔮

在这里插入图片描述

 配置rsync源服务器

既然了解了是什么,那咱们就来配一配吧,看看咱到底配不配!

配置备份源的基本思路

 配置文件-rsyncd.conf

        

 rsync账号文件的作用

启用rsync服务

配置源的两种表示方法

备份工具-rsync

同步方式

 增量备份

rsync命令

基本格式:
rsync [选项] 原始位置 目标位置

 部署rsync远程同步

实验准备

主机ip地址
rsync192.168.35.40
client192.168.35.10

下载rsync并启动 

rpm -q rsync
yum -y install rsync		##安装rsync
vim /etc/rsyncd.conf		##修改/etc/rsyncd.conf配置文件
--------------->全局配置global<---------------------------
	uid = nobody	##root
	gid = nobody	##root
	use chroot = yes		##禁锢在源目录;禁锢的对象:使用用户的权限/身份;禁锢的目录就是源目录;源目录就是共享模块下的path = /var/www/html(源目录会在每个共享模块下)
	address = 192.168.35.40	##监听地址
	port 873		##监听端口/tcp/udp 873, 可通过cat /etc/services | grep rsync查看
	log file = /var/log/rsyncd.log 		##日志文件位置
	pid file = /var/run/rsyncd.pid 		##存放进程ID的文件位置
	hosts allow = 192.168.35.0/24 	##允许访问的客户机地址
--------------->共享模块<---------------------------
	[ wwwroot ]
	path = /var/www/html		##源目录的实际路径
	comment = Document Root of www.1jm.com
	read only = yes		##是否为只读
	dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z	##同步时不再压缩的文件类型,即rsync -avz
	auth users = backuper		##授权账户,多个账号以空格分隔
	secrets file = /etc/rsyncd_users.db 		###存放账户信息的数据文件,即用户密码的地址
	如采用匿名的方式,只要将其中的"auth users" 和"secrets file" 配置项去掉即可
vim /etc/rsyncd_users.db		##为备份账户创建数据文件,存放用户和用户密码
	backuper: klj		##无需建立同名系统用户,
chmod 600 /etc/rsyncd_users.db		##保证所有用户对源目录/var/www/html 都有读的权限
mkdir -p /var/www/html
chmod +r /var/www/html/
ls -ld /var/www/html/
rsync --daemon	##启动rsync 服务程序
netstat -antp | grep rsync 

 测试远程同步

 

rsync -avz backuper@192.168.35.40::wwwroot /abc/	##client进程远程共享

 

 

[root@client abc]# rm -rf 1.txt		##删除
[root@client abc]# ls
[root@client abc]# cd ..
[root@client /]# vim /etc/server.pass
[root@client /]# cat /etc/server.pass
klj
[root@client /]# chmod 600 /etc/server.pass
[root@client /]# rsync -avz backuper@192.168.35.40::wwwroot /abc/
Password: 
receiving incremental file list
./
1.txt
sent 46 bytes  received 113 bytes  63.60 bytes/sec
total size is 6  speedup is 0.04
[root@client /]# cd /abc
[root@client abc]# ls
1.txt

 

 

---------------------->rsync服务端<----------------------
[root@rsync ~]# cd /var/www/html
[root@rsync html]# ls
1.txt
[root@rsync html]# rm -rf 1.txt	##删除共享文件
[root@rsync html]# ls
[root@rsync html]# 
client客户端操作共享发现仍存在1.txt
[root@client ~]# cd /abc
[root@client abc]# ls
1.txt
[root@client abc]# rsync -avz --password-file=/etc/server.pass backuper@192.168.35.40::wwwroot /abc
Password: 
receiving incremental file list
./

sent 27 bytes  received 47 bytes  29.60 bytes/sec
total size is 0  speedup is 0.00
[root@client abc]# ls
1.txt
 
我们想要实现的是同步应该是本地也删除1.txt,加上--delete即可实现
[root@client abc]# rsync -avz --delete --password-file=/etc/server.pass 		backuper@192.168.35.40::wwwroot /abc
receiving incremental file list
deleting 1.txt/

sent 20 bytes  received 40 bytes  40.00 bytes/sec
total size is 0  speedup is 0.00

 

 rsync实时同步的不足 

 rsync结合inotify实时同步

 inotify简介

/etc/sysctl.conf (内核参数配置文件)	##调整inotify内核参数(优化)
参数含义
inotifywait用于持续监控,实时输出结果
inotifywatch用于短期监控,任务完成后再输出结果
max_queue_events监控事件队列大小
max_user_instances最多监控实例数
max_user_watches每个实例最多监控文件数

 持续监控并实时输出监控结果的命令-inotifywait

格式: inotifywait [参数]

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

 实验: rsync+inotify

vim /etc/rsyncd.conf
	uid = root		##修改为root
	gid = root		##修改为root
	[wwwroot]
	read only = no		##关闭只读,上行同步需要可写权限
kill 'cat /var/run/rsyncd.pid'
netstat -natp | grep rsync
rsync --daemon		##启动rsync
netstat -natp | grep rsync		##验证是否开启

 

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
当要监控的目录、文件数据量较多或者变化频繁时,建议加大参数值
sysctl -p		##刷新生效

 在这里插入图片描述

yum -y install gcc gcc-c++
tar -zxvf inotify-tools-3.14.tar.gz -C /opt
 cd /opt/inotify-tools-3.14
./configure
make&&make install

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值