rsync+inotify自动进行同步

rsync+inotify自动进行同步

数据同步 rsync+inotify

Sync:同步
async:异步
Rsync:远程同步,可以将数据同步到多个和它能够通信的主机上。

Rsync特点:

1 增量复制:
第一次去同步全部的内容,第二次同步只同步修改过的内容。
2 支持匿名复制,也支持身份验证。
3 可以镜像目录树,文件系统。

Rsync

Rsync 选项 src root@ip:/dest push
Rsync 选项 root@ip:/src /dest pull

选项:

-a 代表以下所有选项(不包含v)
-r 递归同步
-l 同步链接文件
-o 同步时,不修改文件的属主
-g 同步时,不修改文件的属组
-p 同步时,不修改文件的权限
-z 同步时,对文件进行压缩
-v 同步时,显示同步信息
–delete 同步时,如果目标目录和源目录中有不一致的文件,自动删除
-L 同步时,如果有链接文件,则同步链接文件的源文件。

基础环境:

IP 备注 别名
192.168.1.10 服务端 master
192.168.1.20 客户端 slave

服务端

做免密登录

[root@master ~]# echo '192.168.1.10 master
> 192.168.1.20 slave' >> /etc/hosts [root@master ~]# 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:tWGSuqFJ1PeUXtWTvI/ItEIcDkOLmGNqItF07CxgplE root@master The key's randomart image is:
+---[RSA 2048]----+
| .E..  ..     o..|
|o= ..+ .oo.. . +.|
|=o.o* o ==*..   o|
|...+o. o B++.  . |
|o o.. o S.+o o ..|
|.o . o o  . + . .|
|    o .    .     |
|                 |
|                 |
+----[SHA256]-----+
[root@master ~]# ssh-copy-id -i slave
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'slave (192.168.1.20)' can't be established. ECDSA key fingerprint is SHA256:dnnDcAA2qVnA31i7mtr9LYJmH2veu2+r4t+19qUSqqw. ECDSA key fingerprint is MD5:0e:f3:c1:3c:dc:5f:12:66:ae:c9:01:51:66:db:bb:02. 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 root@slave's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'slave'"
and check to make sure that only the key(s) you wanted were added.
[root@master ~]# scp /etc/hosts slave:/etc/
hosts                                                   100%  197    87.6KB/s   00:00    

验证

[root@master ~]# ssh slave
Last login: Tue Dec 22 09:14:29 2020 from 192.168.1.250
[root@slave ~]# exit
登出
Connection to slave closed.

创建同步目录

[root@master ~]# mkdir /test1
[root@master ~]# cd /test1
[root@master test1]# 

客户端

创建同步目录

[root@slave ~]# mkdir /test2
[root@slave ~]# cd /test2
[root@slave test2]# 

查看是否安装

 [root@master test1]# rpm -qa | grep rsync
 [root@master test1]# yum -y install rsync
 [root@master test1]# rpm -qa | grep rsync
rsync-3.1.2-10.el7.x86_64


实验一:同步目录

服务端

创建目录

[root@master test1]# mkdir -p a/b/c
[root@master test1]# ls
a

同步

[root@master test1]# rsync -rv /test1/* root@slave:/test2
sending incremental file list
a/
a/b/
a/b/c/

sent 95 bytes  received 28 bytes  82.00 bytes/sec
total size is 0  speedup is 0.00

客户端

查看

[root@slave test2]# ls -R a
a:
b

a/b:
c

a/b/c:

实验二:同步文件

服务端

[root@master test1]# touch 1
[root@master test1]# chmod 777 1
[root@master test1]# ls
1  a
[root@master test1]# ls -l 1
-rwxrwxrwx. 1 root root 0 12月 22 09:25 1
[root@master test1]# rsync -pv /test1/* root@slave:/test2
skipping directory a
1

sent 75 bytes  received 35 bytes  73.33 bytes/sec
total size is 0  speedup is 0.00

客户端

[root@slave test2]# ls
1  a

实验三:同步软连接

服务端

[root@master test1]# ln -s 1 2
[root@master test1]# ls
1  2  a
[root@master test1]# rsync -lv /test1/* root@slave:/test2
skipping directory a
1
2 -> 1

sent 100 bytes  received 38 bytes  92.00 bytes/sec
total size is 1  speedup is 0.01

客户端

[root@slave test2]# ls
1  2  a

实验四:删除客户端创建的文件

客户端

[root@slave test2]# touch 3
[root@slave test2]# ls
1  2  3  a

服务端

[root@master test1]# rsync -av --delete /test1/ root@slave:/test2
sending incremental file list
deleting 3
./
1
a/
a/b/
a/b/c/

sent 216 bytes  received 58 bytes  548.00 bytes/sec
total size is 1  speedup is 0.00

客户端

[root@slave test2]# ls
1  2  a

Inotify可以监控目录,文件系统,删除、创建、修改(内容属性)等操作事件

服务端

