rsync远程同步
rsync
一款快速增量备份工具
- Remote Sync,远程同步
- 支持本地复制,或者与其他SSH、rsync主机同步
- 官方网站: httop://rsync.samba.org
配置rsync源
[root@localhost ~]
基本思路
- 建立rsyncd.conf配置文件、独立的账号文件
- 启用rsync的–daemon模式
配置文件rsyncd.conf
- 需要手动建立、语法类似于Samba配置
- 认证配置auth users、secrets file,不加则为匿名
配置rsync源服务器
[root@localhost ~]
uid = nobody
gid = nobody
use chroot = yes
address = 192.168.45.133
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.45.0/24
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
[wwwroot]
path = /var/www/html
comment = www.kgc.cn
read only = yes
auth users = backuper
secrets file = /etc/rsyncd_users.db
rsync账号文件
- 采用“用户名:密码”的记录格式,每行一个用户记录
- 独立的账号数据,不宜懒于系统账号
[root@localhost ~]
backuper:abc123
[root@localhost ~]
启用rsync服务
- 通过–deamon服务独自提供服务
- 执行kill $(cat /var/run/rsyncd.pid)关闭rsync服务
[root@localhost ~]
[root@localhost ~]
tcp 0 0 192.168.45.133:873 0.0.0.0:* LISTEN 100732/rsync
配置网站文件
主服务器
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost html]
[root@localhost html]
[root@localhost www]
客户机
root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost www]
rsync命令的用法
常用选项
- -a:归档模式,递归并保留对象属性,等同于-rlptgoD
- -v:显示同步过程的详细(verbose)信息
- -z:在传递文件时进行压缩(compress)
- -H:保留硬链接文件
- -A: 保留ACL属性信息
- –delete:删除目标位置有而原始位置没有的文件
- –checksum:根据对着的校验和来决定是否跳过文件
配置源的两种方法
格式1:用户名@主机地址::共享模块名
格式2:rsync://用户名@主机地址/共享模块名
[root@localhost ~]
backuper@192.168.45.132::wwwroot/root
[root@localhost ~]
rsync:
第一种方式
[root@localhost www]
Password:
receiving incremental file list
./
index.html
sent 83 bytes received 172 bytes 14.57 bytes/sec
total size is 17 speedup is 0.07
第二种方式
[root@localhost html]
[root@localhost html]
[root@localhost html]
Password:
receiving incremental file list
./
index.html
sent 83 bytes received 172 bytes 72.86 bytes/sec
total size is 17 speedup is 0.07
[root@localhost html]
index.html
rsync同步操作示例
- 下行rsync源:wwwroot共享——>myweb
rsync源的免交互处理
[root@localhost html]
[root@localhost html]
[root@localhost html]
abc123
[root@localhost html]
receiving incremental file list
./
index.html
sent 83 bytes received 172 bytes 24.29 bytes/sec
total size is 17 speedup is 0.07
[root@localhost html]
[root@localhost html]
index.html
rsync实时同步
定期同步的不足
- 执行备份的时间固定,延迟明显,实时性差
- 当同步源长期不变化时,密集的定期任务是不必要的
实时同步的优点
- 一旦同步源出现变化,立即启动备份
- 只要同步源无变化,则不执行备份
inotify
linux内核的inotify机制
- 从版本2.6.13开始提供
- 可以监控文件系统的变动情况,并作出通知响应
- 辅助软件:inotify-tools
rsync+inotify实时同步
调整inotify内核参数
- max_queue_events:监控队列大小
- max_user_instances:最多监控实例数
- max_user_watches:每个实例最多监控文件数
[root@localhost html]
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@localhost html]
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
安装inotify-tools辅助工具
- inotifywait:用于持续监控,实时输出结果
- inotifywatch:用于短期监控,任务完成后再除结果
[root@localhost html]
Password for root@
[root@localhost html]
[root@localhost mnt]
[root@localhost mnt]
[root@localhost inotify-tools-3.14]
[root@localhost inotify-tools-3.14]
[root@localhost inotify-tools-3.14]
[root@localhost inotify-tools-3.14]
通过inotifywait触发rsync同步操作
[root@localhost ~]
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync-azH --delete --password-file=/etc/services.pass /var/www/html/ backuper@192.168.45.130::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ];then
$RSYNC_CMD
fi
done