一、rsync同步简介
.一款快速增量备份工具
- Remote Sync,远程同步
- 支持本地复制,或者与其他SSH、rsync主机同步
- 官方网站: http://rsync.samba.org
1. 软件简介
- Rsync 是一个远程数据同步工具,可通过 LAN/WAN 快速同步多台主机间的文件。
- Rsync 本来是用以取代scp 的一个工具,它当前由 Rsync.samba.org 维护。
- Rsync 使用所谓的“Rsync 演算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。
- 运行 Rsync server 的机器也叫 backup server,一个 Rsync server 可同时备份多个 client 的数据;也可以多个Rsync server 备份一个 client 的数据。
2. Rsync原理
- Rsync 可以使用 daemon 模式。Rsync server 会打开一个873的服务通道(port),等待对方 Rsync 连接。
- 连接时,Rsync server 会检查口令是否相符,若通过口令查核,则可以开始进行文件传输。
- 第一次连通完成时,会把整份文件传输一次,下一次就只传送二个文件之间不同的部份。
Rsync 支持大多数的类 Unix 系统,CentOS7.0图形界面安装基本已经安装了Rsync软件,可以用yum安装
3. Rsync 的基本特点如下:
- 可以镜像保存整个目录树和文件系统;
- 可以很容易做到保持原来文件的权限、时间、软硬链接等;
- 无须特殊权限即可安装;
- 优化的流程,文件传输效率高;
- 可以使用 rcp、ssh 等方式来传输文件,当然也可以通过直接的 socket 连接;
- 支持匿名传输。
4.rsync命令的用法
格式:rsync [选项] 原始位置 目标位置 常用选项
选项 | 用途 |
---|---|
-a | 归档模式,递归并保留对象属性,等同于-rlptgoD |
-V | 显示同步过程的详细(verbose)信息 |
-z | 在传输文件时进行压缩(compress) |
-H | 保留硬连接文件 |
-A | 保留ACL属性信息 |
–delete | 删除目标位置有而原始位置没有的文件 |
–checksum | 根据对象的校验和来决定是否跳过文件 |
5.同步源的两种方法
格式1: rsync [选项] 用户名@主机地址::共享模块名 目标位置
格式2: rsync [选项] rsync://用户名@主机地址/共享模块名 目标位置
二、配置Rsync服务器
Rsync同步源(指备份操作的远程服务器,也称为备份源)
1.基本思路
- 建立rsyncd.conf配置文件、独立的账号文件
- 启用rsync的–daemon模式
daemon守护进程的作用是时该服务即使没有请求服务,服务的端口号任然开放。
应用示例 - 用户backuper,允许下行同步
- 操作的目录为/var/www/html/
实验步骤
- 配置文件/etc/rsyncd.conf
- 设置rsync账号文件
- 启动rsync服务
- 到另一台服务器验证
【服务器1(rsync源端)配置】
1.修改rsync配置文件
[root@oracle ~]# vi /etc/rsyncd.conf
uid = nobody ##主账号为匿名
gid = nobody ##组账号匿名
use chroot = yes ##家目录锁定,锁定家目录后无法使用cd命令进入其他目录
address = 192.168.10.10 ##本地rsync源端地址
max connections = 4 ##最大连接数为4
pid file = /var/run/rsyncd.pid ##pid文件位置
log file = /var/log/rsyncd.log ##日志文件位置
port = 873 ##监听端口873
hosts allow = 192.168.10.0/24 ##允许访问的网段
[wwwroot] ##共享模块的名称
path = /var/www/html ##共享目录
comment = ww.bai.com ##描述信息,自定义
read only = yes ##只读模式
auth users = backuper ##授权的虚拟用户为backuper(不需要系统创建)
secrets file = /etc/rsyncd_user.db ##认证密码存放文件
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 ##不进行压缩的文件格式类型
2.编辑用户记录文件并启动rsync
[root@oracle ~]# vi /etc/rsyncd_user.db ##编写用户数据文件
backuper:abc123 ##每一行为一个用户记录,格式为——用户名:密码
[root@oracle ~]# chmod 600 /etc/rsyncd_user.db ## 600权限,不允许其他用户读取
[root@oracle ~]# rsync --daemon ##启动守护进程
[root@oracle ~]# netstat -anupt |grep 873 ##查看端口状态
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 12109/rsync
tcp6 0 0 :::873 :::* LISTEN 12109/rsync
3.共享目录下创建文件测试
[root@oracle html]# vi abc
abc
[root@oracle html]# cat abc
this abc
【服务器2(发起端)】
1.到发起端测试,查看 /var/www/html目录下文件
[root@squid ~]# cd /var/www/html
[root@squid html]# ls ##查看不到文件,为空
[root@squid html]#
2.和服务器1(源端)进行rsync同步
[root@squid html]# rsync -avz rsync://backuper@192.168.10.10/wwwroot /var/www/html
##rsync -avz backuper@192.168.10.10::wwwroot /var/www/html ###该命令效果一样
Password: ##输入密码
receiving incremental file list
./
abc
sent 46 bytes received 118 bytes 36.44 bytes/sec
total size is 9 speedup is 0.05
[root@squid html]# ls ##在此查看,发现源端的文件同步到发起端了
abc
3.配置免交互
[root@squid html]# rm -rf abc ##先删除abc文件,方便测试
[root@squid html]# ls
[root@squid html]#
[root@squid html]# vi /etc/server.password ##编写免交互密码存放文件
abc123 ##写入rsync指定的虚拟用户backuper的密码
[root@squid html]# chmod 600 /etc/server.password ##同样设置600权限,只允许主人查看
[root@squid html]# vi /etc/server.password
[root@squid html]# rsync -az --delete --password-file=/etc/server.password backuper@192.168.10.10::wwwroot /var/www/html ##免交互同步,--delete会删除本端与源端文件名不相同的文件,--password-file指定存放密码的文件位置
[root@squid html]# ls
abc
[root@squid html]# cat abc ##同步成功
this abc
4.可指定周期性计划任务,定时同步
[root@oracle ~]# crontab -e ##设置每晚10点执行同步
*/30 22 * * * rsync -az --delete --password-file=/etc/server.password backuper@192.168.10.10::wwwroot /var/www/html
[root@oracle ~]# crontab -l
*/30 22 * * * rsync -az --delete --password-file=/etc/server.password backuper@192.168.10.10::wwwroot /var/www/html
三、rsync+inotify实时同步
单靠rsync结合周期计划任务有很大的局限性,不能实时的更新,因此产生inotify这款软件来解决这个瓶颈
1.rsync实时同步
- 定期同步的不足
◆ 执行备份的时间固定,延迟明显、实时性差
◆ 当同步源长期不变化时,密集的定期任务是不必要的 - 实时同步的优点
◆ 一旦同步源出现变化,立即启动备份
◆ 只要同步源无变化,则不执行备份
2.关于inotify
- Linux内核的inotify机制
◆ 从版本2.6.13开始提供
◆ 可以监控文件系统的变动情况,并做出通知
◆ 响应辅助软件: inotify-tools
3.实验设计
安装inotify-tools辅助工具
inotifywait:用于持续监控,实时输出结果
inotifywatch:用于短期监控,任务完成后再出结果
需要两台服务器:服务器1(rsync源端)、服务器2(inotify端)
- 调整inotify需要的内核参数
- 安装inotify软件
- 修改上述实验中rsync服务器1端中配置文件参数:read only = no
- 调整两台服务器共享目录html的权限为777
- 使用inotify内置命令进行测试
- 编辑实时监控脚本
- 验证脚本可用性
rsync分布式+inotify实时监控,实现实时同步
【服务器2(inotify端)】
1.修改内核参数
[root@oracle ~]# vi /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@oracle ~]# sysctl -p ##生效
fs.inotify.max_queued_events = 16384 ##监控事件的队列大小
fs.inotify.max_user_instances = 1024 ##监控的最大实例数
fs.inotify.max_user_watches = 1048576 ##每个实例被监控的最大文件数
2. 安装inotify软件
[root@oracle ~]# ls ##查看inotify软件包
inotify-tools-3.14.tar.gz
[root@oracle inotify-tools-3.14]# yum -y install gcc gcc-c++ make ##安装环境
[root@oracle ~]# tar zxvf inotify-tools-3.14.tar.gz -C /opt/ ##解压软件包
[root@oracle ~]# cd /opt/inotify-tools-3.14/
[root@oracle inotify-tools-3.14]# ./configure ##直接配置
[root@oracle inotify-tools-3.14]# make && make install ##安装软件
[root@oracle ~]# inotifywait -mrq -e modify,create,move,delete /var/www/html & ##后台执行监控
3.测试监控
[root@oracle ~]# cd /var/www/html ##进入html目录创建文件
[root@oracle html]# touch test.txt ##创建文件
[root@oracle ~]#
/var/www/html/ CREATE test.txt ##这里后台会有监控提示,创建了test.txt文件
[root@oracle html]# rm -rf test.txt ## 删除test.txt文件
[root@oracle ~]#
/var/www/html/ CREATE test.txt
/var/www/html/ DELETE test.txt ##后台出现新的提示,删除了test.txt文件
4.配置监控脚本
[root@oracle opt]# vim /opt/inotify.sh ##编写监控脚本
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,move,attrib,delete /var/www/html/"
RSYNC_CMD="rsync -az --delete --password-file=/etc/server.password /var/www/html/ backuper@192.168.10.10::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ]
then
$RSYNC_CMD
fi
done
5.调整共享目录html的权限为777
[root@oracle opt]# chmod +x inotify.sh ##给执行权限
[root@oracle opt]# chmod 777 /var/www/html/ ##给/var/www/html最高权限,可供读写
[root@oracle opt]# ll /var/www/
total 0
drwxrwxrwx. 2 root root 17 Oct 23 22:13 html
【服务器1(rsync源端)】
### 1.修改/etc/rsyncd.conf中[wwwroot]共享模块配置
[root@oracle opt]# vi /etc/rsyncd.conf
……省略部分
[wwwroot]
path = /var/www/html
comment = ww.bai.com
read only = no ##将这里的yes改为no,需要能写
……省略部分
2.调整共享目录html的权限为777
[root@squid html]# chmod 777 /var/www/html ##同样给与最高权限
[root@squid html]# ll /var/www/
drwxrwxrwx 2 root root 17 Oct 23 19:16 html
3.关闭rsync进程,生效配置
[root@oracle ~]# netstat -anupt |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 12109/rsync
[root@oracle ~]# pkill -9 rsync ##关闭进程,重启生效
[root@oracle ~]# netstat -anupt |grep rsync ##进程关闭了
[root@oracle ~]# rsync --daemon ##启动守护进程
error:failed to create pid file /var/run/rsyncd.pid: File exists ##会提示报错,pid文件存在
[root@oracle ~]# rm -rf /var/run/rsyncd.pid ##删除pid文件即可
[root@oracle ~]# rsync --daemon ##再次启动守护进程
验证
1.首先在两台服务器共享目录插入不同文件,造成不同步现象
【服务器1(rsync源端)】
[root@oracle ~]# cd /var/www/html/
[root@oracle html]# touch test.txt
[root@oracle html]# ls
test.txt
[root@oracle html]#
【服务器2(inotify端)】
[root@squid ~]# cd /var/www/html/
[root@squid html]# touch abc.txt
[root@squid html]# ls
abc.txt
[root@squid html]#
两台服务器共享目录内文件不一致
2.执行脚本,进行验证
【服务器2(inotify端)】
[root@squid opt]# ./inotify.sh & ##后台运行脚本
[1] 10901
[root@squid opt]# cd /var/www/html/
[root@squid html]# touch 123.txt ##在共享目录中添加新的文件
[root@squid html]# rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync: chgrp "/.123.txt.BUNMxZ" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/.abc.txt.cVZqbR" (in wwwroot) failed: Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync: chgrp "/.123.txt.wa4qcd" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/.abc.txt.33vNF5" (in wwwroot) failed: Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
###注意,这里的failed提示并不是同步失败,而是因为rsync服务器那一端修改了read only = no的参数,rsync程序默认是yes,因此会提示失败,实际上并不影响
【服务器1(rsync源端)】
[root@oracle html]# ls ##原本的test.txt文件被删除,新增了inotify端的文件,完全一致状态
123.txt abc.txt