一、环境

    192.168.10.2上的/data目录下的数据要同步备份到192.168.10.5上/data


二、部署

1、192.168.10.5

    1)、安装rsync并配置

    yum -y install rsync


    vim /etc/xinetd.d/rsync

    service rsync

    {

            disable = no

            flags           = IPv6

            socket_type     = stream

            wait            = no

            user            = root

            server          = /usr/bin/rsync

            server_args     = --daemon

            log_on_failure  += USERID

    }


    vim  /etc/rsync/rsyncd.conf

    uid = root

    gid = root

    use chroot = false

    read only = false

    hosts allow=192.168.10.0/255.255.255.0

    hosts deny=*

    max connections = 8

    pid file = /var/run/rsyncd.pid

    lock file = /var/run/rsync.lock

    log file = /var/log/rsync.log

    transfer logging = yes

    log format = %t %a %m %f %b

    syslog facility = daemon

    timeout = 20

    [data]

    path = /data

    list=no

    comment = backup_data

    auth users = backup

    secrets file = /etc/rsync/rsyncd.passwd

    strict modes = true

    ignore errors = true


    2)、配置密码文件

    echo "backup:backup@123" > /etc/rsync/rsyncd.passwd

    chmod 600 /etc/rsync/rsyncd.passwd


    3)、启动rsync服务

    usr/bin/rsync --daemon --config=/etc/rsync/rsyncd.conf


2、192.168.10.2 配置

    1)、inotify安装

    tar xf inotify-tools-3.14.tar.gz

    cd inotify-tools-3.14

    ./configure --prefix=/usr/local/services/inotify-3.14

    make && make install

    cd /usr/local/services

    ln -sf inotify-3.14 inotify


    2)、监控脚本

    vim inotify.sh

    #!/bin/bash

    host=192.168.10.5  #备份主机ip

    src=/data/        #本地监控目录

    dst=data         #备份主机上rsync的模板

    user=backup      #备份主机的rsync服务的虚拟用户

    rsync_passfile=/usr/local/services/rsyncd.passwd   #本地调用rsync服务的密码文件(只有密码)

    inotify_home=/usr/local/services/inotify    #inotify的安装目录


    if [ ! -e "$src" ] || [ ! -e "${rsync_passfile}" ] \

        || [ ! -e "${inotify_home}/bin/inotifywait" ] || [ ! -e "/usr/bin/rsync" ];then

        echo "Check File and Folder"

    exit 9

    fi

    ${inotify_home}/bin/inotifywait -mrq --timefmt '%y/%m/%d %H:%M' --format '%T %w%f' -e modify,attrib,moved_to,move,create,delete,unmount,close_write,moved_from,delete_self $src \

| while read file

do

    cd $src && rsync -aruz -R --delete ./  --timeout=5  $user@$host::$dst --password-file=${rsync_passfile} >/dev/null 2>&1

    done

    exit 0


    3)、配置密码文件

    echo "backup@123" > /usr/local/services/rsyncd.passwd

    chmod 600 /usr/local/services/rsyncd.passwd


    4)、后台运行脚本

    /usr/local/services/inotify.sh &