一.rsync 定义
1是一款快速增量备份工具
2 能更新整个目录树和文件系统; 有选择性的保持符号链链、硬链接、文件属性、权限、设备以及时间等;传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。能用rsh、ssh 或直接端口做为传输端口;
3 支持匿名rsync 同步文件,是理想的镜像工具
4 Rsync可以通过rsh或ssh使用,也能以daemon模式去运行,在以daemon方式运行时Rsync server会打开一个873端口,等待客户端去连接。在远程同步任务中,负责发起rsync同步操作的客户机称为发起端,而负责响应来自客机的rsync同步操作的服务器称为备份源。
二、如何配置rsync源
1、基本思路
修改rsyncd.conf配置文件、独立的密码文件
启用rsync的--daemon模式
2、应用示例
用户backuper,允许下行同步
操作的目录为/var/www/html
3、配置文件rsyncd.conf
语法类似于Samba配置
认证配置auth users、secrets file,不加则为匿名(nobody)
4、rsync账号文件
采用“用户名:密码”的记录格式,每行一个用户记录
独立的账号数据,不依赖于系统账号(安全)
5、如何启动和关闭rsync
启动rsync:rsync --daemon
关闭rsync:kill $(cat /var/run/rsyncd.pid)
6.rsync命令的用法
rsync [选项] 原始位置 目标位置
常用选项
-a:归档模式,递归并保留对象属性,等同于-rlptgoD
-v:显示同步过程的详细信息
-z:在传输文件时进行压缩
-H:保留硬连接文件
-A:保留ACL属性信息
--delete:删除目标位置有而原始位置没有的文件
--checksum:根据对象的校验和来决定是否跳过文件
三、配置rsync远程同步
3.1 实验环境拓扑图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0dd0ef52271751c069465b3bd5128f9e.png#pic_center)
3.2、具体步骤(rsync系统自带)
源站配置
1.修改配置文件
[root@Rsync ~]
uid = nobody
gid = nobody
use chroot = yes
address = 20.0.0.11
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 20.0.0.0/24
[wwwroot]
path = /var/www/html
comment = www.test.com
read only = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
auth users = backuper
secrets file = /etc/rsyncd_users.db
2. 创建backuper用户的密码文件,服务端的密码文件要600权限
[root@Rsync ~]
backuper:abc123
[root@Rsync ~]
3. 启动服务并查看状态
[root@Rsync ~]
[root@Rsync ~]
tcp 0 0 192.168.73.10:873 0.0.0.0:* LISTEN 49133/rsync
4. 发起端验证
方法一
[root@client ~]
Password:
receiving incremental file list
./
index1.html
sent 83 bytes received 153 bytes 67.43 bytes/sec
total size is 0 speedup is 0.00
方法二
[root@client html]
Password:
receiving incremental file list
./
index1.html
index2.html
sent 102 bytes received 209 bytes 88.86 bytes/sec
total size is 0 speedup is 0.00
[root@client html]
[root@client opt]
总用量 0
-rw-r--r--. 1 root root 0 11月 11 19:31 index1.html
-rw-r--r--. 1 root root 0 11月 11 19:33 index2.html
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
免密方式同步文件
[root@client opt]
abc123
[root@client opt]
[root@client opt]
[root@client opt]
[root@client backup]
总用量 0
-rw-r--r--. 1 root root 0 11月 11 19:31 index1.html
-rw-r--r--. 1 root root 0 11月 11 19:33 index2.html
rsync+inotify实时同步
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/157f84594351d0fb1f8b372b4a8af501.png#pic_center)
1、调整inotify内核参数
[root@client ~]
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@client ~]
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
2、源站修改配置文件
[root@Rsync ~]
read only = no
3、安装inotify-tools辅助工具
[root@client ~]
[root@client ~]
[root@client inotify-tools-3.14]
[root@client inotify-tools-3.14]
4、开启监控
[root@client inotify-tools-3.14]
inotifywait:用于持续监控,实时输出结果
inotifywatch:用于短期监控,任务完成后再出结果
-m:持续进行监控
-r:递归监控所有子对象
-q:简化输出信息
-e:指定要监控哪些事件类型
modify:修改;create:创建;move:移动;delete:删除
5、测试监控是否正常使用
再开一个客户端终端会话
[root@client opt]
[root@client html]
[root@client inotify-tools-3.14]
/var/www/html/ DELETE index1.html
6、客户端上编写脚本,将inotify监控和rsync远程同步结合起来
[root@client ~]
INOTIFY="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html"
RSYNC="rsync -azH --delete --password-file=/etc/server.pass /var/www/html backuper@20.0.0.11::wwwroot/"
$INOTIFY | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ];then
$RSYNC
fi
done
7、运行脚本,在客户端/var/www/html目录下创建文件,查看源端/var/www/html目录是否同步到
客户端:
[root@client ~]
[root@client html]
服务器端:
[root@Rsync ~]
[root@Rsync html]
总用量 0
drwxrwxrwx. 2 nobody nobody 59 11月 12 12:26 html
-rw-r--r--. 1 root root 0 11月 12 11:31 index1.html
-rw-r--r--. 1 root root 0 11月 12 11:33 index2.html
[root@Rsync html]
[root@Rsync html]
总用量 4
-rw-r--r--. 1 nobody nobody 0 11月 12 11:33 index2.html
-rw-r--r--. 1 nobody nobody 14 11月 12 11:27 index.html
-rw-r--r--. 1 nobody nobody 0 11月 12 12:26 test.txt