rsync+inotify

rsync+inotify

rsync:
什么是rsync呢?
rsync (remote sync) 远程同步 可以将数据同步到LAN/VAN中能够通讯的其他主机 可以用来代替rcp的命令 能够有守护进程 后台运行

scp 远程同步 scp只能去全量复制 rsync可以增量复制
sync :同步复制 数据库 主从 主写入 从写入 回应给客户端
async :异步复制 数据库 主从 主写入 不论从有没有复制成功 直接回应给客户端

线程:
同步:只能干一件是请求
异步:可以做多件事情

作用:
1.代替了 scp rcp
2.对网站进行备份 (镜像目录树 文件系统)
3.对数据进行备份

特点:
1.支持全量复制也支持增量
2.支持匿名复制 也支持身份验证
3.镜像目录树
4.在传输过程中,可以把文件进行压缩 提高传输效率
5.保持原数据的属性 时间 软链接

在架构当中的应用:
备份数据(数据库/web)

应用的案例
1.针对公司的数据比较混乱的状况,把这些数据进行备份
2.可以定期对IDC机房进行数据备份,防止重大事故导致数据丢失

环境:

A192.168.10.3
B192.168.10.10

B主机同步A主机上的数据
rsync命令
格式 rsync 【选项】 src root@ip:/dest A主机 推
rsync 【选项】 root@ip:/src /dest B主机 拉
选项:
-r 递归复制
-l 同步链接文件
-o 同步时 不修改文件的属主
-g 同步时 不修改文件属组
-p 同步时 不修改文件的权限
-a 代表以上所有的选项

-v 显示详情
-z 同步时,在传输过程中对文件进行压缩
–delete 同步时,如果目标目录和源目录有不一致的文件 自动删除
-L 同步时,如果有链接文件 则同步链接文件的源文件

操作:

A主机:
A主机对B主机免密登录

[root@localhost ~]# ssh-keygen 
[root@localhost ~]# ssh-copy-id root@192.168.10.10
[root@localhost ~]# ssh root@192.168.10.10
[root@localhost ~]# exit
[root@localhost ~]# mkdir /test1

B主机:

[root@localhost ~]# mkdir /test2

A和B主机上都要有rsync

[root@localhost ~]# rpm -qa | grep rsync
rsync-3.0.9-17.el7.x86_64

A主机:

[root@localhost ~]# cd /test1/
[root@localhost test1]# touch aa
[root@localhost test1]# rsync /test1/* root@192.168.10.10:/test2

B主机:

[root@localhost ~]# ls /test2/
aa

选项:
-r 递归

[root@localhost test1]# mkdir -p a/b/c
[root@localhost test1]# rsync /test1/* root@192.168.10.10:/test2
skipping directory a
[root@localhost test1]# rsync -r /test1/* root@192.168.10.10:/test2/

-l 同步链接

[root@localhost test1]# ln -s aa ee
[root@localhost test1]# rsync -lv /test1/* root@192.168.10.10:/test2
skipping directory a
aa
ee -> aa

sent 83 bytes  received 34 bytes  78.00 bytes/sec
total size is 2  speedup is 0.02

-o 不修改属主

[root@localhost test1]# useradd test
[root@localhost test2]# useradd test   #在B主机创建
[root@localhost test1]# chown test aa
[root@localhost test1]# ls -l aa
-rw-r--r--. 1 test root 0 6月  24 09:03 aa
[root@localhost test1]# rsync -ov /test1/* root@192.168.10.10:/test2/
skipping directory a
aa
skipping non-regular file "ee"

sent 88 bytes  received 31 bytes  79.33 bytes/sec
total size is 2  speedup is 0.02

-g 不修改属组

[root@localhost test1]# chgrp test aa
[root@localhost test1]# ls -l aa
-rw-r--r--. 1 test test 0 6月  24 09:03 aa
[root@localhost test1]# rsync -gv /test1/* root@192.168.10.10:/test2
skipping directory a
aa
skipping non-regular file "ee"

sent 88 bytes  received 31 bytes  238.00 bytes/sec
total size is 2  speedup is 0.02

-p 不修改文件权限

[root@localhost test1]# chmod 777 aa
[root@localhost test1]# ll aa
-rwxrwxrwx. 1 test test 0 6月  24 09:03 aa
[root@localhost test1]# rsync -pv /test1/* root@192.168.10.10:/test2/
skipping directory a
aa
skipping non-regular file "ee"

sent 77 bytes  received 31 bytes  216.00 bytes/sec
total size is 2  speedup is 0.02

-a 代表以上所有

[root@localhost test1]# rsync -av /test1/* root@192.168.10.10:/test2/
sending incremental file list
aa
a/
a/b/
a/b/c/

sent 167 bytes  received 46 bytes  142.00 bytes/sec
total size is 2  speedup is 0.01

-z 提高传输效率的

[root@localhost test1]# rsync -azv /test1/* root@192.168.10.10:/test2/

-L 复制链接文件的源文件
A:

[root@localhost test1]# rsync -aLv /test1/* root@192.168.10.10:/test2/
[root@localhost test1]# ls -ld ee   
lrwxrwxrwx. 1 root root 1 7月   9 21:09 e -> a

B:

