一.rsync同步简介
关于rsync
1.一款快速增量备份工具
2、Remote Sync,远程同步
3、支持本地复制,或者与其他SSH、rsync主机同步
4、官方网站:http://rsync.samba.org
二.配置rsync源服务器
1.rsync同步源:指备份操作的远程服务器,也称为备份源
2.基本思路
1、建立rsyncd.conf配置文件、独立的账户文件
2、启用rsync的--daemon模式
3.应用示例
1、用户backuper,允许下行同步
2、操作的目录为/var/www/html
4.配置文件rsync.conf
1、需手动建立,语法类似于Samba配置
2、认证配置auth users、secrets file,不加则为匿名
5.rsync账户文件
1、采用“用户名:密码”的记录格式,每行一个用户记录
2、独立的账户数据,不依赖于系统账号
6.启用rsync服务
通过--daemon独自提供服务
三.使用rsync备份工具
1.rsync命令的用法
语法
rsync [选项] 原始位置 目标位置常用选项
-a: 归档模式,递归并保留对象属性,等同于-rlptgoD
-v: 显示同步过程的详细(verbose)信息
-z: 在传输文件时进行压缩(compress)
-H: 保留硬连接文件
-A: 保留ACL属性信息
-p: 保留文件的权限标记
-t: 保留文件的时间标记
-g: 保留文件的属组标记
-o: 保留文件的属主标记
-delete: 删除目标位置有而原始位置没有的文件
-checksum: 根据对象的校验和来决定是否跳过文件
2.配置源的俩种表示方法
格式1:用户名@主机地址::共享模块名
格式2: rsync://用户名@主机地址共享模块名eg:
[root@localhost ~]# rsync -avz
backuper@ 192.168.4.200/wwwroot /root
[root@localhost ~]# rsync -avz
rsync://backuper@192.168.4.200/wwwroot /root
四.实例操作(定时同步)
1.环境:服务器A(rsync同步源)ip:192.168.174.51;服务器B(客户机)ip:192.168.174.52
2.配置rsync源服务器
1.检查rsync是否安装,安装http服务
[root@rsync ~]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64
[root@rsync ~]# yum -y install httpd
2.修改rsync配置文件
[root@rsync ~]# vi /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes #禁锢在宿主目录中
address = 192.168.174.51 #监听地址
port 873 #端口号
# max connections = 4
pid file = /var/run/rsyncd.pid #进程文件位置
log file = /var/log/rsyncd.log #日志文件位置
hosts allow = 192.168.174.0/24 #允许地址范围
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 #不再压缩这几种格式的文件
[wwwroot]
path = /var/www/html #同步的目录
comment = web dir
read only = yes #只读
auth users =backuper #添加用户
secrets file = /etc/rsyncd_users.db #用户密码存放位置
3.创建用户密码文件,提权
[root@rsync ~]# vi /etc/rsyncd_users.db
backuper:abc123
~
[root@rsync ~]# chmod 600 /etc/rsyncd_users.db
4.启动rsync
[root@rsync ~]# rsync --daemon
[root@rsync ~]# netstat -anpt | grep rsync
tcp 0 0 192.168.174.51:873 0.0.0.0:* LISTEN 4368/rsync
5.创建共享目录
[root@rsync ~]# echo "this is new web" > /var/www/html/index.html
[root@rsync ~]# cat /var/www/html/index.html
this is new web
[root@rsync ~]# systemctl start httpd
[root@rsync ~]# curl http://localhost
this is new web
6.客户机测试
方法一
[root@client ~]# rsync -avz backuper@192.168.174.51::wwwroot /var/www/html/
Password:
receiving incremental file list
./
index.html
[root@client ~]# systemctl restart httpd
[root@client ~]# curl http://localhost
this is new web
方法二
[root@client ~]# rsync -avz rsync://backuper@192.168.174.51/wwwroot /var/www/html/
Password:
receiving incremental file list
./
index.html
sent 83 bytes received 171 bytes 56.44 bytes/sec
total size is 16 speedup is 0.06
[root@client ~]# curl http://localhost
this is new web
修改客户机文件
[root@client ~]# vi /var/www/html/index.html
abcthis is new web
~
~
再次同步发现文件被覆盖
[root@client ~]# rsync -avz rsync://backuper@192.168.174.51/wwwroot /var/www/html/
Password:
receiving incremental file list
./
index.html
sent 89 bytes received 171 bytes 57.78 bytes/sec
total size is 16 speedup is 0.06
[root@client ~]# curl http://localhost
this is new web
7.免密登录
创建免密登录文件
[root@client ~]# vi /etc/moon
[root@client ~]# chmod 600 /etc/moon
[root@client ~]# rm -rf /opt/index.html
[root@client ~]# rsync -avz --delete --password-file=/root/moon backuper@192.168.174.51::wwwroot /var/www/html/
receiving incremental file list
deleting nt.txt
deleting nc.txt
./
sent 64 bytes received 116 bytes 120.00 bytes/sec
total size is 16 speedup is 0.09
五.rsync实时同步
定期同步的不足:
- 执行备份的时间固定,延迟明显、实时性差
- 当同步源长期不变化时,密集的定期任务是不必要的
实时同步的优点:
- 一旦同步源出现变化,立即启动备份
- 只要同步源无变化,则不执行备份
关于inotify
- Linux内核的inotify机制
- 从版本2.6.13开始提供
- 可以从监控文件系统的变动情况,并做出通知响应
- 辅助软件:inotify-tools
配置rsync源服务器
1.修改rsync配置文件
[root@rsync ~]# chmod -R 777 /var/www/html/
[root@rsync ~]# vi /etc/rsyncd.conf
read only = no #关闭只读模式
2.客户端更改内核参数
[root@client ~]# chmod -R 777 /var/www/html/
[root@client ~]# vi /etc/sysctl.conf
fs.inotify.max_queued_events = 16384 #指定队列大小
fs.inotify.max_user_instances = 1024 #最多监控实例数
fs.inotify.max_user_watches = 1048576 #每个实例最多监控文件数
3.重新启动rsync服务
重启服务
[root@rsync ~]# pkill -9 rsync
[root@rsync ~]# netstat -anpt | grep rsync
[root@rsync ~]# rsync --daemon
[root@rsync ~]# netstat -anpt | grep rsync
tcp 0 0 192.168.174.51:873 0.0.0.0:* LISTEN 4368/rsync
4.客户机安装inotify工具
inotifywait:用于持续监控,实时输出结果
inotifywatch:用于短期监控,任务完成后再出结果
[root@client ~]# tar xf inotify-tools-3.14.tar.gz
[root@client ~]# cd inotify-tools-3.14/
[root@client inotify-tools-3.14]# ./configure
[root@client inotify-tools-3.14]# make && make install
测试inotify运行
[root@client ~]# cd /var/www/html/
[root@client html]# echo "abc" > a.html
[root@client html]# mkdir moon.txt
[root@client ~]# inotifywait -mrq -e modify,create,move,delete /var/www//html/
/var/www//html/ CREATE a.html
/var/www//html/ MODIFY a.html
/var/www//html/ CREATE,ISDIR moon.txt
编写脚本,将inotify和rsync同步
[root@client ~]# vi inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,move,delete /var/www//html/"
RSYNC_CMD="rsync -avzH --password-file=/root/moon /var/www/html backuper@192.168.174.51::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l ) -le 0 ]
then $RSYNC_CMD
fi
done
运行脚本
[root@client opt]# ./inotify.sh
测试同步
[root@client ~]# ./inotify.sh
sending incremental file list
html/
html/1
html/2
html/a.html
html/index.html
html/abc/
html/moon.txt/
sent 335 bytes received 96 bytes 862.00 bytes/sec
total size is 20 speedup is 0.05
sending incremental file list
html/
html/edg/
sent 182 bytes received 18 bytes 133.33 bytes/sec
total size is 20 speedup is 0.10