目录
6、保证所有用户对源目录 /var/www/html 都有读取权限
2、查看 rsync 是否已安装,一般系统已默认安装 rsync
5.1 在本机运行 /opt/inotify_rsync.sh 脚本程序
5.2切换到本机的 /var/www/html 目录,执行增加、删除、修改文件等操作
引言
rsync(Remote Sync,远程同步) 是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,并保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。
一、概述
1、 rsync服务器
- rsync是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具。并且可以不进行改变原有数据的属性信息,实现数据的备份迁移特性
- rsync软件适用于unix/linux/windows等多种操作系统平台
- rsync是一个快速和非常同样的文件复制工具。它能本能复制,远程复制,或者远程守护进程方式复制,它提供了大量的参数来控制其行为的各个方面,并且允许非常灵活的方式来实现文件的传输复制
- 以其delta-transfer算法闻名
- rsync监听端口:873
- rsync运行模式:C/S
2、同步方式
2.1 全量备份
原有的数据全部传送
把原来的文件和新的文件一起统一传送
全量复制,效率低
2.2 增量备份
在传输数据之前通过一些算法通过你有的数据和我有的数据进行对比,把不一样的数据通过网络传输
增量复制,效率高
3、rsync源服务器的关系
在远程同步任务中,负责发起 rsync 同步操作的客户机称为发起端,而负责响应来自客户机的 rsync 同步操作的服务器称为同步源。在同步过程中,同步源负责提供文件的原始位置,发起端应对该位置具有读取权限。
说明:若服务器A同步服务器B的数据,则服务器B是备份源。相反,若服务器B同步服务器A的数据,则服务器A是备份源
4、rsync的功能及特点
4.1 rsync功能
- 作为命令:实现本地-远程文件同步
- 作为服务:实现本地-远程文件同步
4.2 rsync特点
- 可以镜像保存整个目录树和文件系统
- 可以保留原有的权限(permission,mode),owner,group,时间(修改时间,modify time),软硬链接,文件acl,文件属性(attributes)信息等
- 传输效率高,使用同步算法,只比较变化的
- 支持匿名传输,方便网站镜像;也可以做验证,加强安全
5、 rsync同类服务
- sync 同步:刷新文件系统缓存,强制将修改过的数据块写入磁盘,并且更新超级块。
- async 异步:将数据先放到缓冲区,再周期性(一般是30s)的去同步到磁盘。
- rsync 远程同步:remote synchronous
6、cp与rsync对比
- cp将源文件完整复制到指定路径下 全备
- rsync 做本地复制的时候
- 先将源文件和目标文件进行比较,找出差异部分
- 根据目的位置的文件与源文件差异的部分,进行一致性同步
- scp:secure copy 同样是用来进行远程复制的命令,但是每次备份数据都是完全备份
二、配置rsync源
1、基本思路
建立rsyncd.conf配置文件、独立的账号文件
启用rsync的 --daemon模式
2、配置文件rsyncd.conf
认证配置auth users、secrets file,不加则为匿名
3、独立的账号文件
用户名:密码
每行一个用户记录
独立的账号数据,不依赖系统账号
4、启用rsync服务
通过 --daemon独自提供服务,rsync --daemon
执行kill $(cat /var/run/rsyncd.pid)关闭服务
5、命令语法
rsync 【选项】原始位置 目标位置
6、常用rsync命令
参数 | 说明 |
-r | 递归模式,包含目录及子目录中的所有文件 |
-l | 对于符号链接文件仍然复制为符号链接文件 |
-v | 显示同步过程的详细(verbose)信息 |
-z | 在传输文件时进行压缩(compress) |
-a | 归档模式,保留文件的权限、属性等信息,等同于组合选项“-rlptgoD” |
-p | 保留文件的权限标记 |
-t | 保留文件的时间标记 |
-g | 保留文件的属组标记(仅超级用户使用) |
-o | 保留文件的属主标记(仅超级用户使用) |
-H | 保留硬连接文件 |
-A | 保留 ACL 属性信息 |
-D | 保留设备文件及其他特殊文件 |
--delete | 删除目标位置有而原始位置没有的文件(保留一致性) |
--checksum | 根据校验和(而不是文件大小、修改时间)来决定是否跳过文件 |
三、inotify简介
可以监控文件系统的变动情况,并做出通知响应
-
调整inotify内核参数(优化)
-
/etc/sysctl.conf(内核参数配置文件)
max_queue_events #监控事件队列大小
max_user_instances #最多监控实例数
max_user_watches #每个实例最多监控文件数
-
notifywait:用于持续监控,实时输出结果
-
inotifywatch:用于短期监控,任务完成后再输出结果
inotifywait -mrq -e modify,create,move,delete /var/www/html
参数 说明 -m 持续进行监控 -r 递归监控所有子对象 -q 简化输出信息 -e 指定要监控哪些事件类型 modify 修改 create 创建 move 移动 delete 删除
五、配置rsync备份源
1、环境说明
主机 | 操作系统 | IP地址 | 软件 / 安装包 / 工具 |
client | CentOS7 | 192.168.223.53 | rsync |
server | CentOS7 | 192.168.223.13 | rsync / inotify-tools-3.14.tar.gz |
2、关闭防火墙
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
3、查看 rsync 是否已安装
rpm -q rsync
4、建立 /etc/rsync.conf 配置文件
vim /etc/rsyncd.conf #添加以下配置
uid = nobody #root
gid = nobody #root
use chroot = yes #禁锢在源目录
address = 192.168.223.13 #监听地址
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.223.0/24 #允许访问的客户机地址 (上行同步中的访问策略)
[wwwroot] ##第一个共享模块
path = /var/www/html #源目录的实际路径
comment = Document Root of www.test.com
read only = yes #是否为只读
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z #同步时不再压缩的文件类型
auth users = backuper #授权账户,多个账号以空格分隔
secrets file = /etc/rsyncd_users.db #存放账户信息的数据文件
5、为备份账户创建数据文件
vim /etc/rsyncd_users.db
backuper:123456 #无须建立同名系统用户
chmod 600 /etc/rsyncd_users.db #必须授权 600
6、保证所有用户对源目录 /var/www/html 都有读取权限
chmod +r /var/www/html
ls -ld /var/www/html
7、启动 rsync 服务程序
rsync --daemon #启动 rsync 服务,以独立监听服务的方式(守护进程)运行
netstat -antp | grep rsync
8、关闭 rsync 服务
kill $(cat /var/run/rsyncd.pid)
rm -rf /var/run/rsyncd.pid
netstat -natp | grep rsync
9、编写测试网页
echo "Rsync test!" > /var/www/html/test.html
六、配置发起端
1、环境准备
hostnamectl set-hostname backuper
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
2、查看 rsync 是否已安装,一般系统已默认安装 rsync
rpm -q rsync
3、将指定的资源下载到本地 /opt 目录下进行备份
格式一
rsync -avz backuper@192.168.223.13::rsync /var/www/html
yum -y install httpd && systemctl start httpd
curl http://192.168.223.53/test.html
格式二
rm -rf /var/www/html/*
rsync -avz rsync://backuper@192.168.223.13/rsync /var/www/html
curl http://192.168.223.53/test.html
4、免交互格式配置
echo "123456" > /etc/server.pass
chmod 600 /etc/server.pass
rm -rf /var/www/html/*
rsync -avz --password-file=/etc/server.pass backuper@192.168.223.53::rsync /var/www/html
curl http://192.168.223.13./test.html
5、计划性定时同步
crontab -e
0 1 * * * /usr/bin/rsync/ -az --delete --password-file=/etc/server.pass backuper@192.168.223.13::rsync /var/www/html
systemctl restart crond && systemctl enable crond
七、发起端配置 rsync + inotify
使用 inotiify 通知接口,可以用来监控文件系统的各种变化情况,如文件存取、删除、移动、修改等。利用这一机制,可以非常方便地实现文件异动告警、增量备份,并针对目录或文件的变化及时作出响应。
将 inotify 机制与 rsync 工具相结合,可以实现触发式备份(实时同步),即只要原始位置的文档发生变化,则立即启动增量备份操作;否则处于静默等待状态。这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题。
因为 inotify 通知机制由 Linux 内核提供,因此主要做本机监控,在触发式备份中应用时更适合上行同步。
1、修改 rsync 源服务器配置文件(rsync)
vim /etc/rsyncd.conf
#修改 read only 为 no
read only = no
kill $(cat /var/run/rsyncd.pid)
rm -rf /var/run/rsyncd.pid
rsync --daemon
netstat -natp | grep rsync
chmod 777 /var/www/html
2、调整 inotify 内核参数(backuper)
在 Linux 内核中,默认的 inotify 机制提供了三个调控参数
max_queued_events 监控事件队列,默认值为 16384
max_user_instances 最多监控实例数,默认值为 128
max_user_watches 每个实例最多监控文件数,默认值为 8192
当要监控的目录、文件数较多或者变化较频繁时,建议加大这三个参数的值
cat /proc/sys/fs/inotify/max_queued_events
cat /proc/sys/fs/inotify/max_user_instances
cat /proc/sys/fs/inotify/max_user_watches
可通过 /etc/sysctl.conf 文件进行配置,设定参数应符合服务器性能以及服务需要
vim /etc/sysctl.conf
#添加如下配置
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
sysctl -p
cat /proc/sys/fs/inotify/max_queued_events
cat /proc/sys/fs/inotify/max_user_instances
cat /proc/sys/fs/inotify/max_user_watches
3、安装 inotify-tools(backuper)
用 inotify 机制需要安装 inotify-tools,以便提供 inotifywait、inotifywatch 辅助工具程序,用来监控、汇总改动情况。
- inotifywait:可监控modify(修改)、create(创建)、move(移动)、delete(删除)、attrib(属性更改)等各种事件,一旦变动立即输出结果。
- inotifywatch:可用来收集文件系统变动情况,并在运行结果后输出汇总的变化情况。
[root@backuper ~]# cd /opt
[root@backuper opt]# rz -E
#传入 inotify-tools 安装包
rz waiting to receive.
[root@backuper opt]# tar zxvf inotify-tools-3.14.tar.gz -C /opt
[root@backuper opt]# cd inotify-tools-3.14/
[root@backuper inotify-tools-3.14]# ./configure
[root@backuper inotify-tools-3.14]# make -j 2 && make install
可以先执行 inotifywait
命令,然后另外再开启一个新终端向 /var/www/html 目录下添加文件、移动文件,在原来的终端上跟踪屏幕输出结果。
新终端操作
[root@backuper ~]# cd /var/www/html
[root@backuper html]# touch test.php
[root@backuper html]# mv test.php test.txt
[root@backuper html]# echo 'test' > test.txt
[root@backuper html]# rm -rf test.txt
原终端操作
[root@backuper inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html
/var/www/html/ CREATE test.php
/var/www/html/ MOVED_FROM test.php
/var/www/html/ MOVED_TO test.txt
/var/www/html/ MODIFY test.txt
/var/www/html/ DELETE test.txt
inotifywait 选项说明如下
常用选项 | 说明 |
---|---|
-e | 用来指定要监控哪些事件 |
-m | 表示持续监控 |
-r | 表示递归整个目录 |
-q | 简化输出信息 |
4、另一个终端的触发式同步脚本
[root@backuper html]# vim /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.223.13::rsync/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
##while判断是否接收到监控记录
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done
[root@backuper opt]# chmod +x inotify.sh
[root@backuper opt]# chmod 777 /var/www/html/
[root@backuper opt]# chmod +x /etc/rc.d/rc.local
[root@backuper opt]# echo '/opt/inotify.sh' >> /etc/rc.d/rc.local
##加入开机自动执行
上述脚本用来检测本机 /var/www/html 目录的变动情况,一旦有更新触发 rsync 同步操作,上传备份至服务器 192.168.223.53 的 rsync 共享目录下。
5、验证
5.1 在本机运行 /opt/inotify_rsync.sh 脚本程序
[root@backuper opt]# ./inotify.sh &
[1] 5786
5.2切换到本机的 /var/www/html 目录,执行增加、删除、修改文件等操作
[root@backuper opt]# cd /var/www/html
[root@backuper html]# ls
test.html test.txt
[root@backuper html]# rm -rf *
[root@backuper html]# touch test.html
[root@backuper html]# echo 'this is inotify_rsync test!' > test.html
5.3查看远端服务器中的 rsync 目录下的变化情况
[root@rsync ~]# cd /var/www/html
[root@rsync html]# ls
test.html
[root@rsync html]# cat test.html
this is inotify_rsync test!
6、使用rsync快速删除大量文件
假如要在 linux下删除大量文件,比如 100 万、1000 万,像 /usr/local/nginx/proxy_temp 的 nginx 缓存等,那么 rm -rf * 可能就不好使了,因为要等待很长一段时间。在这种情况下,我们可以利用 rsync 的 --delete 来巧妙处理。rsync 实际用的是替换原理。
6.1模拟垃圾文件
[root@rsync html]# touch {1..9999}.txt
6.2建立空文件夹
[root@rsync html]# mkdir /opt/test
6.3使用 rsync 进行替换删除
[root@rsync html]# rsync --delete-before -a -H -v --progress --stats /opt/test/ /var/www/html/
······
Number of files: 1
Number of files transferred: 0
Total file size: 0 bytes
Total transferred file size: 0 bytes
Literal data: 0 bytes
Matched data: 0 bytes
File list size: 19
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 29
Total bytes received: 15
sent 29 bytes received 15 bytes 29.33 bytes/sec
total size is 0 speedup is 0.00
其中参数说明如下
--delete-before:接受者在传输前进行删除操作
-a:归档模式,表示以递归方式传输文件,并保持所有文件属性
-H:保持硬链接文件
-v:详细输出模式
--progress:在传输室显示传输过程
--stats:给出某些文件的传输状态