[root@localhost test2]# ls -ld ee    #只是变成源文件   但是名字不会改变
drwxr-xr-x. 3 root root 15 7月   9 2019 e

–delete 删除目标目录中多余的文件
B:

[root@localhost test2]# touch tt

A:

[root@localhost test1]# rsync -av --delete /test1/ root@192.168.10.10:/test2/
sending incremental file list
deleting tt
[root@localhost test1]# rsync -av  /test1 root@192.168.10.10:/test2/
目录后的/必须添加   否则会认为是整个目录的复制
delete  /后面不加*

B主机上拉数据:

192  ssh-keygen
  193  ssh-copy-id root@192.168.10.3
  194  ssh root@192.168.10.3
exit

A:

[root@localhost test1]# touch bb
[root@localhost test1]# chmod 777 bb
[root@localhost test1]# chown test:test bb
[root@localhost test1]# ln -s bb rr
[root@localhost test1]# mkdir -p t/y/u

B:

[root@localhost test2]# rsync -avz root@192.168.10.3:/test1/* /test2/
[root@localhost test2]# rsync --delete -avL root@192.168.10.3:/test1/ /test2/

crontab+rsync 定时备份
在备份的期间服务器坏掉 数据会丢失

inotify+rsync 自动同步数据
inotify 监控目录 文件系统 删除 创建 修改(内容、属性)等操作
操作 叫做事件
如果有事件产生 可以数据内容 可以自定义

A主机:

[root@localhost ~]# tar -zxf inotify-tools-3.14.tar.gz 
[root@localhost ~]# cd inotify-tools-3.14/
[root@localhost inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify && make && make install
[root@localhost inotify-tools-3.14]# ln -s /usr/local/inotify/bin/* /usr/local/bin/

inotify的使用:

[root@localhost inotify-tools-3.14]# inotifywait -mrq --format %w%f -e create,delete,close_write /test1
#参数释义:
-m   一直处于监控当中
-r   递归监控
-q  将监控的目录和监控的信息显示在终端上
--format   显示的内容  格式
%w    产生监控的路径
&f   监控为目录   输出文件名
%e  事件名称
%T  输出当前事件的时间
--timefmt  时间格式
%y:年    %m:月   %d:日     %H:小时     %M:分钟
-e  指定监控的事件
事件
open    打开文件
move   移动
create  创建
delete  删除   
modify  修改内容
close_write  修改文件内容
close_mowrite  查看只读文件

验证:另开终端

[root@localhost ~]# cd /test1/
[root@localhost test1]# touch 5

回到原来的终端查看

[root@localhost inotify-tools-3.14]# inotifywait -mrq --format %w%f -e create,delete,close_write /test1
/test1/5
/test1/5

带时间的:

[root@localhost inotify-tools-3.14]# inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format %T%w%f -e create,delete,close_write /test1
2020-06-24 10:21/test1/6
2020-06-24 10:21/test1/6

rsync+inotify 自动同步数据

[root@localhost inotify-tools-3.14]# vim /etc/rsyncd.conf 
7 port = 873     #端口
  8 address = 192.168.10.3  #监听的ip
  9 uid = root  #用户
 10 gid = root
 11 use chroot = no  #不使用root切换模式
 12 max connections = 0  #没有链接数
 13 pid file = /var/run/rsyncd.pid  #pid文件
 14 exclude = lost+found/  #同步数据时,排除lost+found这个目录
 15 transfer logging = yes  #打开日志
 16 log file = /var/lib/rsync.log  #日志文件
 17 timeout = 900  #超时时间
 18 ignore nonreadable = yes   #忽略没有权限的用户
 19 dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2  #如果遇到这类文件  就不用压缩
 20 
 21 [test]   #同步模块的名称
 22         path = /test1  #实际的目录
 23         comment = test1  #详细说明
 24         read only = no   #不只是只读
[root@localhost inotify-tools-3.14]# systemctl restart rsyncd
[root@localhost inotify-tools-3.14]# netstat -anput | grep 873
防火墙关掉
[root@localhost ~]# vim rsync.sh
#!/bin/bash
/usr/local/bin/inotifywait -mrq --format %w%f -e create,delete,close_write /test1 | while read file   #使用inotify对/test1进行监控 看是否有事件触动 生成路径并且不断的读取监控生成的路径
do #如果有路径生成 让其做以下的操作
if [ -f $file ];then #如果这个路径是文件
        rsync -a --delete $file root@192.168.10.10:/test2  #那么调用这个路径直接进行同步test2
else
        rsync -a --delete /test1/ root@192.168.10.10:/test2 #其余的情况test1下面的内容同步到test2
fi  #结束if循环
done  #结束
[root@localhost ~]# chmod +x rsync.sh 
[root@localhost ~]# ./rsync.sh &   #& 代表在后台运行
[1] 63740
[root@localhost ~]# kill -9 63740   #关掉程序
[1]+  已杀死               ./rsync.sh

验证:
另开一个终端

[root@localhost ~]# cd /test1/
[root@localhost test1]# touch haha

B主机查看:

[root@localhost ~]# cd /test2/
[root@localhost test2]# ls
a  aa  ee  haha

A:

[root@localhost test1]# rm -rf haha
[root@localhost test1]# touch haha
[root@localhost test1]# echo "111111" > haha

B:

[root@localhost test2]# cat haha
111111
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值