Skip to content
SRE运维进阶之路SRE运维进阶之路
github icon

    author iconClaycalendar icon2021年5月11日timer icon大约 1 分钟

    此页内容
    • 1 inotify slave部署
      • 1.1 安装rsync
      • 1.2 添加用户和模块目录,并更改用户和组
      • 1.3 修改配置文件/etc/rsyncd.conf
      • 1.4 配置虚拟用户的密码文件
    • 2 inotify master部署
      • 2.1 安装inotify 3.14
      • 2.2 创建rsync服务的密码文件
      • 2.3 编写执行脚本
      • 2.4 将脚本加入后台执行

    由于我的jenkins和ansible没有安装在一台主机上,所以,现在利用rsync+inotify实现jenkins的工作目录,同步到ansible主机上

    环境如下:

    inotify-master IP :192.168.162.175

    inotify-slave IP:192.168.162.119

    # 1 inotify slave部署

    # 1.1 安装rsync

    yum install rsync –y
    
    1

    # 1.2 添加用户和模块目录,并更改用户和组

    useradd rsync –s /sbin/nologin –M
    mkdir -p /var/lib/jenkins/workspace
    chown rsync.rsync /var/lib/jenkins/workspace
    
    1
    2
    3

    # 1.3 修改配置文件/etc/rsyncd.conf

    内容如下:

    # egrep -v "^#|^$" /etc/rsyncd.conf
    uid = rsync
    gid = rsync
    use chroot = no
    max connections = 200
    timeout = 300
    fake super = yes 
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    log file = /var/log/rsyncd.log
    ignore errors
    read only = false
    list = false
    hosts allow = 192.168.162.0/24
    auth users = rsync_backup
    secrets file = /etc/rsync.password
    [jenkinsbackup]
    comment = "jenkinsbackup dir by clay"
    path = /var/lib/jenkins/workspace
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19

    # 1.4 配置虚拟用户的密码文件

    # cat rsync.password 
    rsync_backup:clay123
    # chmod 600 /etc/rsync.password 
    # rsync --daemon
    
    1
    2
    3
    4

    # 2 inotify master部署

    # 2.1 安装inotify 3.14

    # cd /opt
    # wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
    # tar zxf inotify-tools-3.14.tar.gz
    # cd inotify-tools-3.14
    # ./configure --prefix=/usr/local/inotify
    # make && sudo make install
    
    1
    2
    3
    4
    5
    6

    # 2.2 创建rsync服务的密码文件

    # cat /etc/rsync.password 
    clay123
    # chmod 600 /etc/rsync.password
    
    1
    2
    3

    # 2.3 编写执行脚本

    vim /usr/local/inotify.sh

    #!/bin/bash
    HOST=192.168.162.119
    SRC=/var/lib/jenkins/workspace
    DEST=jenkinsbackup
    USER=rsync_backup
    RSYNC_PASSFILE=/etc/rsync.password
    INOTIFY_HMOE=/usr/local/inotify
    
    if [ ! -e "${SRC}" ] \
      || [ ! -e "${RSYNC_PASSFILE}" ] \
      || [ ! -e "${INOTIFY_HMOE}/bin/inotifywait" ] \
      || [ ! -e "/usr/bin/rsync" ]; then
      echo "Check File and Folder"
      exit 1
    fi
    
    ${INOTIFY_HMOE}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' \
      --format '%T %w%f' -e close_write,delete,create,attrib ${SRC} \
      | while read file; do
      cd ${SRC}
      rsync -aruz -R --delete ./  --timeout=100 ${USER}@${HOST}::${DEST} \
        --password-file=${RSYNC_PASSFILE} >/dev/null 2>&1
    done
    exit 0
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24

    # 2.4 将脚本加入后台执行

    # sh inotify.sh &
    
    1

    主要参考链接:https://www.cnblogs.com/jefflee168/p/6795201.html

    补充链接:https://www.cnblogs.com/clsn/p/7707822.html

    edit icon编辑此页open in new window
    上次编辑于: 2021/5/11 03:54:51
    贡献者: clay-wangzhi
    备案号:冀ICP备2021007336号
    Copyright © 2023 Clay