zoukankan      html  css  js  c++  java
  • ✍47 配置两台服务器文件夹同步

    一、场景适用

    • 用于一台服务器开启服务运行, 另一台服务器做备份使用

    二、Unison的使用

    可本地也可远程同步

    • 安装
    # 可以下载 tar 包, 也可以 yum 安装
    yum install -y unison
    
    • 配置两台服务器免密登入
    ssh-keygen
    。。。。(见下面第三段配置服务器SSH双向信任使用)
    
    • 测试连通性
    unison -testServer /root/song_test ssh://root@192.168.10.162//root/song_test
    
    • 执行同步命令
    unison -batch /root/song_test ssh://root@192.168.10.162//root/song_test
    

    三、文件夹备份ocaml+unison+inotify

    针对有修改则进行同步

    1. 环境安装

    • 安装Objective Caml compiler
    yum install make  gcc gcc-c++  # 先安装编译软件
    cd /tmp
    wget http://caml.inria.fr/pub/distrib/ocaml-4.03/ocaml-4.03.0.tar.gz
    tar  -zxvf ocaml-4.03.0.tar.gz
    cd ocaml-4.03.0
    ./configure
    make world opt
    make install
    
    • 安装ctags-etags, Unison
    yum install ctags-etags
    cd /tmp
    wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.48.4.tar.gz
    mkdir unison-2.48.4 && cd unison-2.48.4
    tar -zxvf /tmp/unison-2.48.4.tar.gz
    cd src
    make UISTYLE=text THREADS=true
    cp unison /usr/local/bin/
    //有版本信息出现则安装成功
    unison -version
    
    • 安装inotify-tools

    该软件包可以监控文件的所有事件, 我们在这可以用来监控文件夹是否修改来决定是否要进行文件夹同步

    cd /tmp/
    wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
    tar xvf inotify-tools-3.14.tar.gz
    cd inotify-tools-3.14
    ./configure
    make install
    
    • 安装rsync
    yum install rsync -y
    

    2. 配置服务器SSH双向信任

    ssh-keygen
    cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
    chmod 600 ~/.ssh/authorized_keys
    rsync -avz /root/.ssh/authorized_keys root@192.168.10.155:/root/.ssh/authorized_keys
    

    第一次需要输密码,将两台客户端与服务端建立双向链接

    3. 测试,不需要输密码说明配置成功

    ssh -p 22  192.168.10.155
    

    多台服务器之间相互测试

    然后换另一台服务器做相同的操作

    4. 配置同步

    mkdir -p /root/.unison/
    vim /root/.unison/default.prf
    

    配置文件内容,两个客户端做相同操作

    注意把注释删掉

    #Unison preferences file
    root = /local_one/one/     # 此客户端需要同步的目录
    root = ssh://root@192.168.17.130//local_two/two/   # 同步到服务端的地址
    #path = upload                      # 如果是Tomcat整个目录不用指定path
    #path = image
    #force =
    #ignore =
    batch = true
    maxthreads = 300
    #repeat = 1
    #retry = 3
    owner = true
    group = true
    perms = -1       # 使用ssh压缩传输方式
    fastcheck = false
    rsync = false       # true表示通过文件创建时间来比较两地文件,若为false比较文件的内容
    sshargs = -C
    xferbycopying = true
    confirmbigdel = false
    log = true
    logfile = /root/.unison/unison.log
    
    • 编写 .unison/default.prf 之后可以省略地址使用
    unison -testServer  # 测试连通性
    unison -batch # 测试同步
    

    5. 创建启动记录日志脚本-客户端

    cd /usr
    vim unison.sh
    
    // 内容
    #/bin/bash
    src="/local_one/one/"   # 拷贝目录
    /usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src | while read line; do
    /usr/local/bin/unison
    echo -n "$(date +%F-%T) $line" >> /var/log/inotify.log
    done
    

    注释删掉

    6. 执行脚本

    sh unison.sh
    

    7. 脚本守护模式运行

    chmod +x  unison.sh
    nohup ./unison.sh >/dev/null 2>&1 &
    
  • 相关阅读:
    DotText源码阅读
    看来这世界看得太清楚了也未必是种好事呢~~~~~~~
    在Init之前究竟执行了什么?
    孙子兵法
    Excel区域重命名
    Getbuffer ReleaseBuffer Cstring
    批量删除svn标志
    VB制作网页自动填表(强烈推荐)
    GetModuleFileName
    ansi编码
  • 原文地址:https://www.cnblogs.com/songhaixing/p/15607257.html
Copyright © 2011-2022 走看看