一、rsync
1、 同步与复制的差异:
复制:完全拷贝源到目标
同步:增量拷贝,只传输变化过的数据
2、rsync可实现远程数据同步
格式:rsync [选项...] 源目录 目标目录
3、常用选项
-n:测试同步过程,不做实际修改
--delete:删除目标文件夹内多余的文档
-a:归档模式,相当于-rlptgoD
-v:显示详细操作信息
-z:传输过程中启用压缩/解压
4、本地同步
(1)rsync [选项...] 本地目录1 本地目录2 //同步整个文件夹
(2)rsync [选项...] 本地目录1/ 本地目录2 //只同步目录下的数据
rsync:第一次完全拷贝
5、rsync特点
(1)可以镜像保存整个⽬录树和⽂件系统
(2)可以保留原有的权限(permission,mode),owner,group,时间(修改时间,modify time),软硬
(3)链接,⽂件acl,⽂件属性(attributes)信息等
(4)传输效率⾼,使⽤同步算法,只⽐较变化的(增量备份)
(5)⽀持匿名传输,⽅便⽹站镜像;也可以做验证,加强安全
6、环境准备
[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/
passwd r.txt shadow
[root@lianxi ~]# rsync -av /nsd20/ /todir/
[root@lianxi ~]# ls /todir/
passwd r.txt shadow
[root@lianxi ~]# rm -rf /todir/*
[root@lianxi ~]# rsync -av /nsd20/ /todir
sending incremental file list
created directory /to
./
passwd
r.txt
shadow
sent 2,036 bytes received 102 bytes 4,276.00 bytes/sec
total size is 1,787 speedup is 0.84
[root@lianxi ~]# cp /etc/group /nsd20/
[root@lianxi ~]# rsync -av /nsd20/ /todir/
sending incremental file list
./
group
passwd
r.txt
shadow
sent 2,593 bytes received 95 bytes 5,376.00 bytes/sec
total size is 2,281 speedup is 0.85
[root@lianxi ~]# ls /todir/
group passwd r.txt shadow
[root@lianxi ~]# echo haha > /nsd20/r.txt
[root@lianxi ~]# rsync -av /nsd20/ /todir/
sending incremental file list
r.txt
sent 185 bytes received 35 bytes 440.00 bytes/sec
total size is 2,248 speedup is 10.22
[root@lianxi ~]# cat /todir/r.txt
haha
[root@lianxi ~]# rsync -av --delete /nsd20/ /todir/
sending incremental file list
sent 133 bytes received 12 bytes 290.00 bytes/sec
total size is 2,248 speedup is 15.50
[root@lianxi ~]# ls /nsd20/
group passwd r.txt shadow
[root@lianxi ~]# ls /todir/
group passwd r.txt shadow
[root@lianxi ~]# touch /todir/1.txt
[root@lianxi ~]# touch /todir/2.txt
[root@lianxi ~]# rsync -av --delete /nsd20/ /todir/
sending incremental file list
deleting 2.txt
deleting 1.txt
./
sent 140 bytes received 37 bytes 354.00 bytes/sec
total size is 2,248 speedup is 12.70
[root@lianxi ~]# ls /todir/
group passwd r.txt shadow
二、rsync+SSH同步
1、远程同步
rsync [选项...] 本地目录/ user@host:远程目录 //将本地文件同步到远程主机
rsync [选项...] user@host:远程目录 本地目录/ //将远程文件同步到本地目录
2、先做免密操作,方便后续同步文件时不需要每次输密码
[root@lianxi ~]# ssh-keygen
[root@lianxi ~]# ssh-copy-id root@192.168.110.20
3、实现同步
[root@lianxi ~]# rsync -av --delete /todir/ root@192.168.110.20:/opt/
sending incremental file list
./
group
passwd
r.txt
shadow
sent 815 bytes received 125 bytes 89.52 bytes/sec
total size is 2,248 speedup is 2.39
[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/
group passwd r.txt shadow
[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
#!/bin/bash
while inotifywait -rq /todir
do
rsync --delete -avz /todir/ root@192.168.110.20:/opt
Done
[root@lianxi ~]# chmod +x /root/rsync.sh
[root@lianxi ~]# /root/rsync.sh
新开一个终端
[root@lianxi ~]# touch /todir/5.txt
[root@lianxi ~]# touch /todir/6.txt
回到刚才的终端查看
打开传送同步监控的机子查看
3、优化脚本,让其没有输出,静默模式
[root@lianxi ~]# vim /root/rsync.sh
#!/bin/bash
while inotifywait -rqq /todir
do
rsync --delete -avz /todir/ root@192.168.110.20:/opt
done
新开一个终端
[root@lianxi ~]# touch /todir/7.txt
[root@lianxi ~]# touch /todir/8.txt
回到刚才的终端查看
放入后台运行
[root@lianxi ~]# /root/rsync.sh &
[1] 28384
[root@lianxi ~]#
打开传送同步监控的机子查看
五、给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 ./
Password:123