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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • stat函数使用

    stat函数使用要点 通过 stat 函数获取文件属性代码示例 运行结果 a outa outatime mtime ctime a outxyz outErroroccu

    2025年7月31日
    4
  • HDU4876ZCC loves cards(多校题)

    HDU4876ZCC loves cards(多校题)

    2022年1月21日
    46
  • 堆栈溢出排查

    堆栈溢出排查ps-ef|greprimsjmap-histo:live28972|head-7启动程序时配置内存溢出时自动导出dump文件-XX:+HeapDumpOnOutOfMemoryError-XX:HeapDumpPath=/home/d5000/eas/easDmSync/heapdump.hprof

    2025年6月13日
    1
  • vue 中 Promise 使用方法

    vue 中 Promise 使用方法1.Promise基本概念:Promise是一个构造函数,所以可以new出一个Promise的实例;在Promise上有两个函数resolve(成功之后的回调函数)和reject(失败后的回调函数);在Promise构造函数的prototype属性上,有一个.then()方法。所以只要是Promise构造函数创建的实例,都可以访问到.then()方法;Promise表示一个一步操作,每当我们new一个Promise的实例,这个实例就代表具体的异步操作。Promise创建

    2022年6月15日
    253
  • 网易云音乐如何将多个账号的音乐合并到一个账号

    网易云音乐如何将多个账号的音乐合并到一个账号

    2021年5月17日
    716
  • 万物共享的物联网架构「建议收藏」

    万物共享的物联网架构「建议收藏」前言作为物联网领域最贴近用户的一个分支,智能家居行业在这两年持续火热。但是,除了智能家居外,物联网领域还有很多重要的组成部分:车联物流、智慧医疗、智慧社区、公共基础服务、智慧农业等。由于物联网的第一批先驱者往往都是从某个具体子行业转型过来的,对于物联网的认知也如盲人摸象,管中窥豹,很难有全局性的眼光。基于国外物联网大神DanielKarzel,HanneloreMarginean,

    2022年9月18日
    2

发表回复

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

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