• rsync+inotify 实现自动同步


    一、定期同步和自动同步的区别
    定期同步:

    执行备份的时间固定,延期明显,实时性差 当同步源长期不变化时,密集的定期任务是浪费资源的。

    自动同步:

    一旦同步源出现变化,立即启动备份,实时性好 只要同步源无变化,则不执行备份,节省资源。

    二、inotify
    1、inotify 简介

    inotify 是一个 Linux 内核特性,它监控文件系统,并且及时向专门的应用程序发出相关的事 件警告,比如删除、读、写和卸载操作等。要使用 inotify,必须具备一台带有 2.6.13 版本的内核操作系统。
    inotify 两个监控命令:

    • inotifywait:用于持续监控,实时输出结果。
    • inotifywatch:用于短期监控,任务完成后再出结果。
    2、安装 inotify
    #tar -zxvf inotify-tools-3.13.tar.gz
    #cd inotify-tools-3.13
    # ./configure && make && make install
    
    3、inotify 命令
    格式:inotifywait -mrq -e 监控动作 1,监控动作 2  /监控目录 & 
    示例:inotifywait -mrq -e create,delete /share &  
        -m:始终保持事件监听状态  
        -r:递归查询目录  
        -q:只打印监控事件的信息 
    监控动作:
        modify(内容)
        create
        attrib(权限)
        move
        elete 
    
    三、用脚本实现单向备份
    1、单向备份脚本
    [root@centos ~]# cat sy.sh 
    #!/bin/bash
    rs_script="inotifywait -mrq -e create,delete /share"
    ny_script="rsync -avz /share/* root@100.100.100.105:/root/test/"
    $rs_script|while read
    do
    echo "备份开始..."
    	$ny_script	
    echo "备份结束..."
    done
    #最好配置免密登录
    
    2、在/share目录下进行文件删除,或者创建,看是否能够进行自动备份
    四、unison+inotify 实现双向实时同步

    rsync 在单向同步上支持的非常好,且效率很高,但是在双向同步支持较差;unison 则是双向同步的 优秀工具,但其缺点是同步效率较低。

    1、环境准备

    1.1、配置免密登录

    #### 100.100.100.103
    # ssh-keygen -t rsa -b 2048
    # ssh-copy-id root@100.100.100.105
    # mkdir /filesrc
    
    #### 100.100.100.105
    # ssh-keygen -t rsa -b 2048
    # ssh-copy-id root@100.100.100.103
    # mkdir /filedst
    
    2、安装软件(都需要安装)

    2.1、安装 inotify

    # tar -zxvf inotify-tools-3.13.tar.gz
    # cd inotify-tools-3.13
    # ./configure && make && make install
    

    2.2、安装 ocaml

    # tar -zxvf ocaml-3.10.2.tar.gz 
    # cd ocaml-3.10.2
    # ./configure 
    # make world opt && make install
    

    2.3 安装 unison

    # tar -zxvf unison-2.13.16.tar.gz 
    # cd unison-2.13.16
    # make UISTYLE=text THREADS=true STATIC=true
    # cp unison /usr/local/bin/
    
    3、脚本配置
    #### 100.100.100.103
    #!/bin/bash
    rs_script="inotifywait -mrq -e create,delete,modify /filesrc"
    ny_script="/usr/local/bin/unison -batch /filesrc/ ssh://100.100.100.105//filedst/"
    $rs_script| while read
    do
            $ny_script
    done
    
    #### 100.100.100.105
    #!/bin/bash
    rs_script="inotifywait -mrq -e create,delete,modify /filedst"
    ny_script="/usr/local/bin/unison -batch /filedst/ ssh://100.100.100.103//filesrc/"
    $rs_script| while read 
    do
            $ny_script
    done
    
    4、运行脚本,测试
    ./rc.sh &
    
  • 相关阅读:
    在 ubuntu20 上安装 docker
    在 ubuntu20 上替换原有的源,解决下载软件慢的问题
    thymeleaf 模板语法
    对 spring 中默认的 DataSource 创建进行覆盖
    Spring Security OAuth2 笔记(一)
    对 ThreadLocal 的了解(一)
    解决 docker.io 上拉取 images Get https://registry-1.docker.io/v2/: net/http: TLS handshake timeout
    Spring Security Oauth2 使用 token 访问资源服务器出现异常:Invalid token does not contain resource id (oauth2)
    windows 查看被占用的端口信息
    不写注释的程序员-Models
  • 原文地址:https://www.cnblogs.com/hjnzs/p/12189835.html
Copyright © 2020-2023  润新知