目录
一、rsync的概述
1.1 关于rsync
- 一款快速增量备份工具
- Remote Sync ,远程同步
- 支持本地复制,或者与其他SSH、rsync主机同步
1.2 rsync源的配置
- 配置文件rsyncd.conf
需手动建立,语法类似于Samba配置
认证配置auth users、secrets file ,不加则为匿名 - rsync账号文件
采用“用户名: 密码” 的记录格式,每行一个用户记录
独立的账号数据,不依赖于系统账号 - rsync服务的启动于关闭
通过–daemon 独自提供服务
关闭rsync服务 kill $(cat /var/run/rsyncd.pid)
1.3 rsync命令的用法
- 基本用法 : rsync [选项] 原始位置 目标位置
-a : 归档模式,递归并保留对象属性,等同于 -rlptgoD 例如属组属主
-v: 显示同步过程的详细(verbose) 信息
-z: 在传输文件时进行压缩(compress)
-H: 保留硬连接位置
-A:保留ACL属性信息
--delete : 删除目标位置有而原始位置没有的文件 #保持两边一致 这条命令要小心
--checksum: 根据对象的校验和来决定是否跳过文件
1.4 rsync的同步
- rsync同步源
指备份操作的远程服务器,也称为备份源
1.5 配置源的表示方法
- 方法一
用户名@主机地址::共享模块名
- 方法二
rsync://用户名@主机地址/共享模块名
1.6 rsync源的免交互处理
- 使用–password-file=文件路径
- 例
vim /etc/server.pass
abc123
[root@localhost opt]# chmod 600 /etc/server.pass
rsync -az --delete --password-file=/etc/server.pass [email protected]::wwwroot /opt/
1.7 rysnc 实时同步
- 定期同步的不足
执行备份的时间固定,延迟明显,实时性差
当同步源长期不变化时,密集的定期任务是不必要的 - 实时同步的优点
一旦同步源出现变化,立即启动备份
只要同步源无变化,则不执行备份
二、inotify
2.1 关于 innotify
- linux内核的inotify机制
- 可以监控文件系统的变动请跨,做出通知响应
- 辅助软件:inotify-tools
2.2 inotify 的内核参数
vim /etc/sysctl.conf ## 写入到 /etc/sysctl.conf 里面
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@localhost ~]# sysctl -p
2.3 inotify辅助工具
inotifywait: 用于持续监控,实时输出结果
innotifywatch: 用于短期监控,任务完成后再出结果
inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html
## -m 持续进行监控 -r 递归监控所有子对象 -q 简化输出信息 -e 指定 监控哪些事件类型
modify 修改 create 创建 move 移动 delete 删除
三、rsyn远同步与innotify结合实时同步
3.1 实验环境
- rsync客户机 192.168.233.140
- rsync同步源: 192.168.233.127
3.2 配置rsync源服务器
[root@localhost ~]# vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
pid file = /var/run/rsyncd.pid
address = 192.168.233.140
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.233.0/24
[wwwroot]
path = /var/www/html
comment = www.kgc.cn ## 文本描述
read only = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
auth users = backuper
secrets file = /etc/rsyncd_users.db
[root@localhost ~]# vim /etc/rsyncd_users.db
backuper:abc123
[root@localhost ~]# chmod 600 /etc/rsyncd_users.db
[root@localhost ~]# rsync --daemon
[root@localhost ~]# netstat -ntap | grep rsync
tcp 0 0 192.168.233.140:873 0.0.0.0:* LISTEN 10105/rsync
[root@localhost ~]# yum -y install httpd
[root@localhost html]# cat index1.html
this is test web2
[root@localhost html]# cat index.html
<h1>this is web1 </h1>
3.2 发起端同步
### 同步方法一
[root@localhost ~]# rsync -avz [email protected]::wwwroot /opt/
Password:
receiving incremental file list
./
index.html
index1.html
sent 65 bytes received 214 bytes 111.60 bytes/sec
total size is 41 speedup is 0.15
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
index1.html index.html rh
##########同步方法二
[root@localhost opt]# rsync -avz rsync://[email protected]/wwwroot /opt/
Password:
receiving incremental file list
./
index.html
index1.html
sent 65 bytes received 214 bytes 111.60 bytes/sec
total size is 41 speedup is 0.15
[root@localhost opt]# ls
index1.html index.html rh
- ######## 方法三 免交互同步
vim /etc/server.pass
abc123
[root@localhost opt]# chmod 600 /etc/server.pass
rsync -az --delete --password-file=/etc/server.pass [email protected]::wwwroot /opt/
3.3 rsync+inotify 结合实时同步
--- 源端口操作
[root@localhost html]# vim /etc/rsyncd.conf
read only = no
---发起端操作
[root@localhost ~]#vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@localhost ~]# sysctl -p
#####################inotify的安装
[root@localhost ~]# yum -y install gcc gcc-c++
准备安装包 inotify-tools-3.14.tar.gz
[root@localhost ~]# tar zxvf inotify-tools-3.14.tar.gz
[root@localhost ~]# cd inotify-tools-3.14/
[root@localhost inotify-tools-3.14]# ./configure
[root@localhost inotify-tools-3.14]# make && make install
- 结合脚本进行实时同步
[root@localhost ~]# vim 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/ [email protected]::wwwroot /opt/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE ## 读取监听输出的记录
do
if [ $(pgrep rsync | wc -l) -le 0 ];then ## rsync未在执行 则立刻启动
$RSYNC_CMD
fi
done
~
[root@localhost ~]# chmod 777 /var/www/html/ ## 基于同步目录权限,两端都可以给
[root@localhost ~]# ./inotify.sh ## 执行脚本 inotify进入监听状态
3.5 测试
- 在xshell上重新开一个窗口连接发起端
- 发起端
[root@localhost html]# cd /var/www/html/
[root@localhost html]# touch acddd.txt
[root@localhost html]# touch acddd1.txt
[root@localhost html]#
[root@localhost ~]# ./inotify.sh ## 查看监听端口输出的信息
/var/www/html/ CREATE acddd.txt
/var/www/html/ ATTRIB acddd.txt
/var/www/html/ CREATE acddd1.txt
/var/www/html/ ATTRIB acddd1.txt
- 同步源查看
[root@localhost ~]# cd -
/var/www/html
[root@localhost html]# ls
acddd1.txt acddd.txt
- 实验成功