zoukankan      html  css  js  c++  java
  • inotify+rsync实现文件即使备份

        // Rsync + inotiry 实现即时同步数据 20140913
        [环境需求]
            1.linux 内核版本 > 2.6.13
            2.预先安装rsync , inotify是在rsync基础上运行
            3.可以查看 ls /proc/sys/fs/inotify/
                    -rw-r--r-- 1 root root 0 Sep 14 00:00 max_queued_events // 时间队列 后期生产环境需要改大点
                    -rw-r--r-- 1 root root 0 Sep 14 00:00 max_user_instances // 用户实例数 后期生产环境需要改大点
                    -rw-r--r-- 1 root root 0 Sep 14 00:00 max_user_watches // 用户监听数 后期生产环境需要改大点
    
        [安装inotify]
            1.下载inotify.tgz软件包,解压 、编译、安装 // 软件包自行baidu . goole.com 搜索inotify-tools down
            2. 安装后在/usr/local/inotify-3.14/bin/[inotifywait/inotifywatch]
                    inotifywait  [-hcmrq]  [-e  <event>  ]  [-t <seconds> ] [--format <fmt> ] [--timefmt <fmt> ]
           <file> [ ... ]
                        -h // help
                        -c // csv 
                        -r // recursion 递归监听
                        -q // quit  安静模式
                        -e // 监听时间
                        -t // 超时时间
                        -format // terminal display format 终端显示样式
    
            3. 脚本实例(启动脚本,先确定rsync配置及启动[ps:]详解)
    
    
                [shell version 2]
                    #!/bin/bash
                    USER=luowen
                    HOST=192.168.213.133
                    PASSWD_FILE=/etc/rsyncd.secrets
                    SRC_DIR=/www
                    BACK_DIR=backup
    
                    # inotify soft
                    INOTIFY_BIN='/usr/local/inotify/bin'
    
                    if [ ! -e "$PASSWD_FILE" ] && [ ! -e "$SRC_DIR" ] && [ ! -e "$INOTIFY_BIN" ];
                        then
                            echo 'CHECK CONFIGURE'
                            exit 9;
                    fi
    
                    "$INOTIFY_BIN"/inotifywait -mrq --format='%f%w' --timefmt='%F %T' -e close_write,delete,attrib,create "$SRC_DIR" | while read file 
                        do
                            # rsync
                            #echo $file
                            #`$RSYNC -avzP --delete --timeout=100 $USER@$HOST::$BACK_DIR --password-file=${PASSWD_FILE}`  > /dev/null 2>&1
                            cd $SRC_DIR && rsync -avrzP --delete --timeout=100 $SRC_DIR $USER@$HOST::$BACK_DIR --password-file=${PASSWD_FILE} > /dev/null 2>&1 
                        done
                    exit 0;
    
                #============================version 2======================
                #!/bin/sh
    
                HOST=192.168.1.100 # 备份及的ip地址
                SRC=/backup # 备份机的备份目录
                DST=www # 模块 与/etc/rsync.conf 的模块相对应
                USER=rsync-backup 
                RSYNC_PASSFILE=/etc/rsync.password
                INOTIFY_HOME=/usr/local/initify/
    
                #judge
                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 '%F %T' --format '%T%w%f' -e close_write,delete,create,attrib $SRC  | while read file 
                do
                    rsync -avzP --delete --timeout=100 --password-file=${RSYNC_PASSFILE} $SRC $USER@$HOST::$DST > /dev/null 2>&1
    
                    cd $SRC && rsync -aruz -R --delete ./ --timeout=100 --password-file=/etc/rsync $USER@$HOST::$DST > /dev/null 2>&1
    
                done
                exit 0
    
    
                [ps:]
                    rsync 命令模式:
                        1. rsync -avz -P --delete rsync://user@ip:/backup-directory/ /tmp --password-file=/etc/rsync/password
                        2. rsync -avz -P --delete user@ip:/directory /tmp --password-file=/etc/rsync/passwd
    
                        [rsync 服务器配置]
                            1. A服务器(B服务器上/www目录下的文件,备份到A服务器)
                                ip : 192.168.213.133 
                                安装rsync
                                     1. [backup]
                                           comment = public archive
                                           path = /backup
                                           use chroot = no
                                           max connections=10
                                           lock file = /var/lock/rsyncd
                                           read only = false
                                           list = yes
                                           uid = rsync # 对应的/backup目录rsync用户具有读写权限
                                           gid = rsync # 对应的/backup目录rsync用户具有读写权限
                                           auth users = luowen # 备份用户名,可以随便填写不需要是系统上的用户 用户 sync命令中的 luowen@xx.x.xx.xx 用户名
                                           secrets file = /etc/rsyncd.secrets # 存放用户认证用户名 格式为 上行的 luowen:password  // password 在B服务器上认证用
                                           hosts allow = 192.168.213.132 # 允许的机器ip地址 用于安全
    
                                    2. 创建 /etc/rsyncd.secrets 文件 (umask 177; echo 'luowen:password' > /etc/rsyncd.secrets) 文件权限必须是600,不然A服务器验证不通过
                                    3. 启动rsync服务,以daemon运行 (A服务端必须启动rsync服务B服务端不需要启动rsync服务)
                                    4. chown -R rysnc:rsync /backup
    
                            2. B服务器 (将/www的数据备份到A服务器/backup目录下)
                                1. ip 192.168.213.132
                                    安装rsync
                                2. 创建密码文件
                                    (umask 177; echo 'password' > /etc/rsyncd.secrets) # 同样此文件必须是600 否则验证不通过 password为A服务端设置的密码
    
                                3. 测试下 rsync -avzP --delete /www luowen@192.168.213.133::backup

  • 相关阅读:
    Maven MicroTip: Get sources and Javadocs
    重定向标准流
    Unix: How to redirect stderr to stdout
    ruby doc
    1. 数据流重定向(stdin,stdout,stderr)
    jruby.org十多M的jar都是放到amazon s3上提供服务的
    试试rvm
    ruby doc
    Velocity用户手册中文版
    对Windows Mobile Timer不准的总结
  • 原文地址:https://www.cnblogs.com/luowen/p/3971267.html
Copyright © 2011-2022 走看看