zoukankan      html  css  js  c++  java
  • inotify+rsync

    backup_to_rsync.sh
    #!/bin/bash
    #source function library
    . /etc/init.d/functions
    rsync_host=rsync.etiantian.org

    #define variables
    IP=$(ifconfig eth1|awk -F '[ :]+' 'NR==2 {print $4}')
    Path="/backup/$IP"
    TIME=`/bin/data +%F`
    BackupFile=/server/scripts/backuplist

    [ ! -d $Path ] && mkdir -p $Path
    [ ! -f $BackupFile ] && [
    echo "Please give me $BackupFile"
    exit 1
    ]

    function Msg(){
    if [ $? -eq 0 ];then
    action "$*" /bin/true
    else
    action "$*" /bin/false
    fi
    }

    # Backup config files
    tar zcfh $Path/conf_${TIME}.tar.gz `cat $BackupFile` &>/dev/null
    Msg 'Backup config files'

    #Make a flag for backup
    find $Path -type f -name "*${TIME}.tar.gz"|xargs md5sum >$Path/flag_$TIME 2>/dev/null
    Msg 'Make a flag for backup'

    # Send backup to backup server
    rsync -az $Path rsync_backup@${rsync_host}::backup --password-file=/etc/rsync.password &>/dev/null
    Msg 'Send backup to backup server'

    #Delete backup a week ago
    find ${Path} -type f -name "*.tar.gz" -mtime +7 | xargs rm -f &>/dev/null
    Msg 'Delete backup a week ago'


    echo -e "#backup data 00 00 * * * /bin/sh /server/scripts/backup_to_rsync.sh &>/dev/null" >> /var/spool/cron/root


    /server/scripts/rsync.sh
    #!/bin/bash
    ###################
    # file name : rsync.sh
    # description : install and config rsync server
    ###################
    . /etc/init.d/functions

    function Msg(){
    if [ $? -eq 0 ];then
    action "$*" /bin/ture
    else
    action "$*" /bin/false
    fi
    }

    function rsyncd_conf(){
    cat >/etc/rsyncd.conf<<EOF
    sync server
    uid = rsync
    gid = rsync
    use chroot = no
    max connections = 2000
    timeout = 600
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsyncd.lock
    log file = /var/log/rsyncd.log
    ignore errors
    read only = false
    list = false
    hosts allow = 172.16.1.0/24
    hosts deny = 0.0.0.0/24
    auth users = rsync_backup
    secrets file = /etc/rsync.password
    ##################
    [backup]
    comment = back server by
    path = /backup
    EOF
    Msg "rsyncd_conf"
    }

    function rsync(){
    useradd -u 666 rsync -M -s /sbin/nologin
    [ ! -d /backup ] && mkdir -p /backup
    [ ! -d /server ] && mkdir -p /server/tools
    [ ! -d /server/scripts ] && mkdir -p /server/scripts
    chown -R rsync.rsync /backup
    echo "rsync_backup:123456" > /etc/rsync.password &&
    chmod 600 /etc/rsync.password
    # 判断rsync是否启动
    [ ! -f /server/scripts/rsyncd ] && {
    echo "/server/scripts/rsyncd is not exist,error."
    exit 2
    }
    cp /server/scripts/rsyncd /etc/init.d/
    chmod 755 /etc/init.d/rsyncd
    netstat -ntulp | grep "873"
    if [ $? -eq 0 ];then
    echo "rsync is running" > /dev/null
    else
    /etc/init.d/rsyncd restart
    fi
    /sbin/chkconfig --add rsyncd
    /sbin/chkconfig rsyncd on
    Msg "rsync"
    sleep 1
    }

    function postfix(){
    if [ `grep "oldboyedu" /etc/mail.rc|wc -l` -eq 0 ];then
    /bin/cp /etc/mail.rc /etc/mail.rc.$RANDOM
    echo -e "set from=oldboyedu@163.com smtp=smtp.163.com set smtp-auth-user=oldboyedu smtp-auth-password=oldboy123 smtp-auth=login" >> /etc/mail.rc
    /etc/init.d/postfix restart
    else
    echo "postfix is not need to config it."
    fi
    sleep 1
    }

    function crond_check(){
    [ `crontab -l | grep "check.sh"| wc -l` -eq 0 ] &&{
    echo -e "#check backup 00 08 * * * /bin/sh /server/scripts/check.sh >/dev/null 2>&1" >> /var/spool/cron/root
    crontab -l
    sleep 2
    } || {
    echo "check.sh cron is exist, no config"
    }
    }
    function main(){
    rsyncd_conf
    rsync
    postfix
    crond_check
    }
    main


    #!/bin/bash
    # filename : rsyncd
    # description :
    #chkconfig: 2345 56 24
    . /etc/init.d/functions
    RETVAL=0
    prog="rsync"
    #check if rsync is already running
    start(){
    if [ ! -f /var/run/rsyncd.pid ];then
    /usr/vin/rsync --daemon --config=/etc/rsyncd.conf
    fi
    RETVAL=$?
    sleep 1
    if [ $RETVAL -eq 0 ];then
    action "starting $prog ..." /bin/true
    else
    action "starting $prog ..." /bin/false
    fi
    return $RETVAL
    }

    stop(){
    killproc /usr/sbin/rsync
    RETVAL=$?
    sleep 1
    if [ $RETVAL -eq 0 ];then
    rm -f /var/run/rsyncd.pid
    action "stopped $prog ..." /bin/true
    else
    action "stopped $prog ..." /bin/false
    fi
    }
    restart(){
    $0 stop
    $0 start
    }

    case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart|reload)
    restart
    ;;
    *)
    echo "Usage:$0 {start|stop|restart}"
    exit 1
    esac


    #!/bin/bash
    # filename : check_bak.sh
    DIR=/backup
    TIME=`/bin/date +%F`
    log=/tmp/$TIME-check.log

    [ -d $DIR ] && {
    find $DIR -type f -name "flag_$TIME" |xargs md5sum -c >$log 2>/dev/null
    mail -s "$(date +%F_%T) back check result" 12343qq.com <&log
    }


    #!/bin/bash
    # filename : inotifyd.sh
    [ ! -f /etc/yum.repos.d/epel.repo ] &&
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
    if [ $? -eq 0 -a `rpm -qa inotify-tools|wc -l` -ne 1 ];then
    yum -y install inotify-tools
    fi
    inotify=/usr/bin/inotifywait
    rsync_host=rsync.etiantian.org
    [ ! -d /data0 ] && mkdir /data0
    $inotify -mrq --format '%w%f' -e create,close_write,delete /data0
    | while read file
    do
    cd / &&
    rsync -az /data0 --delete rsync_backup@${rsync_host}::backup/ --password-file=/etc/rsync.password
    done

  • 相关阅读:
    Element库的Vue版本ElementUI的本地引入方法
    在Win7操作系统上安装VS2017报错:安装程序清单签名验证失败
    [转]五大主流浏览器及四大内核
    [转]idea2021.1破解版 附安装教程免激活码
    [转]Node.js安装详细步骤教程(Windows版)
    [转]Windows系统下彻底删除Windows.old 文件夹的方法
    Springboot+Vue进行Web开发时特别需要注意的小事项
    基带信号与频带信号,基带传输与频带传输各是什么?两者有什么区别?
    springboot的Web项目编译运行时提示错误:Field userService in com.cetc.UserManger.controller.UserController required a bean of type 'com.cetc.UserManger.service.UserService' that could not be found.
    创建springboot项目时出现Selected Java version 11 is not supported by SDK (maximum 8)
  • 原文地址:https://www.cnblogs.com/shengy/p/7418665.html
Copyright © 2011-2022 走看看