rsync远程同步一起来了解一下吧!

一.rsync简介

①rsync是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具。并且可以不进行改变原有数据的属性信息,实现数据的备份迁移特性。

②rsync软件适用于unix/linux/windows等多种操作系统平台

③rsync是一个快速和非常方便的文件复制工具。它能本地复制,远程复制,或者远程守护进程方式复制,它提供了大量的参数来控制其行为的各个方面,并且允许非常灵活的方式来实现文件的传输复制

④以其delta-transfer算法闻名。

⑤rsync监听端口:873

⑥rsync运行模式:c/s

二.配置rsnc服务源

1.基本思路

备份操作的远程服务器,也称为备份源

(1)建立rsyncd.conf配置文件、独立的账号文件;服务账号远程登录使用rsync的相关功能,服务账号是自定义的

(2)启用rsync的–daemon模式

2.配置文件-rsyncd.conf

(1)需手动建立,语法类似于Samba配置

(2)认证配置auth users、secrets file, 不加则为匿名

3.rsync账号文件的作用

采用“用户名:密码”的记录格式,每行一个用户记录;独立的账号数据,不依赖于系统账号

(1)需要对应配置文件模块(使用不同的账号+功能模块,就可以使用不同的同步策略/方式

(2)在使用同步时,我们需要指定账号+功能模块来指定我们同步的策略

4.启用rsync服务

通过–daemon独自提供服务
执行kill $(cat /var/run/rsyncd.pid)关闭rsync服务

5.配置源的两种表示方法

(1)格式一
用户名@主机地址: :共享模块名.
rsync -avz backuper@192.168.35.40::wwwroot /root

(2)格式二
rsyne://用户名@主机地址/共享模块名
rsync -avz rsync:/ /backuper@192.168.35.40::/wwwroot /root

三.备份工具rsync

1.同步方式

① 全量备份:

② 原有的数据全部传送

③ 把原来的文件和新的文件一起统一传送

④ 全量复制,效率低

2.增量备份

在传输数据之前通过一些算法通过你有的数据和我有的数据进行对比,把不一样的数据通过网络传输
增量复制,效率高

3.rsync命令

rsync [选项] 原始位置 目标位置

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

四.远程复制实验

1.实验准备

2台服务器,一台源服务器,一台客户端

rsync:192.168.206.177
client:192.168.206.188

2.配置rsync源服务器

[root@rsync ~]# rpm -q rsync
rsync-3.1.2-4.el7.x86_64
[root@rsync ~]# yum -y install rsync

3.修改/etc/rsyncd.conf配置文件

[root@rsync ~]# vim /etc/rsyncd.conf

#添加以下配置
uid = nobody										#root
gid = nobody										#root
use chroot = yes									#禁锢在源目录                                                
address = 192.168.206.177							#监听地址
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.206.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/rsyncd_users.db 				#存放账户信息的数据文件      

如采用匿名的方式,只要将其中的 “auth users” 和 “secrets file”配置项去掉即可

4.为备份账户创建数据文件

[root@rsync ~]# vim /etc/user.db

backuper:abc123		#无需建立同名系统用户

[root@rsync ~]# chmod 600 /etc/user.db

5.保证所有用户对源目录 /var/www/html 都有读的权限

[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 24 61 00:59 /var/www/html/

6.启动 rsync 服务程序

[root@rsync ~]# rsync --daemon
[root@rsync ~]# netstat -natp | grep rsync
tcp        0      0 192.168.206.177:873     0.0.0.0:*               LISTEN      73543/rsync

7.测试远程同步

服务源

[root@rsync ~]# cd /var/www/html
[root@rsync html]# echo "hello world" >> 111.txt

客户端

[root@client ~]# mkdir /abc

[root@client ~]# rsync -avz backuper@192.168.206.177::wwwroot /abc/
Password:
receiving incremental file list
./
111.txt
index.html

sent 65 bytes  received 213 bytes  17.94 bytes/sec
total size is 38  speedup is 0.14
[root@client ~]# cd /abc
[root@client abc]# ls
111.txt  index.html
[root@client abc]# cat 111.txt
hello world

以上是交互的操作
设置无交互

客户端

[root@client abc]# echo "abc123" > /etc/server.pass

[root@client abc]# chmod 600 /etc/server.pass

[root@client abc]# rsync -avz --delete --password-file=/etc/server.pass backuper@192.168.206.177::wwwroot /abc/
receiving incremental file list

sent 20 bytes  received 89 bytes  10.38 bytes/sec
total size is 38  speedup is 0.35
[root@client abc]#

8.rsync同步的优缺点

(1)定期同步的不足
执行备份的时间固定,延迟明显、实时性差
当同步源长期不变化时,密集的定期任务是不必要的
需要人为触发,shell脚本或者crontab

(2)实时同步的优点
一旦同步源出现变化,立即启动备份
只要同步源无变化,则不执行备份

五.rsync+inotify部署

1.inotify简介

可以监控文件系统的变动情况,并做出通知响应
调整inotify内核参数(优化)
/etc/sysctl.conf(内核参数配置文件)
inotifywait: #用于持续监控,实时输出结果
inotifywatch: #用于短期监控,任务完成后再输出结果
max_queue_events #监控事件队列大小
max_user_instances #最多监控实例数
max_user_watches #每个实例最多监控文件数

2.inotifywait(持续监控并实时输出监控结果的命令)

格式:inotifywait [参数]

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

3.rsync+inotify部署

(1)服务端:修改rsync配置文件

[root@rsync html]# vim /etc/rsyncd.conf

uid = root
gid = root
read only = no       #关闭只读,上行同步需要可写权限

[root@rsync html]# kill `cat /var/run/rsyncd.pid`
[root@rsync html]# netstat -natp | grep rsync
[root@rsync html]# rsync --daemon
[root@rsync html]# netstat -natp | grep rsync
tcp        0      0 192.168.206.177:873     0.0.0.0:*               LISTEN      73958/rsync

(2)客户端查看inotify内核参数并且修改

[root@client ~]# cat /proc/sys/fs/inotify/max_queued_events
16384
[root@client ~]# cat /proc/sys/fs/inotify/max_user_instances
128
[root@client ~]# cat /proc/sys/fs/inotify/max_user_watches
8192
[root@client ~]# 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 ~]# sysctl -p
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@client ~]#

(3)client安装 inotify-tools

[root@client ~]# yum -y install gcc gcc-c++					#下载依赖环境

[root@client ~]# cd /opt
[root@client opt]# ls
redis-5.0.7  redis-5.0.7.tar.gz  rh
[root@client opt]# tar zxvf inotify-tools-3.14.tar.gz -C /opt		#上传软件包解压

[root@client opt]# ls
inotify-tools-3.14  inotify-tools-3.14.tar.gz  redis-5.0.7  redis-5.0.7.tar.gz  rh
[root@client opt]# cd inotify-tools-3.14/
[root@client inotify-tools-3.14]# ./configure

[root@client inotify-tools-3.14]# make && make install

(4)执行“inotifywait”命令查看

然后在服务端向 /var/www/html 目录下添加文件、移动文件,跟踪屏幕输出结果
inotifywait -mrq -e modify,create,move,delete /abc

[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /abc

在这里插入图片描述
在这里插入图片描述

(5)client编写触发同步脚本

[root@client opt]# vim /opt/inotify.sh

#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /abc/"
RSYNC_CMD="rsync -apzH --delete --password-file=/etc/server.pass /abc/ backuper@192.168.206.177::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
        $RSYNC_CMD
	fi
done

(6)测试

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值