文章目录
rsync服务基本介绍
Rync服务器
Rsync是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具。并且可以不进行改变原有数据的属性信息,实现数据的备份迁移特性。Rsync.软件适用于unix/linux/windows等多种操作系统平台。Rsync是一个快速和非常通用的文件复制工具。它能本地复制,远程复制,或者远程守护进程方式复制。它提供了大量的参数来控制其行为的各个方面,并且允许非常灵活的方式来实现文件的传输复制。它以其delta-transfer 算法闻名。rsync监听端口:873 rsync运行模式:C/S
rsync实时同步
定期同步的不足
- 执行备份的时间固定,延迟明显、实时性差
- 当同步源长期不变化时,密集的定期任务是不必要的
实时同步的优点 - 一旦同步源出现变化,立即启动备份
- 只要同步源无变化,则不执行备份
inotify
Linux内核的inotify机制
- 从版本2.6.13开始提供
- 可以监控文件系统的变动情况,并做出通知响应
- 辅助软件:inotify-tools
调整inotify内核参数
- max_queue_events:监控事件队列大小
- max_user_instances:最多监控实例数
- max user watches:每个实例最多监控文件数
安装inotify-tools辅助工具
- inotifywait:用于持续监控,实时输出结果
- inotifywatch:用于短期监控,任务完成后再出结果
inotify命令
- #m:持续监听
- #-r:使用递归形式监视目录
- #-q:减少冗余信息,只打印出需要的信息
- #-e指定要监视的事件,多个时间使用逗号隔开
rsync命令参数
-a #归档模式传输, 等于-tropgDl
-v #详细模式输出, 打印速率, 文件数量等
-z #传输时进行压缩以提高效率
-r #递归传输目录及子目录,即目录下得所有目录都同样传输。
-t #保持文件时间信息
-o #保持文件属主信息
-p #保持文件权限
-g #保持文件属组信息
-l #保留软连接
-P #显示同步的过程及传输时的进度等信息
-D #保持设备文件信息
-L #保留软连接指向的目标文件
-e #使用的信道协议,指定替代 rsh 的 shell 程序
–exclude=PATTERN #指定排除不需要传输的文件模式
–exclude-from=file #文件名所在的目录文件
–bwlimit=100 #限速传输
–partial #断点续传
–delete #让目标目录和源目录数据保持一致
全量备份
在server1上
[root@server1 ~]# mkdir /backup
[root@server1 ~]# cd /backup/
[root@server1 backup]# touch a b c
[root@server1 backup]# ls -lh
总用量 0
-rw-r--r--. 1 root root 0 12月 30 15:57 a
-rw-r--r--. 1 root root 0 12月 30 15:57 b
-rw-r--r--. 1 root root 0 12月 30 15:57 c
[root@server1 backup]# cd
[root@server1 ~]# rpm -qa | grep rsync
rsync-3.0.9-18.el7.x86_64
[root@server1 ~]# rsync -avz /backup /opt
sending incremental file list
backup/
backup/a
backup/b
backup/c
sent 176 bytes received 73 bytes 498.00 bytes/sec
total size is 0 speedup is 0.00
[root@server1 ~]# cd /opt/
[root@server1 opt]# ls -lh
总用量 0
drwxr-xr-x. 2 root root 33 12月 30 15:57 backup
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
[root@server1 opt]# cd
[root@server1 ~]# cd /a
[root@server1 a]# touch 1 2 3
[root@server1 a]# cd
[root@server1 ~]# rsync -avz /a/ /opt/
sending incremental file list
./
1
2
3
sent 159 bytes received 72 bytes 462.00 bytes/sec
total size is 0 speedup is 0.00
[root@server1 ~]# cd /opt/
[root@server1 opt]# ls -lh
总用量 0
-rw-r--r--. 1 root root 0 12月 30 15:59 1
-rw-r--r--. 1 root root 0 12月 30 15:59 2
-rw-r--r--. 1 root root 0 12月 30 15:59 3
drwxr-xr-x. 2 root root 33 12月 30 15:57 backup
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
增量备份
在server2上
[root@server2 ~]# mkdir /bak
在server1上
[root@server1 opt]# rsync -avz /backup root@20.0.0.11:/bak
The authenticity of host '20.0.0.11 (20.0.0.11)' can't be established.
ECDSA key fingerprint is SHA256:kMJc6r0eXxa/3K7BTIY3Qx/1iIm3JjNxZSPZjEpfJGE.
ECDSA key fingerprint is MD5:fd:2a:2e:5d:cd:e1:0d:2b:39:fa:f9:11:b9:4d:34:09.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '20.0.0.11' (ECDSA) to the list of known hosts.
root@20.0.0.11's password:
sending incremental file list
backup/
backup/a
backup/b
backup/c
sent 176 bytes received 73 bytes 45.27 bytes/sec
total size is 0 speedup is 0.00
在server2上
[root@server2 ~]# cd /bak/
[root@server2 bak]# ls -lh
总用量 0
drwxr-xr-x. 2 root root 33 12月 30 15:57 backup
[root@server2 bak]# cd backup/
[root@server2 backup]# ls -lh
总用量 0
-rw-r--r--. 1 root root 0 12月 30 15:57 a
-rw-r--r--. 1 root root 0 12月 30 15:57 b
-rw-r--r--. 1 root root 0 12月 30 15:57 c
备份成功
server1
[root@server1 opt]# cd /opt/
[root@server1 opt]# ls -lh
总用量 0
-rw-r--r--. 1 root root 0 12月 30 15:59 1
-rw-r--r--. 1 root root 0 12月 30 15:59 2
-rw-r--r--. 1 root root 0 12月 30 15:59 3
drwxr-xr-x. 2 root root 33 12月 30 15:57 backup
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
[root@server1 opt]# rsync -avz /opt/ root@20.0.0.11:/bak
root@20.0.0.11's password:
sending incremental file list
./
1
2
3
rh/
//因为backup已存在,所以增量备份不会再次备份
sent 235 bytes received 77 bytes 89.14 bytes/sec
total size is 0 speedup is 0.00
server2
[root@server2 ~]# cd /bak/
[root@server2 bak]# ls -lh
总用量 0
-rw-r--r--. 1 root root 0 12月 30 15:59 1
-rw-r--r--. 1 root root 0 12月 30 15:59 2
-rw-r--r--. 1 root root 0 12月 30 15:59 3
drwxr-xr-x. 2 root root 33 12月 30 15:57 backup
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
增量备份成功
代替删除(相当于全覆盖)
注:源目录中的文件对目标目录做全覆盖
server1
[root@server1 ~]# mkdir /www
[root@server1 ~]# cd /www/
[root@server1 www]# echo "this is aaa" > index.html
[root@server1 www]# ls -lh
总用量 4.0K
-rw-r--r--. 1 root root 12 12月 30 16:15 index.html
[root@server1 www]# cd
[root@server1 ~]# rsync -avz --delete /www/ root@20.0.0.11:/bak
root@20.0.0.11's password:
sending incremental file list
./
deleting rh/
deleting backup/c
deleting backup/b
deleting backup/a
deleting backup/
deleting 3
deleting 2
deleting 1
index.html
sent 103 bytes received 34 bytes 39.14 bytes/sec
total size is 12 speedup is 0.09
server2
[root@server2 ~]# cd /bak/
[root@server2 bak]# ls -lh
总用量 4.0K
-rw-r--r--. 1 root root 12 12月 30 16:15 index.html
替代删除成功
远程数据备份
rsync下拉操作
server2
[root@server2 ~]# cd /opt/
[root@server2 opt]# vi 111
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
[root@server2 opt]# ls -lh
总用量 4.0K
-rw-r--r--. 1 root root 133 12月 30 16:22 111
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
server1
[root@server1 ~]# rsync -avz root@20.0.0.11:/opt/ /opt
root@20.0.0.11's password:
receiving incremental file list
./
111
sent 34 bytes received 114 bytes 59.20 bytes/sec
total size is 133 speedup is 0.90
[root@server1 ~]# cd /opt/
[root@server1 opt]# ls -lh
总用量 4.0K
-rw-r--r--. 1 root root 0 12月 30 15:59 1
-rw-r--r--. 1 root root 133 12月 30 16:22 111
-rw-r--r--. 1 root root 0 12月 30 15:59 2
-rw-r--r--. 1 root root 0 12月 30 15:59 3
drwxr-xr-x. 2 root root 33 12月 30 15:57 backup
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
[root@server1 opt]# cat 111
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
下拉操作成功
rsync上传操作
[root@server1 ~]# cd /opt/
[root@server1 opt]# vi 123
[root@server1 opt]# ls -lh
总用量 8.0K
-rw-r--r--. 1 root root 0 12月 30 15:59 1
-rw-r--r--. 1 root root 133 12月 30 16:22 111
-rw-r--r--. 1 root root 7 12月 30 16:28 123
-rw-r--r--. 1 root root 0 12月 30 15:59 2
-rw-r--r--. 1 root root 0 12月 30 15:59 3
drwxr-xr-x. 2 root root 33 12月 30 15:57 backup
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
[root@server1 ~]# rsync -avz /opt/ root@20.0.0.11:/opt/
root@20.0.0.11's password:
sending incremental file list
1
123
2
3
backup/
backup/a
backup/b
backup/c
sent 411 bytes received 150 bytes 224.40 bytes/sec
total size is 140 speedup is 0.25
server2
[root@server2 opt]# ls -lh
总用量 8.0K
-rw-r--r--. 1 root root 0 12月 30 15:59 1
-rw-r--r--. 1 root root 133 12月 30 16:22 111
-rw-r--r--. 1 root root 7 12月 30 16:28 123
-rw-r--r--. 1 root root 0 12月 30 15:59 2
-rw-r--r--. 1 root root 0 12月 30 15:59 3
drwxr-xr-x. 2 root root 33 12月 30 15:57 backup
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
上传操作成功
rsync守护进程部署
server1
修改配置文件
[root@server1 ~]# vi /etc/rsyncd.conf
# configuration example:
uid = nobody //匿名,删除前面注释符
gid = nobody //匿名,删除前面注释符
use chroot = no //不锁定宿主目录,删除前面注释符
max connections = 200 //后台最大连接数,删除注释符
pid file = /var/run/rsyncd.pid //删除注释符
log file = /var/log/rsync.log //添加日志目录
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 //指明以那些为后缀的文件不压缩
hosts allow = 20.0.0.0/24
address = 20.0.0.10
port 873
[wwwroot] //复制目录修改内容,及以下内容
path = /www //目录位置
comment = web site page //目录描述
read only = yes //只读权限
auth users = zhangsan //授权账户名
secrets file = /etc/user.db //账户信息存储文件(用户数据库文件)
设置值用户信息
[root@server1 ~]# vi /etc/user.db
zhangsan:123456 //用户名和密码用:隔开
查看服务状态
[root@server1 ~]# chmod 600 /etc/user.db //授予只读权限
[root@server1 ~]# rsync --daemon //重启服务
[root@server1 ~]# netstat -anpt | grep 873 //查看服务状态
tcp 0 0 20.0.0.10:873 0.0.0.0:* LISTEN 56540/rsync
创建新文件
[root@server1 ~]# cd /www/
[root@server1 www]# touch x y z
[root@server1 www]# ls -lh
总用量 4.0K
-rw-r--r--. 1 root root 12 12月 30 16:15 index.html
-rw-r--r--. 1 root root 0 12月 30 16:57 x
-rw-r--r--. 1 root root 0 12月 30 16:57 y
-rw-r--r--. 1 root root 0 12月 30 16:57 z
server2
方法1:
[root@server2 opt]# rsync -avz zhangsan@20.0.0.10::wwwroot /bak
Password:
receiving incremental file list
./
x
y
z
sent 121 bytes received 250 bytes 148.40 bytes/sec
total size is 12 speedup is 0.03
[root@server2 opt]# cd /bak/
[root@server2 bak]# ls -lh
总用量 4.0K
-rw-r--r--. 1 root root 12 12月 30 16:15 index.html
-rw-r--r--. 1 root root 0 12月 30 16:57 x
-rw-r--r--. 1 root root 0 12月 30 16:57 y
-rw-r--r--. 1 root root 0 12月 30 16:57 z
成功获取信息,配置成功
方法2:
[root@server2 ~]# mkdir /bak2
[root@server2 ~]# rsync -avz rsync://zhangsan@20.0.0.10/wwwroot /bak2
Password:
receiving incremental file list
./
index.html
x
y
z
sent 140 bytes received 301 bytes 176.40 bytes/sec
total size is 12 speedup is 0.03
[root@server2 ~]# cd /bak2
[root@server2 bak2]# ls -lh
总用量 4.0K
-rw-r--r--. 1 root root 12 12月 30 16:15 index.html
-rw-r--r--. 1 root root 0 12月 30 16:57 x
-rw-r--r--. 1 root root 0 12月 30 16:57 y
-rw-r--r--. 1 root root 0 12月 30 16:57 z
方法三:免交互
[root@server2 ~]# vi password //放入zhangsan密码
123456
[root@server2 ~]# ls -lh
总用量 16K
-rw-------. 1 root root 2.3K 12月 12 22:52 anaconda-ks.cfg
-rw-r--r--. 1 root root 2.3K 12月 12 22:53 initial-setup-ks.cfg
-rw-r--r--. 1 root root 228 12月 12 23:17 local.repo
-rw-r--r--. 1 root root 7 12月 30 17:07 password
drwxr-xr-x. 2 root root 6 12月 12 22:54 公共
drwxr-xr-x. 2 root root 6 12月 12 22:54 模板
drwxr-xr-x. 2 root root 6 12月 12 22:54 视频
drwxr-xr-x. 2 root root 6 12月 12 22:54 图片
drwxr-xr-x. 2 root root 6 12月 12 22:54 文档
drwxr-xr-x. 2 root root 6 12月 12 22:54 下载
drwxr-xr-x. 2 root root 6 12月 12 22:54 音乐
drwxr-xr-x. 2 root root 6 12月 12 22:54 桌面
[root@server2 ~]# chmod 600 password //设置zhangsan密码文件夹为只读权限,密码文件必须不能为其他人可读
[root@server2 ~]# rsync -avz --password-file=/root/password rsync://zhangsan@20.0.0.10/wwwroot /bak2
receiving incremental file list
sent 61 bytes received 139 bytes 400.00 bytes/sec
total size is 12 speedup is 0.06 //由于之前成功,因此本次没有文件生成
rsync+inotify
server1、server2上创建html目录,并安装httpd
[root@server1 ~]# mkdir -p /var/www/html
[root@server1 ~]# yum -y install httpd
[root@server1 ~]# systemctl start httpd
[root@server1 ~]# netstat -anpt | grep httpd
tcp6 0 0 :::80 :::* LISTEN 56890/httpd
server2
[root@server2 ~]# cd /var/www/html/
[root@server2 html]# ls -lh
总用量 0
[root@server2 html]# vi index.html
this is aaa!!!
server1
[root@server1 ~]# vi /etc/rsyncd.conf
[wwwroot]
path = /var/www/html //修改
comment = web site page
read only = yes
auth users = zhangsan
secrets file = /etc/user.db
[root@server1 ~]# pkill rsync
[root@server1 ~]# rsync --daemon //重启服务
[root@server1 ~]# netstat -anpt | grep 873 //查看状态
tcp 0 0 20.0.0.10:873 0.0.0.0:* LISTEN 57062/rsync
server2
[root@server2 ~]# rsync -avz --delete /var/www/html/ root@20.0.0.10:/var/www/html
root@20.0.0.10's password:
sending incremental file list
./
index.html
sent 110 bytes received 34 bytes 41.14 bytes/sec
total size is 15 speedup is 0.10
server1
[root@server1 ~]# curl http://localhost
this is aaa!!!
文件同步成功
inotify配置
server2
导入并解包inotify-tools-3.14.tar.gz
[root@server2 ~]# tar zxvf inotify-tools-3.14.tar.gz
[root@server2 ~]# cd inotify-tools-3.14/
[root@server2 inotify-tools-3.14]# ./configure && make && make install
[root@server2 inotify-tools-3.14]# cd
[root@server2 ~]# inotifywait -mrq -e modify,create,delete,move /var/www/html //开启持续监听
在xshell中再次打开一个server2窗口(以下称为server3)
server3
[root@server2 ~]# cd /var/www/html/
[root@server2 html]# ls -lh
总用量 4.0K
-rw-r--r--. 1 root root 15 12月 30 17:27 index.html
[root@server2 html]# touch a.html
[root@server2 html]# touch b.html
此时查看到server2上监听出现新信息
[root@server2 html]# vi a.html
server a
此时再次查看server2
[root@server2 html]# touch a
[root@server2 html]# rm -rf a
再次查看server2
附:判断是否是服务器
server1
[root@server1 ~]# pgrep rsync
57062
server2
配置自动检测脚本
[root@server2 ~]# vi /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,delete,move /var/www/html"
RSYNC_CMD="rsync -avz --delete --password-file=/root/password /var/www/html/ zhangsan@20.0.0.10::wwwroot"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ]
then $RSYNC_CMD
fi
done
server1
修改rsync配置文件
[root@server2 ~]# vi /etc/rsyncd.conf
[wwwroot]
path = /var/www/html
comment = web site page
read only = no //修改为no
auth users = zhangsan
secrets file = /etc/user.db
[root@server1 ~]# ls -l /var/www/
总用量 0
drwxr-xr-x. 2 root root 6 11月 17 00:19 cgi-bin
drwxr-xr-x. 2 root root 24 12月 30 17:28 html
[root@server1 ~]# chmod 777 /var/www/html //赋予html目录写权限
[root@server1 ~]# ls -l /var/www/
总用量 0
drwxr-xr-x. 2 root root 6 11月 17 00:19 cgi-bin
drwxrwxrwx. 2 root root 24 12月 30 17:28 html
server2
[root@server2 ~]# sh /opt/inotify.sh //启动脚本
server3
[root@server2 ~]# cd /var/www/html/
[root@server2 html]# vi e.html
server e
server2上查看
server1
[root@server1 ~]# cd /var/www/html/
[root@server1 html]# ls -lh
总用量 12K
-rw-r--r--. 1 nobody nobody 9 12月 30 17:55 a.html //拷贝成功
-rw-r--r--. 1 nobody nobody 0 12月 30 17:54 b.html
-rw-r--r--. 1 nobody nobody 9 12月 30 18:21 e.html
-rw-r--r--. 1 root root 15 12月 30 17:27 index.html
[root@server1 html]# curl http://localhost/e.html
server e
对于inotify监控调整内核参数
server3
[root@server2 html]# vi /etc/sysctl.conf
//添加以下
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 104876
备注:
- fs.inotify.max_queued_events = 16384 //监控事件的最大队列数
- fs.inotify.max_user_instances = 1024 //监控最大实例数
- fs.inotify.max_user_watches = 104876 //监控每个实例的最大文件数
[root@server2 html]# sysctl -p //写入系统内存
//调完参数需立即启用
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 104876