7.18 rsync

一、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@localhost  ~]# rm -rf /nsd20/ /todir/
[root@localhost ~]# mkdir /nsd20
[root@localhost ~]# mkdir /todir
[root@localhost ~]# cp /etc/passwd /nsd20/
[root@localhost ~]# cp /etc/shadow /nsd20/
[root@localhost ~]# cp /etc/redhat-release /nsd20/r.txt
[root@localhost ~]# ls /nsd20/
passwd  r.txt  shadow
[root@localhost ~]# rsync  -av /nsd20/ /todir/

[root@localhost ~]# ls /todir/
 passwd  r.txt  shadow
[root@localhost ~]# rm -rf /todir/*
[root@localhost ~]# 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@localhost ~]#[root@localhost ~]# cp /etc/group /nsd20/
 [root@localhost ~]# 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@localhost ~]# ls /todir/
group  passwd  r.txt  shadow

[root@localhost ~]# echo haha > /nsd20/r.txt
[root@localhost ~]# 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@localhost ~]# cat /todir/r.txt
haha

[root@localhost ~]# 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@localhost ~]# ls /nsd20/
group  passwd  r.txt  shadow
[root@localhost ~]# ls /todir/
group  passwd  r.txt  shadow

[root@localhost ~]# touch /todir/1.txt
[root@localhost ~]# touch /todir/2.txt
[root@localhost ~]# 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@localhost ~]# 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@localhost ~]# 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@localhost ~]# ssh 192.168.110.20
 Last login: Thu Jul 18 15:36:08 2024 from 192.168.110.22
 [root@localhost ~]#  ls /opt/
 group  passwd  r.txt  shadow
 [root@localhost ~]# exit
 登出
 Connection to 192.168.110.20 closed.

 三、inotify实时同步
1、同步实时性
(1)安装相关包

[root@localhost ~]# yum -y install make gcc

(2)解压包

[root@localhost ~]# tar -xf tools.tar.gz  -C /opt/
[root@localhost ~]# cd /opt/tools/
[root@localhost tools]# tar -xf inotify-tools-3.13.tar.gz

(3)./configure 配置,指定安装目录/功能模块等选项

[root@localhost tools]# cd inotify-tools-3.13/
 [root@localhost inotify-tools-3.13]# ./configure

(4)make 编译,生成可执行的二进制程序文件

[root@localhost inotify-tools-3.13]# make

(5)make install 安装,将编译好的文件复制到安装目录

[root@localhost inotify-tools-3.13]# make install 
[root@localhost inotify-tools-3.13]# ls /usr/local/bin/inotifywait
/usr/local/bin/inotifywait 

 2、inotifywait监控
(1)格式
 inotifywait [选项] 目标文件夹

(2)常用选项:

-m:持续监控 (捕获一个事件后不退出)

-r:递归监控、包括子目录及文件

-q:减少屏幕输出信息

-e:指定监视的 modify、move、create、delete、attrib 等事件类别,不写-e全部监控

(3)监控

[root@localhost inotify-tools-3.13]# inotifywait -rq /todir/

新开一个终端测试

[root@localhost ~]# touch /todir/3.txt
返回之前的终端查看

加-m选项继续测试

[root@localhost inotify-tools-3.13]# inotifywait  -rqm /todir/

新开一个终端测试

[root@localhost ~]# touch /todir/4.txt

返回之前的终端查看

[root@localhost 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@localhost ~]# rm -rf /todir/3.txt /todir/4.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 ./
 Password:123

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值