文章目录
rsync服务基本介绍
一:Rync服务器
-
Rsync是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具。并且可以不进行改变原有数据的属性信息,实现数据的备份迁移特性。Rsync软件适用于unix/linux/windows等多种操作系统平台。
Rsync是一个快速和非常通用的文件复制工具。它能本地复制,远程复制,或者远程守护进程方式复制。它提供了大量的参数来控制其行为的各个方面,并且允许非常灵活的方式来实现文件的传输复制。它以其delta-transfer算法闻名。 -
rsync监听端口:873
-
rsync运行模式:C/S
二:备份方式
1:全量备份
- 所有数据全部传送,把原来的文件和新的文件一起统一传送
- 全量复制,效率低
假设客户端上有 file1 file2 file3 文件,服务端上有 file1 文件,现要将客户端上的数据备份至服务端
2:增量备份
- 在传输数据之前通过一些算法通过你有的数据和我有的数据进行对比,把不一样的数据通过网络传输
- 增量复制,效率较高
假设客户端上有 file1 file2 file3 文件,服务端上有 file1 文件,现要将客户端上的数据备份至服务端
三:rsync命令使用方法
1:命令参数
- -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 #让目标目录和源目录数据保持一致
1:本地备份命令:cp
- 创建一个文件夹写入数据
[root@server5 ~]# mkdir /aaa
[root@server5 ~]# cd /aaa/
[root@server5 aaa]# touch a b c
[root@server5 aaa]# ll
总用量 0
-rw-r--r--. 1 root root 0 12月 30 15:16 a
-rw-r--r--. 1 root root 0 12月 30 15:16 b
-rw-r--r--. 1 root root 0 12月 30 15:16 c
(1)本地备份数据
- 第一种
/aaa 后面没有带 / 则是备份整个目录
[root@server5 ~]# rsync -avz /aaa /opt/
sending incremental file list
aaa/
aaa/a
aaa/b
aaa/c
[root@server5 ~]# cd /opt/
[root@server5 opt]# ll
总用量 0
drwxr-xr-x. 2 root root 33 12月 30 15:16 aaa
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
- 第二种
/aaa/ 后面带 / 则是备份里面的文件
[root@server5 ~]# rsync -avz /aaa/ /opt/
sending incremental file list
./
a
b
c
[root@server5 ~]# cd /opt/
[root@server5 opt]# ll
总用量 0
-rw-r--r--. 1 root root 0 12月 30 15:16 a
drwxr-xr-x. 2 root root 33 12月 30 15:16 aaa
-rw-r--r--. 1 root root 0 12月 30 15:16 b
-rw-r--r--. 1 root root 0 12月 30 15:16 c
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
2:远程备份数据
(1)全量备份
- 本机地址:20.0.0.14
- 远程地址:20.0.0.15
[root@server5 ~]# rsync -avz /aaa root@20.0.0.15:/bbb
- 20.0.0.15 查看
[root@server6 ~]# cd /bbb/
[root@server6 bbb]# ll
总用量 0
drwxr-xr-x. 2 root root 33 12月 30 15:16 aaa
[root@server6 bbb]# cd aaa/
[root@server6 aaa]# ll
总用量 0
-rw-r--r--. 1 root root 0 12月 30 15:16 a
-rw-r--r--. 1 root root 0 12月 30 15:16 b
-rw-r--r--. 1 root root 0 12月 30 15:16 c
(2)增量备份
- 在 20.0.0.15 /opt touch 一个文件
[root@server6 aaa]# cd /opt/
[root@server6 opt]# touch a
[root@server6 opt]# ll
总用量 0
-rw-r--r--. 1 root root 0 12月 30 17:07 a
drwxr-xr-x. 2 root root 33 12月 30 15:16 aaa
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
- 20.0.0.14 远程备份
[root@server5 ~]# rsync -avz /aaa/ root@20.0.0.15:/opt
root@20.0.0.15's password:
sending incremental file list
./
a
b
c
- 20.0.0.15查看
只备份 a 和 b
[root@server6 ~]# cd /opt/
[root@server6 opt]# ll
总用量 0
-rw-r--r--. 1 root root 0 12月 30 15:16 a
drwxr-xr-x. 2 root root 33 12月 30 15:16 aaa
-rw-r--r--. 1 root root 0 12月 30 15:16 b
-rw-r--r--. 1 root root 0 12月 30 15:16 c
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
3:代替删除命令:rm
- 创建目录文件
root@server5 ~]# mkdir /www
[root@server5 ~]# cd /www/
[root@server5 www]# echo "hello" > index.html
[root@server5 www]# ll
总用量 4
-rw-r--r--. 1 root root 6 12月 30 17:19 index.html
- 20.0.0.15查看复制路径
[root@server6 opt]# cd /bbb/
[root@server6 bbb]# ll
总用量 0
drwxr-xr-x. 2 root root 33 12月 30 15:16 aaa
- 20.0.0.14 代替删除
[root@server5 ~]# rsync -avz --delete /www/ root@20.0.0.15:/bbb
- 20.0.0.15 查看
aaa 目录删除
[root@server6 opt]# cd /bbb/
[root@server6 bbb]# ll
总用量 4
-rw-r--r--. 1 root root 6 12月 30 17:19 index.html
2:远程备份数据
1:rsync 拉操作
- Pull: rsync [OPTION…] [USER@]HOST:SRC…[DEST]
- [USER@] 以什么用户身份拉取数据(默认以当前用户)
注意:需要对端也要有相同的用户(对端用户密码)
- hosts 指定远程主机IP地址或者主机名称
- SRC 要拉取的数据信息
- dest 保存到本地的路径信息
操作
- 20.0.0.15创建一个文件,20.0.0.14 下载
[root@server6 ~]# cd /opt/
[root@server6 opt]# vi aaa
1234456
[root@server6 ~]# cd /opt/
[root@server6 opt]# ll
总用量 4
-rw-r--r--. 1 root root 8 12月 30 17:32 aaa
- 将 aaa 文件拷贝到 20.0.0.14
在 20.0.0.14 上操作
[root@server5 ~]# rsync -avz root@20.0.0.15:/opt/ /opt
[root@server5 ~]# cd /opt/
[root@server5 opt]# ll
总用量 4
-rw-r--r--. 1 root root 8 12月 30 22:00 aaa
配置 rsync 服务器
rsync同步源
指备份操作的远程服务器,也称为备份源
1:rsync CentOS7.4 版本默认安装
2:进入配置文件修改
[root@rsync ~]# vi /etc/rsyncd.conf
uid = nobody # 7 运行用户是匿名用户,可以改也可以不改
gid = nobody # 8 运行组是匿名用户,可以改也可以不改
use chroot = no # 9 不锁定宿主目录
max connections = 200 # 10 最大连接数
pid file = /var/run/rsyncd.pid # 11 进程文件存储路径
log file = /var/log/rsyncd.log # 接 11 行添加第 12 行,日志文件路径
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # 17 哪些数据类型不做压缩
hosts allow = 20.0.0.0/24 # 18 添加,设置 rsync 下共享目录
address = 20.0.0.14 # 19 添加,监听地址为本机
port 873 # 20 添加,本机端口号
[wwwroot] # 21 修改共享目录
path = /www # 22 目录路径
comment = web site page # 23 网页描述
read only = yes # 24 权限为只读
auth users = lisi # 25 访问的授权账号与密码
secrets file = /etc/user.db # 26 存账户名密码的数据库文件
账户名与密码
[root@rsync ~]# vi /etc/user.db
lisi:123456
给本地读的权限
[root@rsync ~]# chmod 600 /etc/user.db
重启服务
[root@rsync ~]# rsync --daemon # 以守护进程开启
[root@rsync ~]# netstat -anpt | grep 873
tcp 0 0 20.0.0.14:873 0.0.0.0:* LISTEN 57955/rsync
1:复制操作
格式一
1:客户机复制服务器上的文件
- (1)服务器上创建文件
[root@rsync ~]# mkdir /www
[root@rsync ~]# cd /www
[root@rsync www]# touch 1 2 3
[root@rsync www]# ll
总用量 0
-rw-r--r--. 1 root root 0 12月 30 22:55 1
-rw-r--r--. 1 root root 0 12月 30 22:55 2
-rw-r--r--. 1 root root 0 12月 30 22:55 3
- (2)客户机创建存放目录
[root@client ~]# mkdir /bak
- (3)将服务器上的文件复制过来
[root@client ~]# rsync -avz lisi@20.0.0.14::wwwroot /bak
Password:
receiving incremental file list
./
1
2
3
- (4)查看文件
[root@client ~]# cd /bak
[root@client bak]# ll
总用量 0
-rw-r--r--. 1 root root 0 12月 30 22:55 1
-rw-r--r--. 1 root root 0 12月 30 22:55 2
-rw-r--r--. 1 root root 0 12月 30 22:55 3
格式二
- (1)客户机创建存放目录
[root@client ~]# mkdir /bak2
- (2)依旧复制服务器上的文件
[root@client ~]# rsync -avz rsync://lisi@20.0.0.14/wwwroot /bak2
Password:
receiving incremental file list
./
1
2
3
- (3)查看文件
[root@client ~]# cd /bak2
[root@client bak2]# ll
总用量 0
-rw-r--r--. 1 root root 0 12月 30 22:55 1
-rw-r--r--. 1 root root 0 12月 30 22:55 2
-rw-r--r--. 1 root root 0 12月 30 22:55 3
免交互
- (1)客户机创建 lisi 的密码文件
[root@client ~]# vi paassword
123456
- (2)对密码文件授权
[root@client ~]# chmod 600 password
- (3)客户机创建存放目录
[root@client ~]# mkdir /bak3
- (4)免密复制
[root@client ~]# rsync -avz --password-file=/root/password rsync://lisi@20.0.0.14/wwwroot /bak3
receiving incremental file list
./
1
2
3
- (5)查看文件
[root@client ~]# cd /bak3
[root@client bak3]# ll
总用量 0
-rw-r--r--. 1 root root 0 12月 30 22:55 1
-rw-r--r--. 1 root root 0 12月 30 22:55 2
-rw-r--r--. 1 root root 0 12月 30 22:55 3
rsync + inotify
1:关于inotify
(1)Linux内核的 inotify 机制
- 从版本2.6.13开始提供
- 可以监控文件系统的变动情况,并做出通知响应
- 辅助软件:inotifv-tools
1:客户据和服务器创建文件
[root@rsync ~]# mkdir -p /var/www/html
[root@client ~]# mkdir -p /var/www/html
2:客户据和服务器安装 apache
[root@rsync ~]# yum -y install httpd
[root@client ~]# yum -y install httpd
3:客户据和服务器开启 apache 服务
[root@rsync ~]# systemctl start httpd
[root@client ~]# systemctl start httpd
4:客户机对创建的文件写入网页数据
[root@client ~]# cd /var/www/html/
[root@client html]# vi index.html
this is my web.
5:修改服务器 rsync 配置文件中的描述文件
[root@rsync ~]# vi /etc/rsyncd.conf
path = /var/www/html # 22
6:重启服务
[root@rsync ~]# pkill rsync
[root@rsync ~]# rsync --daemon
[root@rsync ~]# netstat -anpt | grep 873
tcp 0 0 20.0.0.14:873 0.0.0.0:* LISTEN 58776/rsync
7:客户机将网页文件复制给服务器
[root@client ~]# rsync -avz --delete /var/www/html/ root@20.0.0.14:/var/www/html/
8:服务器读取网页数据
[root@rsync ~]# curl http://localhost
this is my web.
实现数据同步
2:rsync实时同步
定期同步的不足
- 执行备份的时间固定,延迟明显、实时性差
- 当同步源长期不变化时,密集的定期任务是不必要的
实时同步的优点
- 一旦同步源出现变化,立即启动备份
- 只要同步源无变化,则不执行备份
2:客户机安装 inotify 软件
安装inotify-tools辅助工具
- inotifywait:用于持续监控,实时输出结果
- inotifywatch:用于短期监控任务完成后再出结果
1:解压缩,编译安装
[root@client ~]# tar zxvf 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
2:开启监控
持续监控
[root@client ~]# inotifywait -mrq -e modify,create,delete,move /var/www/html/
- -m:持续监听
- -r:使用递归形式监视目录
- -q:减少冗余信息,只打印出需要的信息
- -e:指定要监视的事件,多个时间使用逗号隔开
3:再开启一个客户机窗口添加文件
另外再开启一个新的终端设置,因为客户机端开启监控后不能做输入操作
[root@client ~]# cd /var/www/html/
[root@client html]# touch a.html
[root@client html]# touch b.html
4:到监听的客户机上查看
/var/www/html/ CREATE a.html
/var/www/html/ CREATE b.html
5:停掉监听编写脚本
[root@client ~]# 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/ lisi@20.0.0.14::wwwroot"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ]
then $RSYNC_CMD
fi
done
6:修改服务器 rsync 配置
[root@rsync ~]# vi /etc/rsyncd.conf
read only = no # 24
7:对服务器 /var/www/html 授权
[root@rsync ~]# chmod 777 /var/www/html/
[root@rsync ~]# ls -l /var/www/
总用量 0
drwxr-xr-x. 2 root root 6 8月 4 2017 cgi-bin
drwxrwxrwx. 2 root root 24 12月 30 23:31 html
8:客户机开启脚本
[root@client ~]# sh /opt/inotify.sh
9:客户机重开一个窗口写入数据
[root@client html]# vi ccc.html
server ccc.
由于脚本的原因,写入的数据已经自动复制到服务器上
10:服务器查看
[root@rsync html]# cd /var/www/html/
[root@rsync html]# ll
-rw-r--r--. 1 nobody nobody 12 12月 31 00:23 ccc.html
[root@rsync html]# curl http://localhost/ccc.html
server ccc.
11:客户机修改内核参数
- rsync+inotify实时同步
[root@client ~]# vi /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@client ~]# sysctl -p # 写入系统内存
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
- fs.inotify. max queued_events = 16384//监控事件的最大队列数
- fs.inotify.max_user_instances = 1024//监控的最大实例数
- fs.inotify.max_user_watches = 1048576//监控的每实例的最大文件数