⼀、RSYNC概述
1、什么是rsync
rsync的好姐妹
sync 同步:刷新⽂件系统缓存,强制将修改过的数据块写⼊磁盘,并且更新超级块。
async 异步:将数据先放到缓冲区,再周期性(⼀般是30s)的去同步到磁盘。
rsync 远程同步:==remote synchronous==
数据同步过程
sync数据同步 => 保存⽂件(⽬标)=> 强制把缓存中的数据写⼊磁盘(⽴即保存),实时性
要求⽐较⾼的场景
asyn数据异步 => 保存⽂件(⽬标)=> 将数据先放到缓冲区,再周期性(⼀般是30s)的去同
步到磁盘,适合⼤批量数据同步的场景
2、rsync特点
可以镜像保存整个⽬录树和⽂件系统
可以保留原有的权限(permission,mode),owner,group,时间(修改时间,modify time),软硬
链接,⽂件acl,⽂件属性(attributes)信息等
传输==效率⾼==,使⽤同步算法,只⽐较变化的(增量备份)file1.txt file2.txt file3.txt(A服务器)
rsync实现数据同步 => 只同步file3.txt => 增量备份
file1.txt file2.txt(B服务器)
⽀持匿名传输,⽅便⽹站镜像;也可以做验证,加强安全
3、rsync与scp的区别
两者都可以实现远程同步,但是相对⽐⽽⾔,rsync能⼒更强
① ⽀持增量备份
② 数据同步时保持⽂件的原有属性
yum -y install rsync
二.环境准备
[root@lianxi ~]# rm -rf /nsd20/ /todir/
[root@lianxi ~]# mkdir /nsd20
[root@lianxi ~]# mkdir /todir
[root@lianxi ~]# cp /etc/passwd /nsd20/
[root@lianxi ~]# cp /etc/shadow /nsd20/
[root@lianxi ~]# cp /etc/redhat-release /nsd20/r.txt
[root@lianxi ~]# ls /nsd20/
[root@lianxi ~]# rsync -av /nsd20/ /todir/
[root@lianxi ~]# ls /todir/
[root@lianxi ~]# rm -rf /todir/*
[root@lianxi ~]# rsync -av /nsd20/ /todir
[root@lianxi ~]# echo haha > /nsd20/r.txt
[root@lianxi ~]# rsync -av /nsd20/ /todir/
[root@lianxi ~]# cat /todir/r.txt
[root@lianxi ~]# rsync -av --delete /nsd20/ /todir/
[root@lianxi ~]# ls /nsd20/
[root@lianxi ~]# ls /todir/
[root@lianxi ~]# touch /todir/1.txt
[root@lianxi ~]# touch /todir/2.txt
[root@lianxi ~]# rsync -av --delete /nsd20/ /todir/
三.rsync+SSH同步
1、远程同步
rsync [选项...] 本地目录/ user@host:远程目录 //将本地文件同步到远程主机
rsync [选项...] user@host:远程目录 本地目录/ //将远程文件同步到本地目录
2、先做免密操作,方便后续同步文件时不需要每次输密码
[root@lianxi ~]# ssh-keygen
[root@lianxi ~]# ssh-copy-id root@192.168.110.20
[root@lianxi ~]# rsync -av --delete /todir/ root@192.168.110.20:/opt/
[root@lianxi ~]# ssh 192.168.110.20
Last login: Thu Jul 18 15:36:08 2024 from 192.168.110.22
[root@shixun2 ~]# ls /opt/
[root@shixun2 ~]# exit
登出
Connection to 192.168.110.20 closed.
四、inotify实时同步
1、同步实时性
(1)安装相关包
[root@lianxi ~]# yum -y install make gcc
(2)解压包
[root@lianxi ~]# tar -xf tools.tar.gz -C /opt/
[root@lianxi ~]# cd /opt/tools/
[root@lianxi tools]# tar -xf inotify-tools-3.13.tar.gz
(3)./configure 配置,指定安装目录/功能模块等选项
[root@lianxi tools]# cd inotify-tools-3.13/
[root@lianxi inotify-tools-3.13]# ./configure
(4)make 编译,生成可执行的二进制程序文件
[root@lianxi inotify-tools-3.13]# make
(5)make install 安装,将编译好的文件复制到安装目录
[root@lianxi inotify-tools-3.13]# make install
[root@lianxi inotify-tools-3.13]# ls /usr/local/bin/inotifywait
/usr/local/bin/inotifywait
[root@lianxi inotify-tools-3.13]#
2、inotifywait监控
(1)格式
inotifywait [选项] 目标文件夹
(2)常用选项:
-m:持续监控 (捕获一个事件后不退出)
-r:递归监控、包括子目录及文件
-q:减少屏幕输出信息
-e:指定监视的 modify、move、create、delete、attrib 等事件类别,不写-e全部监控
(3)监控
[root@lianxi inotify-tools-3.13]# inotifywait -rq /todir/
新开一个终端测试
[root@lianxi ~]# touch /todir/3.txt
返回之前的终端查看
加-m选项继续测试
[root@lianxi inotify-tools-3.13]# inotifywait -rqm /todir/
新开一个终端测试
[root@lianxi ~]# touch /todir/4.txt
返回之前的终端查看
[root@lianxi inotify-tools-3.13]# inotifywait -rqm /todir/
/todir/ CREATE 4.txt
/todir/ OPEN 4.txt //打开
/todir/ ATTRIB 4.txt //修改或显示文件的属性
/todir/ CLOSE_WRITE,CLOSE 4.txt //关闭写入 关闭
删除3.txt与4.txt
[root@lianxi ~]# rm -rf /todir/3.txt /todir/4.txt
查看
四、inotify与rsync的结合
1、解决重复操作,循环
for循环:适合写有次数的循环
while循环:适合写不定次数的循环(死循环)
while 条件或命令
do
循环执行的代码
Done
2、编写脚本,实现监控同步
[root@lianxi ~]# vim /root/rsync.sh
[root@lianxi ~]# chmod +x /root/rsync.sh
[root@lianxi ~]# /root/rsync.sh
新开一个终端
[root@lianxi ~]# touch /todir/5.txt
[root@lianxi ~]# touch /todir/6.txt
回到刚才的终端查看
六、给RSYNC服务添加密码
1、Code服务器:
(1)打开/etc/rsyncd.conf配置⽂件
vim /etc/rsyncd.conf
[app]
path=/app/java_project
log file=/var/log/rsync.log
auth users = user1 //user2 => ⽤户名
secrets file = /etc/rsyncd //secrets => 密码⽂件
(2)在/etc⽬录下创建rsyncd.secrets⽂件
vim /etc/rsyncd.secrets
user1:123 //设置密码,⽤户名:密码
user2:123
(3)更改密码⽂件权限为600
chmod 600 /etc/rsyncd.secrets
(4)重启rsyncd服务
systemctl restart rsyncd
2、Backup备份服务器:
rsync -av user1@10.1.1.10::app ./