目录
服务端
客户端
rsync的实时同步数据
多台客户端
简述:
一、Rsync远程同步
1、Rsync的作用和特点
1)rsync的作用
是一个数据备份工具
通过rsync传输文件或者目录
2)rsync的特点
支持本地数据备份
支持异地网络数据备份
支持增量备份
大文件或者小文件都可以使用rsync
2、常见的备份工具和rsync软件包的安装
1)常见的备份工具
cp
tar
scp
sftp
Rsync
2)rsync命令的常见选项
-a归档模式包含-rlptoD
-r递归模式,同步目录使用
-l同步符号连接
-v显示详细信息
-z传输进行压缩
-p保留权限
-t保持源文件时间标记
-g保留组标识
-o保留用户标识
-H保留硬链接文件
-A保留ACL属性
-D保留设备文件特殊文件使用
--delete:同步数据删除目标源文件数据
--checksum:校验同步数据
环境:
两台虚拟机
192.168.1.118 服务端(源端)
192.168.1.117 客户端(发起端)
关闭防火墙规则
[root@bogon ~]# rpm -qa rsync 检查有没有安装rsync
rsync-3.1.2-10.el7.x86_64
配置文件
[root@bogon ~]# vim /etc/rsyncd.conf
uid = nobody 管理账号
gid = nobody 管理组
use chroot = yes 禁锢在源目录
address = 192.168.1.118 本机IP
port 873 监听端口
log file = /var/log/rsyncd.log 日志文件位置
pid file = /var/run/rsyncd.pid pid文件存放位置
hosts allow = 192.168.1.0/24 同步文件IP网段
[wwwroot] 共享模块名字
path=/var/www/html rsync目标服务器数据存放路径,源服务器的数据将同步至此目录
comment = wwwroot 同步目录数据说明
read only = no 设置rsync源服务器为读写权限
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
auth users = haha 执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
secrets file = /etc/rsyncd_users.db 用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件
~
[root@bogon ~]# vim /etc/rsyncd_users.db 用户认证配置文件
haha:123456
[root@bogon ~]# chmod 600 /etc/rsyncd_users.db 设置读写权限
[root@bogon ~]# rsync --daemon 启动
[root@bogon ~]# netstat -lanpt |grep rsync 查看端口
tcp 0 0 192.168.1.118:873 0.0.0.0:* LISTEN 2919/rsync[root@bogon ~]# mkdir -p /var/www/html 创建模块对应目录
[root@bogon ~]# touch /var/www/html/1.txt 创建测试文件
[root@bogon ~]# systemctl stop firewalld 关闭防火墙
[root@bogon ~]# setenforce 0
[root@bogon ~]# export RSYNC_PASSWORD=123456 服务端密码 免密
[root@bogon ~]# rsync -avz --delete [email protected]::wwwroot /root/html 获取同步文件
receiving incremental file list
./
1.txt
111.txtsent 65 bytes received 165 bytes 460.00 bytes/sec
total size is 0 speedup is 0.00
服务端开启文件数据修改实时显示
需要开一个分身
[root@bogon ~]# inotifywait -m -e MOVE,DELETE,CREATE,ATTRIB /var/www/html 开启文件数据修改实时显示
创建文件
[root@bogon html]# touch 1254.txt
root@bogon html]# touch 111111111.txt
[root@bogon html]# ll
分身查看
[root@bogon ~]# inotifywait -m -e MOVE,DELETE,CREATE,ATTRIB /var/www/html
Setting up watches.
Watches established.
/var/www/html/ CREATE 1254.txt
/var/www/html/ ATTRIB 1254.txt
/var/www/html/ CREATE 111111111.txt
/var/www/html/ ATTRIB 111111111.txt-m 持续监听
-e 指定监听事件,多个逗号隔开
MOVE 对文件移动操作
DELETE 删除文件操作
CREATE 创建生成新文件
ATTRIB 属性元数据被修改
只要触发一个条件就会显示出来
客户端获取
[root@bogon wwwroot]# rsync -avz --delete [email protected]::wwwroot /root/wwwroot
receiving incremental file list
deleting 1232.txt
./
111111111.txt
1254.txtsent 65 bytes received 384 bytes 898.00 bytes/sec
total size is 0 speedup is 0.00
1、rsync同步数据的方式
1)rsync计划任务
同步容易延迟
无法实时同步数据
必须通过计划任务实现
灵活性差
2)inotify和rsync结合实时同步数据
需要通过脚本实现
3)inotifywait监控常见的命令选项
-e指定监控事件
-m持续监控
4)inotifywait监控事件的类型
create:创建目录或者文件触发同步
move:移动或者重命名触发同步
delete:删除数据触发同步
modify:修改数据触发同步
attrib:修改文件目录属性触发同
发现源更新数据自动触发更新
服务端
[root@bogon ~]# tar xf inotify-tools-3.14.tar.gz 安装监控软件
[root@bogon ~]# cd inotify-tools-3.14/
[root@bogon inotify-tools-3.14]# ./configure && make && make install[root@bogon inotify-tools-3.14]# ssh-keygen 获取密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:czU4n33UvMqJCvWyAMepkHypQqnRp7ttKotqOC64kNg root@bogon
The key's randomart image is:
+---[RSA 2048]----+
| |
| . ..|
| o o +|
| .o . o . + + ..|
|.o.+.+ +S.. o ...|
|++ o+ + .o. o o. |
|B.E. . o . o + |
|O..o. o + |
|X*++. o |
+----[SHA256]-----+
[root@bogon inotify-tools-3.14]# ssh-copy-id -i [email protected] 转发给客户端/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.1.117 (192.168.1.117)' can't be established.
ECDSA key fingerprint is SHA256:O4oLmwfwSx1D9axNV4oR7zXYA3WTolCwUs5Sq8SF5G0.
ECDSA key fingerprint is MD5:bc:36:f3:ad:a5:5d:1c:c9:8a:3f:f9:9c:3a:46:cc:fe.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.[root@bogon inotify-tools-3.14]# rsync -avz /var/www/html/ [email protected]:/root/html
sending incremental file listsent 86 bytes received 12 bytes 196.00 bytes/sec
total size is 0 speedup is 0.00[root@bogon html]# rsync -avz /var/www/html/ [email protected]:/root/wwwroot/ 同步客户端
sending incremental file listsent 232 bytes received 12 bytes 488.00 bytes/sec
total size is 0 speedup is 0.00
编写实时同步脚本
[root@bogon ~]# vim q.sh
#!/bin/bash
INW="inotifywait -rmq -e MOVE,DELETE,CREATE,ATTRIB /var/www/html"
RSY="rsync -azH --delete /var/www/html/ [email protected]:/root/wwwroot"
$INW | while read DIRECTORY EVENT FILE
do
$RSY
done
这段代码是一个简单的Shell脚本,用于监视/var/www/html目录下的文件变动,并将变动的文件同步到远程服务器上
INW="inotifywait -rmq -e MOVE,DELETE,CREATE,ATTRIB /var/www/html"
:定义一个变量INW,赋值为inotifywait命令,用于监视/var/www/html目录下的文件变动。-r表示递归监视子目录,-m表示持续监视,-q表示静默模式,-e指定监视的事件类型。
RSY="rsync -azH --delete /var/www/html/ [email protected]:/root/wwwroot"
:定义一个变量RSY,赋值为rsync命令,用于将/var/www/html目录下的文件同步到远程服务器。-a表示归档模式,-z表示压缩传输,-H表示保留硬链接,--delete表示删除目标服务器上不存在的文件。
$INW | while read DIRECTORY EVENT FILE
:执行INW变量中的命令,并通过管道传递给while循环,读取监视到的文件变动信息。
do
:循环开始。
$RSY
:执行RSY变量中的命令,将变动的文件同步到远程服务器。
done
:循环结束。简而言之,这段代码的作用是实时监视/var/www/html目录下的文件变动,并将变动的文件同步到远程服务器。
-e 指定监听事件,多个逗号隔开
MOVE 对文件移动操作
DELETE 删除文件操作
CREATE 创建生成新文件
ATTRIB 属性元数据被修改
只要触发一个条件就会显示出来
sh 启动
[root@yuan ~]# sh a.sh
创建文件测试 服务端
[root@bogon html]# touch 98765.txt
客户端查看有一点慢 ok成功
[root@bogon wwwroot]# ll
总用量 0
-rw-r--r--. 1 root root 0 7月 13 19:36 111111111.txt
-rw-r--r--. 1 root root 0 7月 13 19:35 1254.txt
-rw-r--r--. 1 root root 0 7月 13 20:25 98765.txt
服务端操作
[root@bogon ~]# ssh-copy-id -i [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.[root@bogon html]# rsync -avz /var/www/html/ [email protected]:/root/html
The authenticity of host '192.168.1.139 (192.168.1.139)' can't be established.
ECDSA key fingerprint is SHA256:KUnDzA9EBdTNT0UE3pDB18XcdeWBK780vEkpfsZR7PI.
ECDSA key fingerprint is MD5:f6:96:4e:cc:7f:5d:73:60:d6:e3:22:7c:00:25:32:3a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.139' (ECDSA) to the list of known hosts.
[email protected]'s password:
sending incremental file listsent 329 bytes received 12 bytes 52.46 bytes/sec
total size is 0 speedup is 0.00[root@bogon ~]# vim q.sh
#!/bin/bash
INW="inotifywait -rmq -e MOVE,DELETE,CREATE,ATTRIB /var/www/html"
RSY="rsync -azH --delete /var/www/html/ [email protected]:/root/wwwroot"
RSY1="rsync -azH --delete /var/www/html/ [email protected]:/root/wwwroot"
$INW | while read DIRECTORY EVENT FILE
do
$RSY&&$RSE1
done
~
客户端
[root@bogon ~]# systemctl stop firewalld
[root@bogon ~]# setenforce 0
[root@bogon ~]# export RSYNC_PASSWORD=123456
[root@bogon ~]# mkdir html[root@bogon ~]# rsync -avz --delete [email protected]::wwwroot /root/html
receiving incremental file list
./
1.txt
111.txt
111111111.txt
122.txt
1234.txt
1254.txt
2.txt
23.txt
2345.txt
98765.txt
99.txt
qw.txtsent 255 bytes received 768 bytes 186.00 bytes/sec
OK