项目目录
rsync同步简介
rsync是一款快速增量备份工具
- Remote Sync,远程同步
- 支持本地复制,或者与其他SSH、rsync主机同步
- 官方网站: http://rsync.samba.org
配置rsync备份源
rsync同步源
指备份操作的远程服务器,也称为备份源
rsync命令基本用法
配置rsync源基本思路
1、建立rsyncd.conf配置文件、独立的账号文件
启用rsync的–daemon模式
2、应用示例
- 用户backuper,允许下行同步
- 操作的目录为/var/www/html/
配置文件rsyncd.conf
- 需手动建立,语法类似于Samba配置
- 认证配置auth users、secrets file,不加则为匿名
rsync账号文件
- 采用“用户名:密码”的记录格式,每行一个用户记录
- 独立的账号数据,不依赖于系统账号
启用rsync服务
- 通过–daemon独自提供服务
- 执行kill $(cat /varlrun/rsyncd.pid)关闭rsync服务
rsync命令的用法
rsync [选项] 原始位置 目标位置
常用选项
-a: 归档模式,递归并保留对象属性,等同于-rlptgoD-v:显示同步过程的详细(verbose)信息
-z:在传输文件时进行压缩(compress)-H:保留硬连接文件
-A:保留ACL属性信息
–delete:删除目标位置有而原始位置没有的文件
–checksum:根据对象的校验和来决定是否跳过文件
rsync备份操作示例
配置源的两种表示方法
格式1:用户名@主机地址::共享模块名
格式2: rsync://用户名@主机地址/共享模块名
[root@localhost ~]# rsync -avz
backuper@192.168.4.200::wwwoot /root
[root@localhost ~]# rsync -avz
rsync://backuper@192.168.4.200/wwwroot /root
rsync同步操作示例
下行rsync源: wwwroot共享—>/myweb
[root@localhost ~]# mkdir /myweb
[root@localhost ~]# rsync -avzH --deletebackuper@192.168.4.200::wwwroot /myweb
Password:
receiving incremental file list
./
index.html
index.php
........
rsync实时同步
1、定期同步的不足
- 执行备份的时间固定,延迟明显、实时性差
- 当同步源长期不变化时,密集的定期任务是不必要的
2、实时同步的优点 - —旦同步源出现变化,立即启动备份
- 只要同步源无变化,则不执行备份
rsync源的免交互处理
使用–password-file=密码文件
[root@localhost ~]# cat /etc/server.passpwd123
[root@localhost ~]# chmod 600 /etc/server.pass
[root@localhost ~]# crontab -e
30 22* * */usr/bin/rsync -az --delete --passwordfile=/etclserver.pass
backuper@192.168.4.200::wwwroot /myweb/
[root@localhost ~]# systemctl restart crond
[root@localhost ~]# systemctl enable crond
rsync+inotify结合使用
Linux内核的inotify机制
- 从版本2.6.13开始提供
- 可以监控文件系统的变动情况,并做出通知响应
- 辅肋软件:inotify-tools
调整inotify内核参数
- max_queue_events:监控事件队列大小
- max_user_instances:最多监控实例数
- max_user watches:每个实例最多监控文件数
[root@localhost ~]# vi/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-tools辅助工具
- inotifywait:用于持续监控,实时输出结果
- inotifywatch:用于短期监控,任务完成后再出结果
[root@localhost ~]# inotifywait -mrq -e modify,create,move,delete/var/www/html
Setting up watches. Beware: since -r was given, this may take a while!Watches established.
/var/www/html/ CREATE index.php
/var/www/html/ MODIFY index.php
/var/www/html/ MOVED_FROM index.php
/var/www/html/ MOVED_TO test.php
通过inotifywait触发rsync同步操作
- 使用while、read持续获取监控结果
- 根据结果可以作进—步判断,决定执行何种操作
[root@localhost~]# vi /opt/inotify_rsync.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /varlwww/htmI"
RSYNC CMD="rsync -azH --delete --password-file=letclserver.pass /varlwww/htmI" rput@20.0.100:/var/www/html"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE #读取输出的监控记录
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then #如果rsync未在执行,则立即启动
$RSYNC_CMD
fi
done
配置rsync源服务器
rsync–源服务器设置
1、安装rsync
[root@rsync-yunan ~]# yum -y install rsync.x86_64
2、修改rsync配置文件
[root@rsync-yunan ~]# vim /etc/rsyncd.conf
uid = nobody 去掉#
gid = nobody 去掉#
use chroot = yes 去掉#,锁定宿主目录
address = 20.0.0.10 #增加
port 873 #增加
log file = /var/log/rsyncd.log #增加
hosts allow = 20.0.0.0/24 #增加
pid file = /var/run/rsyncd.pid 去掉#
增加下面内容:
[wwwroot]
path = /var/www/html
comment = web
read only = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
auth users = abc
secrets file = /etc/rsyncd_users.db
[root@rsync-yunan ~]# vim /etc/rsyncd_users.db
abc:123456
[root@rsync-yunan ~]# chmod 600 /etc/rsyncd_users.db
[root@rsync-yunan ~]# rsync --daemon
[root@rsync-yunan ~]# yum -y install httpd
[root@rsync-yunan ~]# cd /var/www/html/
[root@rsync-yunan html]# echo "<h1>this is web</h1>" >index.html
[root@rsync-yunan html]# systemctl start httpd.service
rsync–发起方服务器设置
[root@rsync-fa ~]# yum -y install httpd
1、请求源服务器进行同步方法一
[root@rsync-fa ~]# rsync -avz abc@20.0.0.10::wwwroot /var/www/html/ #请求源服务器进行同步
Password:
receiving incremental file list
./
index.html
sent 49 bytes received 122 bytes 48.86 bytes/sec
total size is 21 speedup is 0.12
[root@rsync-fa ~]# systemctl start httpd.service #启动httpd服务
[root@rsync-fa ~]# curl http://localhost #测试数据是否同步
<h1>this is web</h1>
[root@rsync-fa ~]# cd /var/www/html/
[root@rsync-fa html]# ll
总用量 4
-rw-r--r-- 1 root root 21 11月 12 14:52 index.html
2、请求源服务器进行同步方法二
[root@rsync-fa html]# rsync -avz rsync://abc@20.0.0.10/wwwroot /var/www/html/
Password:
receiving incremental file list
./
index.html
sent 49 bytes received 122 bytes 68.40 bytes/sec
total size is 21 speedup is 0.12
[root@rsync-fa html]# curl http://localhost
<h1>this is web</h1>
免密登录
[root@rsync-fa html]# vim /opt/abc.txt
123456
[root@rsync-fa html]# chmod 600 /etc/passwd.txt
[root@rsync-fa opt]# rsync -avz --password-file=/opt/abc.txt abc@20.0.0.10::wwwroot /var/www/html/
receiving incremental file list
./
index.html
sent 49 bytes received 122 bytes 342.00 bytes/sec
total size is 21 speedup is 0.12
搭建rsync+inotify
1、将rsync源服务器只读模式关闭
[root@rsync-yunan ~]# vim /etc/rsyncd.conf
read only = yes #yes改为no
2、客户端更改内核参数,指定队列大小,最多监控实例数,每个实例最多监控文件数
[root@rsync-yunan ~]# vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384 #增加,#监控事件的最大队列数
fs.inotify.max_user_instances = 1024 #增加,#监控的最大实例数
fs.inotify.max_user_watches = 1048576 #增加, #监控的每实例的最大文件数
3、重启rsyncd
[root@rsync-yunan ~]# pkill rsyncd
[root@rsync-yunan run]# rsync --daemon
4、配置rsync--发起方服务器
解压缩inotify并安装
inotifywait:用于持续监控,实时输出结果
inotifywatch:用于短期监控,任务完成后再出结果
4.1导入数据包
[root@rsync-fa opt]# ll
总用量 352
-rw-r--r-- 1 root root 358772 11月 12 15:57 inotify-tools-3.14.tar.gz
4.2解压数据包
[root@rsync-fa opt]# tar zxvf inotify-tools-3.14.tar.gz
[root@rsync-fa opt]# cd inotify-tools-3.14/
4.3、编译安装
[root@rsync-fa inotify-tools-3.14]# ./config
[root@rsync-fa inotify-tools-3.14]# make && make istall
4.4测试inotifywait监控命令是否正常使用
4.5执行完上面最后一条命令后处于监控状态,不能做输入操作,在开启一个新的终端设备
[root@rsync-fa ~]# cd /var/www/html/
[root@rsync-fa html]# touch a.txt
监控端查看
[root@rsync-fa inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html
/var/www/html/ CREATE a.txt
4.6客户端上编写脚本,将inotify监控和rsync远程同步结合起来
[root@rsync-fa ~]# vim /opt/inotify.sh
#!/bin/bash
INOTIFY="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html"
RSYNC="rsync -azH --delete --password-file=/opt/abc.txt /var/www/html/ abc@20.0.0.10::wwwroot/"
$INOTIFY | while read DIRECTORY EVENT FILE #逐条读取监控记录
do
if [ $(pgrep rsync | wc -l) -le 0 ];then
$RSYNC
fi
done
[root@rsync-fa ~]# chmod +x /opt/inotify.sh
[root@rsync-fa opt]# chmod +x /opt/inotify.sh
[root@rsync-fa opt]# chmod 777 /var/www/html/
[root@rsync-yunan ~]# chmod 777 /var/www/html/
[root@rsync-fa opt]# ./inotify.sh
测试
[root@rsync-fa html]# touch abc
[root@rsync-yunan html]# ll
总用量 4
-rw------- 1 nobody nobody 0 11月 12 17:19 abc