搭建文件同步服务:rsync+inotify

  • 服务端安装与配置192.168.2.10

    1. 安装rsync

    [root@localhost ~]# yum -y install rsync
    

    2. 建立密码认证文件,并且赋权限

    [root@localhost ~]# cd /etc/
    [root@localhost etc]# vi rsyncd.password
    wbc@wodata
    [root@localhost etc]# chmod 600 rsyncd.password
    

    3 修改配置文件

    [root@localhost ~]# cd /etc/
    [root@localhost etc]# vi rsyncd.conf 
    uid = root
    gid = root
    use chroot = no
    max connections = 3
    timeout = 800
    pid file = /var/run/rsyncd.pid
    lockfile = /var/run/rsyncd.lock
    log file = /var/log/rsyncd.log
    
    [uploads]
    path = /home/wbc/webroot/ROOT/upload
    ignore errors = yes
    host allow = 192.168.0.0/255.255.0.0
    #hosts deny = *
    read only = no
    write only = no
    list = yes
    auth users = wbc
    secrets file = /etc/rsyncd.password
    

    4. 安装inotify-tools,安装在etc下

    需要有编译环境 yum install make gcc gcc-c++

    [root@localhost ~]# cd /etc/
    [root@localhost etc]# wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
    [root@localhost etc]# tar zxvf inotify-tools-3.14.tar.gz 
    [root@localhost etc]# rm -rf inotify-tools-3.14.tar.gz
    [root@localhost ~]# cd /etc/inotify-tools-3.14/
    [root@localhost inotify-tools-3.14]# ./configure --prefix=/etc/inotify
    [root@localhost inotify-tools-3.14]# make
    [root@localhost inotify-tools-3.14]# make install
    

    5. 创建rsync复制脚本

    将服务端的/home/wbc/webapps/ROOT/upload/同步到备份端的/home/wbc/webapps/ROOT/uploads/,进行冗余备份,不做删除同步

    [root@localhost etc]# vi rs-upload.sh
    #!/bin/bash
    
    src='/home/wbc/webapps/ROOT/upload/'
    passwordfile='/etc/rsyncd.password'
    user='wbc'
    host0='192.168.2.11'
    rsync_module='uploads'
    
    if [ ! -d "${src}" ]; then
        mkdir -p ${src}
    fi
    
    /etc/inotify/bin/inotifywait -mrq --timefmt '%Y%m%d %H%M%S' --format '%T %w %f' -e modify,delete,create,attrib ${src} | while read DATE TIME DIR file
    
    do
        /usr/bin/rsync -vzrtopg --progress ${src} ${user}@${host0}::${rsync_module} --password-file=${passwordfile}
    
        echo "${file} was rsynced at ${DATE}_${TIME} in ${DIR}" >> /var/log/rsync-upload.log 2>&1
    done
    

    6. 后台执行脚本

    [root@localhost etc]# sh rs-upload.sh &
    
    如果有报错
    inotifywait: error while loading shared libraries: libinotifytools.so.0: cannot open shared object file: No such file or directory
    
    解决方法:
    32位系统:ln -s /etc/inotify/lib/libinotifytools.so.0 /usr/lib/libinotifytools.so.0
    64位系统:ln -s /etc/inotify/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0
    
  • 备份端安装与配置192.168.2.11

    1. 安装rsync

    [root@localhost ~]# yum -y install rsync
    

    2. 建立密码认证文件,并且赋权限

    [root@localhost ~]# cd /etc/
    [root@localhost etc]# vi rsyncd.password
    wbc@wodata
    [root@localhost etc]# chmod 600 rsyncd.password
    

    3 修改配置文件

    [root@localhost ~]# cd /etc/
    [root@localhost etc]# vi rsyncd.conf 
    uid = root
    gid = root
    use chroot = no
    max connections = 3
    timeout = 800
    pid file = /var/run/rsyncd.pid
    lockfile = /var/run/rsyncd.lock
    log file = /var/log/rsyncd.log
    
    [uploads]
    path = /home/wbc/webapps/uploads
    ignore errors = yes
    host allow = 192.168.0.0/255.255.0.0
    #hosts deny = *
    read only = no
    write only = no
    list = yes
    auth users = wbc
    secrets file = /etc/rsyncd.password
    

    4. 新增自定义服务文件

    [root@localhost etc]# vi /init.d/rsync
    #!/bin/sh
    #whoami
    #root
    #chkconfig: 345 51 49
    #description: starts the rsync deamons
    #
    
    case "$1" in
    start)
    echo "Starting Rsync ..."
    rsync --daemon --config=/etc/rsyncd.conf
    ;;
    stop)
    echo "Stop Rsync ..."
    pkill rsync > /dev/null 2>/dev/null
    ;;
    restart)
    echo "Restart Rsync ..."
    $0 stop
    $0 start
    ;;
    *)
    echo "Usage: $1 {start|stop|restart}"
    ;;
    esac
    exit 0
    

    5. 启动服务

    [root@localhost etc]# cd /init.d
    [root@localhost etc]# vi rsync
    #!/bin/sh
    #whoami
    #root
    #chkconfig: 345 51 49
    #description: starts the rsync deamons
    #
    
    case "$1" in
    start)
    echo "Starting Rsync ..."
    rsync --daemon --config=/etc/rsyncd.conf
    ;;
    stop)
    echo "Stop Rsync ..."
    pkill rsync > /dev/null 2>/dev/null
    ;;
    restart)
    echo "Restart Rsync ..."
    $0 stop
    $0 start
    ;;
    *)
    echo "Usage: $1 {start|stop|restart}"
    ;;
    esac
    exit 0
    

    6. 为自定义服务赋权限

    [root@localhost init.d]# chmod +x rsync 
    

    7. 启动服务

    [root@localhost init.d]# service rsync start
    
  • 目前为止已经安装配置好了

    1. 在服务端测试

    [root@localhost upload]# mkdir test
    
    正常情况会输出以下信息
    [root@localhost upload]# sending incremental file list
    ./
    test/
    
    sent 107 bytes  received 19 bytes  252.00 bytes/sec
    total size is 0  speedup is 0.00
    

    2. 在备份端做检查

    查看该目录下是否有test文件夹
    [root@localhost ~]# ll /home/wbc/webapps/uploads
    
  • 引文

    linux系统中rsync+inotify实现服务器之间文件实时同步

你可能感兴趣的:(搭建文件同步服务:rsync+inotify)