zoukankan      html  css  js  c++  java
  • Linux 之 rsync实现服务器的文件同步

    rsync实现服务器的文件同步


     参考文献链接:

    一、rsync实现负载均衡集群文件同步,搭建线上测试部署环境

    二、rsync

    三、rsync常见错误

    四、rsync 安装使用详解


     环境部署:

      服务器1:192.168.1.169,作为客户端

      服务器2:192.168.1.167,作为服务端

      实现功能:每当169服务器中的文件发生改变时,就同步到167服务器中。


     服务端配置(即167服务器的配置):

    (1)软件安装
    yum install rsync xinetd
    (2)为 rsyncd 服务编辑配置文件,默认没有,需自己编辑
    vim /etc/rsyncd.conf
    写入以下内容:
    uid = root
    gid = root
    use chroot = no
    max connections = 5
    timeout = 600
    pid file = /var/run/rsyncd.pid
    lockfile = /var/run/rsyncd.lock
    log file = /var/log/rsyncd.log
    [web1]
    path = /usr/local/nginx/html/hello/
    ignore errors = yes
    read only = no
    write only = no
    hosts allow = 192.168.1.169
    hosts deny = *
    list = yes
    auth users = web
    secrets file = /etc/web.passwd
    (3)创建文件同步的目录,上面配置里的path,如果有就不用创建了
    mkdir /usr/local/nginx/html/hello/
    (4)创建配置中的密码文件,并增加权限:
    echo "web:123" > /etc/web.passwd
    chmod 600 /etc/web.passwd
    (5)重新启动
    service xinetd restart


     客户端配置(即169服务器的配置):

    (1)安装软件
    yum -y install rsync
    (2)创建web目录
    mkdir /usr/local/nginx/html/hello/
    (3)设置密码并设置权限
    echo "123"> /tmp/rsync.password
    chmod 600 /tmp/rsync.password

    (4)关闭防火墙:service iptables stop。


    在客户端测试(即169服务器):
    rsync -avzP --delete --password-file=/tmp/rsync.password /usr/local/nginx/html/hello/ web@192.168.1.167::web1

    如果看到文件同步过去表示成功。


    数据实时同步:
      环境:Rsync + Inotify-tools。

      下载安装

      wget https://jaist.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
      tar -zxvf inotify-tools-3.13.tar.gz
      mkdir /usr/local/inotify
      cd inotify-tools-3.13
      ./configure --prefix=/usr/local/inotify/
      make && make install

    设置环境变量

      vim /etc/profile
      在末尾增加一行:
      export PATH=$PATH:/usr/local/inotify/bin
      使配置生效:
      source /etc/profile


    echo '/usr/local/inotify/lib' >> /etc/ld.so.conf --加载库文件
    ldconfig
    ln -s /usr/local/inotify/include /usr/include/inotify


     测试脚本:

    创建shell文件:
    vim /test.sh
    输入以下内容:

    #!/bin/bash
    
    src=/usr/local/nginx/html/hello/
    user=web
    host1=192.168.1.167
    dst1=web1
    passpath=/tmp/rsync.password
    
    /usr/local/inotify/bin/inotifywait 
    -mrq --timefmt '%d/%m/%y' 
    --format '%T %w%f%e' 
    -e modify,delete,create,attrib 
    /usr/local/nginx/html/hello/ | while read files 
    do
        rsync  -vzrtopg --delete --progress --passfile=$passfile-path  $src $user@$host1::$dst1
        echo "${files} was rsyncd" >>/tmp/rsync.log 2>&1
    done

    设置自动运行:

    chmod 755 /data/test/test.sh
    /data/test/test.sh &
    echo '/data/test/test.sh &' >> /etc/rc.local --设置开机自启


     扩展知识:

      查看已安装的软件包
        yum list rsync
      卸载rsync
        yum remove rsync

      常见错误:    

        问题一:
          rsync: failed to set times on “directory” Operation not permitted (1)
          解决:
            请检查/etc/rsyncd.conf这个配置文件是否正确。

        问题二:

          @ERROR: auth failed on module web
          rsync error: error starting client-server protocol (code 5) at main.c(1657) [Receiver=3.1.3]
          原因:
            服务器端该模块(web)需要验证用户名密码,但客户端没有提供正确的用户名密码,认证失败。
            提供正确的用户名密码解决此问题。

        问题三:

          inotifywait: error while loading shared libraries: libinotifytools.so.0: cannot open shared object

          [root@db zzh]# ll /proc/sys/fs/inotify (如果有下列三项则支持inotifytools)
          total 0
          -rw-r--r-- 1 root root 0 Sep 20 16:52 max_queued_events
          -rw-r--r-- 1 root root 0 Sep 20 16:52 max_user_instances
          -rw-r--r-- 1 root root 0 Sep 20 16:52 max_user_watches

          解决:
          [root@db zzh]# ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0

        问题四:

          wile read 命令未找到

          解决:

            检出sh文件,或重新编写。

    rsync配置文件说明:

    uid = root #--rsync运行权限为root
    gid = root #--rsync运行权限为root
    use chroot = no #--是否让进程离开工作目录
    max connections = 5 #--最大并发连接数,0为不限制
    timeout = 600 #--超时时间
    pid file = /var/run/rsyncd.pid #--指定rsync的pid存放路径
    lockfile = /var/run/rsyncd.lock #--指定rsync的锁文件存放路径
    log file = /var/log/rsyncd.log #--指定rsync的日志存放路径
    [web1] #--模块名称
    path = /data/test/src #--该模块存放文件的基础路径
    ignore errors = yes #--忽略一些无关的I/O错误
    read only = no #--客户端可以上传
    write only = no #--客户端可以下载
    hosts allow = 192.168.8.167 #--允许连接的客户端主机ip
    hosts deny = * #--黑名单,*表示任何主机
    list = yes
    auth users = web #--认证此模块的用户名
    secrets file = /etc/web.passwd #--指定存放“用户名:密码”格式的文件


     

  • 相关阅读:
    慕课网 k8s环境搭建坑点
    这种yum源为阿里云
    linux yum安装jdk
    docker 常用命令
    docker部署
    docker windows安装 就是这么简单
    idea
    jrebel 破解失败 Unexpected response from server
    bladex flowable 表关系
    bladex 接口
  • 原文地址:https://www.cnblogs.com/gyfluck/p/10497578.html
Copyright © 2011-2022 走看看