zoukankan      html  css  js  c++  java
  • rsync实验(单向同步和双向同步)【转】

    环境

    两台虚拟机Centos8操作系统
    ip分别为192.168.37.100和192.168.37.101
    100作为源,101作为目标服务器

    实验一:使用SSH协议数据同步

    分别准备好目录,源/filesrc 目标/filedst

    在这里插入图片描述
    可以看到上传到了101了
    在这里插入图片描述
    下载类似,在101上进行执行即可,类似scp命令
    在这里插入图片描述

    实验二:使用RSYNC协议进行数据同步

    在192.168.37.100上搭建rsync服务
    新建/etc/rsyncd.conf文件

    address = 192.168.37.100
    port 873
    log file = /var/log/rsyncd.log
    pid file = /var/run/rsyncd.pid
    [web]
            comment = huangbaokang's system backup
            path = /filesrc
            read only = no
            dont compress = *.gz *.bz2
            auth users = test1
            secrets file = /etc/rsyncd_users.db
    

    创建密码文件,并修改权限为600
    在这里插入图片描述
    后台启动rsync服务
    在这里插入图片描述
    设置映射用户对共享目录有权限(r)
    在这里插入图片描述
    测试
    在101上进行下载,先删除之前实验的文件

    rsync -avz rsync://test1@192.168.37.100/web /filedst/

    在这里插入图片描述
    拓展:–delete删除本地比服务器多出来的文件(源地址没有,目标地址有的删掉)
    在这里插入图片描述
    同理上传,反过来即可。

    rsync -avz /filedst/* rsync://test1@192.168.37.100/web
    

    要实现rsync协议免密码可以借助一个环境变量实现

    export RSYNC_PASSWORD=123456
    

    在这里插入图片描述

    实验三:使用RSYNC+INOTIFY实时同步

    前面两个实验都是手工进行同步的,这个实验将结合inotify实现实时同步,但是是单向的。第四个实验将介绍双向实时同步
    上传安装包到192.168.37.100,并解压

    yum -y install gcc*
    tar -xf inotify-tools-3.14.tar.gz
    cd inotify-tools-3.14/
    ./configure && make && make install
    

    主要用inotifywait命令来实现监控
    格式 inotifywait -mrq -e 监控动作1,监控动作2 /监控目录 &
    -m:始终保持事件监听状态
    -r:递归查询目录
    -q:只打印监控事件的信息

    监控动作:modify,create,attrib,move,delete

    编写脚本,src.sh

    #!/bin/bash
    a="inotifywait -mrq -e create,delete,modify /filesrc"
    b="rsync -avz /filesrc/* root@192.168.37.101:/filedst"
    $a | while read directory event file
    do 
    	$b
    done
    
    

    在这里插入图片描述
    要求两台机器免密码登录

    ssh-****** -t rsa -b 2048
    ssh-copy-id root@192.168.37.101
    

    启动脚本,进行测试

    bash src.sh &
    

    调整inotify监控的文件数量主要跟内核参数相关(/etc/sysctl.conf),如监控队列大小,最多监控实例数,每个实例最多监控文件数

    实验四:使用UNISON+INOTIFY实现双向实时同步

    环境安装,先安装inotify,略,实验三有安装步骤

    删除实验三的src.sh运行进程,同时清空/filesrc目录和/filedst目录的内容。
    下载unison的软件包
    https://github.com/bcpierce00/unison/releases
    在这里插入图片描述
    解压

    tar -xf unison-v2.51.3+ocaml-4.11.1+x86_64.linux.tar.gz
    

    解压会在当前目录下生成bin目录,把bin目录下的unison拷贝到/usr/local/bin下

    cp unison /usr/local/bin/
    

    两边服务器都安装inotify和unison软件
    测试
    在192.168.37.100:/filesrc目录下新建一个a.txt文件,手动执行同步命令

    /usr/local/bin/unison -batch /filesrc/ ssh://192.168.37.101//filedst/
    

    发现已经同步到了192.168.37.101的/filedst目录下

    配置脚本
    100端filesrc.sh

    #!/bin/bash
    a="inotifywait -mrq -e create,delete,modify,move /filesrc"
    b="/usr/local/bin/unison -batch /filesrc/ ssh://192.168.37.101//filedst/"
    $a | while read directory event file
    do
    	$b
    done
    

    101端filedst.sh

    [root@k8s-node1 filedst]# cat /root/filedst.sh 
    #!/bin/bash
    a="inotifywait -mrq -e create,delete,modify,move /filedst"
    b="/usr/local/bin/unison -batch /filedst/ ssh://192.168.37.100//filesrc/"
    $a | while read directory event file
    do
    	$b
    done
    

    把这两个脚本后台执行,进行测试

    [root@k8s-master ~]# bash /root/filesrc.sh &
    [root@k8s-node1 ~]# bash /root/filedst.sh &
    

    测试创建文件,重命名文件,删除文件等,可以发现一边修改会同时影响另外一边,达到了双向实时同步的目的。

    注意:双向自动同步,监控目录和数据同步时,源目录不能使用*通配符传输,否则会变成死循环。

    同时该实验需要设置双方免密登录。

    转自

    (26条消息) rsync实验(单向同步和双向同步)_huangbaokang的博客-CSDN博客_rsync 双向
    https://blog.csdn.net/huangbaokang/article/details/109214617

    rsync实验(单向同步和双向同步) - 灰信网(软件开发博客聚合)
    https://www.freesion.com/article/36351404641/

    inotify+rsync双向同步_suifeng528_51CTO博客
    https://blog.51cto.com/cpvbird/1592642

  • 相关阅读:
    A Look Inside Presentation Controllers
    iOS用户数据安全:Keychain、Touch ID以及1Password
    iOS开发UI篇—UITableView全面解析
    iOS 上的相机捕捉
    iOS UIScrollView
    ViewController类中得方法和属性的用途
    ios 中__bridge,__bridge_transfer和__bridge_retained详解
    __weak如何实现对象值自动设置为nil
    iOS开发UI篇—UIWindow简单介绍
    iOS:App启动过程详解
  • 原文地址:https://www.cnblogs.com/paul8339/p/15762618.html
Copyright © 2011-2022 走看看