[root@master ~]# ls
anaconda-ks.cfg  inotify-tools-3.14.tar.gz
[root@master ~]# tar -zxf inotify-tools-3.14.tar.gz -C /usr/src
[root@master ~]# cd /usr/src/inotify-tools-3.14/
[root@master inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify && make && make install
[root@master inotify-tools-3.14]# ln -s /usr/local/inotify/bin/* /usr/local/bin/

Inotifywait
-m 一直处于监控
-r 递归监控
-q 将监控的目录的监控信息显示在终端上
-e 指定监控的事件
–format 指定事件输出的格式

监控的事件
Move 移动
Create 创建
Delete 删除
Modify 修改内容
Close_write 修改文件内容
Close——nowrite 查看只读文件内容

输出事件的格式
%w 产生监控的路径
%f 监控的目录,输出文件名
%e 事件名称
%T 输出当前事件

监控事件(会阻塞终端)

[root@master ~]# inotifywait -mrq --format %w%f -e create,delete,move,close_write /test1

在开启一个服务端进行测试

[root@master ~]# touch /test1/www
[root@master ~]#

查看
在这里插入图片描述

Sync+inotify 自动进行同步

编写脚本

[root@master ~]# vim rsyncd.sh
#!/bin/bash
rs_script="inotifywait -mrq --format %w%f -e create,delete,move,close_write /test1"
ny_script="rsync -avz --delete /test1/* root@slave:/test2/"
$rs_script|while read file
do
         if [ -f $file ];then
             echo "备份开始..."
             $ny_script
             echo "备份结束..."
         else
            cd /test1 && rsync -az --delete ./ root@slave:/test2
         fi
done

[root@master ~]# chmod +x rsyncd.sh #添加权限
[root@master ~]# ./rsyncd.sh & #后台运行
 > 也可以使用nohup进行后台运行
    nohup  ./rsyncd.sh & 

验证

服务端

[root@master test1]# mkdir -p a/b/c
[root@master test1]# touch test.txt
[root@master test1]# ln -s test.txt new.txt

客户端

[root@slave test2]# ls
a  new.txt  test.txt
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/101906.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【AVD】简述某些视频在线播放时卡顿、本地播放时不卡顿的问题

    【AVD】简述某些视频在线播放时卡顿、本地播放时不卡顿的问题曾经在业务中遇到过这样的问题,我们编码出来的视频在Android、iOS端,使用ijkplayer内核的播放器播放时卡顿,甚至无法任意定位播放位置,将导致卡顿无法播放。今天,又有同事遇到类似的问题,而我发现,我只写过一个《用notepad++和Excel协助分析媒体文件包》,而并没有把当时遇到的问题分析记录下来。于是,在此简单说明一下。视频文件结构教科书般的教程、课程中对视频文件结构的描述非常详细,此处不赘述,简单地说,视频文件也是一种文件,是文件,就是一堆二进制数的集合,而且是一个.

    2022年9月15日
    0
  • c语言里的pow函数「建议收藏」

    头文件:#include<math.h>pow()函数用来求x的y次幂(次方),x、y及函数值都是double型,其原型为:  doublepow(doublex,doubley);pow()用来计算以x为底的y次方值,然后将结果返回。设返回值为ret,则 ret=xy。可能导致错误的情况:如果底数x为负数并且指数y不是整数,将会导致do…

    2022年4月5日
    91
  • zencart免费模板下载

    zencart免费模板下载最近工作比较忙,没有时间专门来制作这个免费的包包模板。趁国庆放假有时间,顺便就把这个免费模板制作完了。今天特别提供出来给大家下载使用。考虑到很难满足所有有的要求,所以这个模板在一些地方基本没有修改原有模板的布局,只是简单的修改CSS。不过经过修改的这个模板也还算漂亮,大体上的布局已经设置好。我们没有那么多的时间去美化一个这样的模板,俗话说:授人鱼不如授人渔。如果有兴趣做二次开发的朋友可以继续修

    2022年7月27日
    3
  • idea激活码2021.12【2021最新】

    (idea激活码2021.12)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlFN…

    2022年3月30日
    55
  • ES数据库操作入门总结「建议收藏」

    ES数据库操作入门总结「建议收藏」elasticsearch总的来说应该算是一个搜索引擎,公司使用一般是作为日志结果查询。json文档格式,倒排索引的快速以及分布式的特性,使得es可以在大量数据中快速查询到结果。windows安装和配置可参考官方网址。https://www.elastic.co/guide/en/elasticsearch/reference/current/zip-windows.html倒排查询可参考这个知乎回答https://zhuanlan.zhihu.com/p/62892586可以使用浏览器的U

    2022年5月13日
    66
  • JAVA环境配置

    JAVA环境配置JAVA开发环境配置1window系统搭建java环境1.1下载JDK首先我们需要下载java开发工具包JDK,下载地址:http://www.oracle.com/technetwork

    2022年7月1日
    17

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号