zoukankan      html  css  js  c++  java
  • Rsync+inotify搭建使用

    ## Rsync搭建
    ### 1.1 环境准备
    ```
    Rsync-Server  192.168.1.174
    Client-Rsync  192.168.1.173
    服务启动用户都是root,客户端的用户也是root
    [root@Rsync-Server file]# systemctl stop firewalld
    [root@Rsync-Server file]# getenforce 
    Permissive
    ```
    ### 1.1 检查是否安装rsync
    ```
    [root@Rsync-Server ~]# rpm -qa|grep rsync
    #如果没有安装Rsync
    [root@Rsync-Server ~]# yum install -y rsync
    ```
    
    ### 1.2 服务端配置Rsync
    ```
    [root@Rsync-Server ~]# cat /etc/rsyncd.conf 
    uid = root
    gid = root
    use chroot = no
    max connections = 200
    timeout = 300
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    log file = /var/log/rsyncd.log  
    [file]
    path = /data/file/
    ignore errors
    read only = false
    list = false
    hosts allow = 192.168.1.0/24
    hosts deny = 0.0.0.0/32
    auth users = rsync_file
    secrets file = /etc/rsync.password
    [root@Rsync-Server ~]# cat /etc/rsync.password 
    rsync_file:123456
    [root@Rsync-Server ~]# ll /etc/rsync.password
    -rw-------. 1 root root 18 Oct 19 11:37 /etc/rsync.password
    ```
    
    ### 1.3 Rsync启动脚本
    ```
    [root@Rsync-Server file]# cat /etc/init.d/rsyncd 
    #!/bin/bash
    . /etc/init.d/functions
    
    start() {
      rsync --daemon &>/dev/null
      if [ $? = 0 ];then
         action "startting rsync" /bin/true
      else
         action "startting rsync" /bin/false
      fi
    }
    
    stop() {
      if [ -e /var/run/rsyncd.pid ];then
         kill -9 `cat /var/run/rsyncd.pid` &>/dev/null
         rm -fr /var/run/rsyncd.pid /var/run/rsync.lock
         action "stopping rsync" /bin/true
      else
         echo "the rsyncd is not running"
      fi
    }
    
    status() {
      if [ -e "/var/run/rsyncd.pid" ];then
          echo -e "33[32m rsyncd is running... 33[0m"
      else
          echo -e "33[31m rsyncd is stopped 33[0m"
       fi
    }  
    
    restart() {
        stop
        start
    }
    
    case $1 in 
          start)
               start
          ;;
          stop)
               stop
          ;;
          status)
               status
          ;;
          restart)
               restart
          ;;
          *)
          echo "USAG: $0 {start|stop|status|restart}"
    esac
    "Centos7用systemctl管理Rsync"
    [root@Rsync-Server ~]# cat /usr/lib/systemd/system/rsyncd.service 
    [Unit]
    Description=fast remote file copy program daemon
    ConditionPathExists=/etc/rsyncd.conf
    
    [Service]
    EnvironmentFile=/etc/sysconfig/rsyncd
    Type=forking
    PIDFile=/var/run/rsyncd.pid
    ExecStart=/etc/init.d/rsyncd start
    ExecReload=/etc/init.d/rsyncd restart
    ExecStop=/etc/init.d/rsyncd stop
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    ```
    
    ### 1.4 Rsync客户端
    ```
    [root@Client-Rsync ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    [root@Client-Rsync ~]# yum install -y inotify-tools rsync 
    [root@Client-Rsync ~]# cat /etc/rsync.password 
    123456
    #测试一下rsync推送是否有问题
    [root@Client-Rsync ~]# touch /data/file/ceshi_Test
    [root@Client-Rsync ~]# rsync -avz /data/file/ rsync_file@192.168.1.174::file --password-file=/etc/rsync.password 
    sending incremental file list
    ./
    ceshi_Test
    
    sent 172 bytes  received 46 bytes  436.00 bytes/sec
    total size is 0  speedup is 0.00
    没有报错证明rsync推送成功,参数说明:
    -v    详细模式输出,给出传输进度等信息
    -z    压缩传输  --compress-level=NUM 指定压缩级别 1-9,9是最大压缩级别
    -a    以归档方式传输,保留文件属性
            -r    递归传输
            -t    保持文件时间信息
            -o    保持文件属主信息
            -p    保持文件权限
            -g    保持文件属组信息
            -P    显示同步过程及进度等信息
            -D    保持设备文件信息
            -l    保持软链接
            这些参数加起来等于 –a
    --exclude=PATTERN    指定排除不需要传输的文件
    --exclude-from=FILE  排除FILE中记录的文件
    --delete   保证两边数据完全一样,如果源里没有该文件,就在目标目录删除
    ```
    
    ### 1.5 配合inotify-tools
    ```
    [root@Client-Rsync ~]# cat inotify.sh 
    #!/bin/bash
    inotifywait  -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,close_write,modify,move,attrib /data/file/ 
    |while read file
    do
        rsync -avz /data/file/ rsync_file@192.168.1.174::file --password-file=/etc/rsync.password 
    done
    inotifywait参数详解:
    -m      是保持一直监听
    -r      是递归查看目录
    -q      是打印出事件
    -e      modify,delete,create,attrib 是指"监听 创建 移动 删除 写入权限"
    #启动测试
    [root@Client-Rsync ~]# nohup sh inotify.sh  & 
    ```
    
    ### 优化
    ```
    如果实际并发较大,可以适当的把inotify简单优化下:
    ls -l /proc/sys/fs/inotify/
    echo "50000000" > /proc/sys/fs/inotify/max_user_watches	加大单进程最大的文件监视数量
    echo "50000000" > /proc/sys/fs/inotify/max_queued_events	加大队列可容纳的事件数量
    ```
    

      

  • 相关阅读:
    如何得到需要下载文件的链接(路径)?
    Python之内存泄漏和内存溢出
    IO多路复用
    python-socket和进程线程协程(代码展示)
    Xshell(远程)连接不上linux服务器(防火墙介绍)
    Shell--变量
    Python读写配置文件模块--Configobj
    python文件处理之fileinput
    python之commands和subprocess入门介绍(可执行shell命令的模块)
    linux下 > /dev/null 2 > &1 的意思和如何在后台启动进程
  • 原文地址:https://www.cnblogs.com/so-cool/p/9817736.html
Copyright © 2011-2022 走看看