文章目录
一、rsync同步简介
1.1 关于rsync
- 一款快速增量备份工具
- Remote Sync,远程同步
- 支持本地恢复,或者与其他SSH、rsync主机同步
- 官方网站:http://rsync.samba.org
二、配置rsync备份源
2.1 rsync同步源
- 指备份操作的远程服务器,也称为备份源
2.2 配置rsync源
-
基本思路
- 建立rsync。conf配置文件、独立的账号文件
- 启用rsync的–daemon模式
-
应用示例
- 用户backuper,允许下行同步
- 操作的目录为/var/www/html
-
配置文件rsync.conf
- 需手动建立,语法类似于Samba配置
- 认证配置auth users、secrets file,不加则为匿名
-
rsync账号文件
- 采用 “用户名:密码” 的记录格式,每行一个用户记录
- 独立的账号数据,不依赖于系统账号
-
启用rsync服务
- 通过–daemon独自提供服务
-
关闭rsync服务
- 执行 kill $(cat /var/run/rsync.pid)
三、rsync命令基本用法
3.1 使用rsync备份工具
rsync命令的用法
rsync [选项] 原始位置 目标位置
常用选项
- -a:归档模式,递归并保留对象属性,等同于-rlptgoD
- -v:显示同步过程的详细(verbose)信息
- -z:在传输文件时进行压缩(compress)
- -H:保留硬链接文件
- -A:保留ACL属性信息
- –delete:删除目标位置有,而原始位置没有的文件
- –checksum:根据对象的校验和来决定是否跳过文件
配置源的两种表示方法
格式1:用户名@主机地址::共享模块名
格式:rsync://用户名@主机地址/共享模块名
[root@localhost ~]# rsync -avz
backuper@192.168.4.200::wwwroot /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 --delete
backuper@192.168.4.200::wwwroot /myweb
Password:
receiving incremental file list
./
index.html
index.php
......
四、rsync备份操作实例
-------配置rsync源服务器------
hostnamectl set-hostname source
su
iptables -F
setenforce 0
vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
port 873
log file = /var/log/rsync.log
pid file = /var/run/rsyncd.pid
hosts allow = 14.0.0.0/24
[wwwroot]
path = /var/www/html
comment = www.yyc.cn
read only = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
auth users = backuper
secrets file = /etc/rsyncd_users.db
vim /etc/rsyncd_users.db
backuper:19961207
chmod 600 /etc/rsyncd_users.db
##启动服务
rsync --daemon
##查看状态
[root@source etc]# netstat -ntap | grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 114655/rsync
tcp6 0 0 :::873 :::* LISTEN 114655/rsync
###################################
--源服务器
yum install httpd -y
cd /var/www/html
vim index.html
<h1>this is test web</h1>
echo "this is yyc web" > web.html
------发起端-------
格式一:
rsync -avz backuper@14.0.0.11::wwwroot /opt/ ##密码19961207
格式二:
rsync -avz rsync://backuper@14.0.0.11::wwwroot /opt/
#免交互:(慎用!!!用之前做好备份)
vim /etc/server.pass
19961207
chmod 600 /etc/server.pass
rsync -az --delete --password-file=/etc/server.pass backuper@14.0.0.11::wwwroot /opt/
'会把/opt/路径下的所有原有非源文件删了,慎用!'
五、rsync+inotify结合使用
5.1 rsync实时同步
-
定期同步的不足
- 执行备份的时间固定,延迟明显、实时性差
- 当同步源长期不变化时,密集的定期任务是不必要的
-
实时同步的优点
- 一旦同步源出现变化,立即启动备份
- 只要同步源无变化,则不执行备份
5.2 关于inotify
- Linux内核的inotify机制
- 从版本1.6.13开始提供
- 可以监控文件系统的变动情况,并做出通知响应
- 辅助软件:inotify-tools
5.3 rsync+inotify实时同步
- 调整inotify内核参数
- max_queue_events:监控事件队列大小
- max_user_instances:最多监控实例数
- max_user_watches:每个实例最多监控文件数
[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-tools辅助工具
- initifywait:用于持续监控,实时输出结果
- inotifywatch:用于短期监控,任务完成后再输出结果
[root@localhost ~]# inotifywait -mrq -e modify,create,move,delete /var/www/html
'-m,持续进行监控'
'-r,递归监控所有子对象'
'-q,简化输出信息'
'-e,指定要监控哪些事件类型'
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_YO test.php
- 通过inotifywait触发rsync同步操作
- 使用while、read持续读取监控操作
- 根据结果可以作进一步判断,决定执行何种操作
[root@localhost ~]# vim /opt/inotify_rsync.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/ rput@192.168.4.200:/var/www/html"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE '读取输出的监控记录'
do
if [ $(pgrep rsync | wc -l) -le 0 ];then
$RSYNC_CMD '如果rsync未在进行,则立即启动'
fi
done
六、rsync+inotify实例
--------------rsync+inotify(发起端)-----------
注意:源端设置:read only = no
yum install httpd -y
yum install gcc gcc-c++ -y ##如已安装则不需要再安装
##将rsync源服务器/var/www/html/路径下所有页面都删掉
#调整inotify内核参数
vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
sysctl -p ##生效
##拖入软件包 inotify-tools-3.14.tar.gz
tar zxvf inotify-tools-3.14.tar.gz -C /opt/
cd /opt/inotify-tools-3.14/
./configure
make && make install
#安装inotify-tools辅助工具
inotifywait -mrq -e modify,create,move,delete /var/www/html
#通过inotifywait触发rsync同步操作
cd /opt/
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/ backuper@14.0.0.11::ww
wroot/"
$INOTIFY_CDM | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done
chmod +x inotify.sh
#rsync源服务器和发起端都要加权限
chmod 777 /var/www/html/
./inotify.sh