zoukankan      html  css  js  c++  java
  • mysql备份脚本,每天执行一次全量备份,三次增量备份

    线上一个小业务的mysql备份

    全量备份

    #!/bin/bash
    #crete by hexm at 2016-10-02
    #scripte name : full_backup.sh
    #descriptioni :  mysql full backup. oneday at a time.
    
    #time info
    date=`date +%Y%m%d`
    #time_now1=`date +%Y%m%d-%H:%M:%S`
    
    #dir info
    BACKUP_DIR="/app/dbbackup/full_backup/"
    today_dir="/app/dbbackup/full_backup/${date}"
    LOG=/app/dbbackup/logs/dumpfull_${date}.txt
    if [ ! -d "${BACKUP_DIR}" ]; then
      mkdir -p "${BACKUP_DIR}"
    fi
    if [ ! -d "${today_dir}" ]; then
      mkdir -p "${today_dir}"
    fi
    
    
    echo `date +%Y%m%d%H%M%S` > ${today_dir}/time.txt
    # 循环databases数组
    time_now1=`date +%Y%m%d-%H:%M:%S`
    echo "${time_now1} start to full backup" >> ${LOG}
    for db in `/usr/local/mysql/bin/mysql -uroot -pwoyoudabaitu -e 'show databases'|grep -Ev "mysql|test|performance_schema|information_schema|Database"`
    do
        # 备份数据库生成SQL文件
        #/bin/nice -n 19 /usr/local/mysql/bin/mysqldump -uroot -pwoyou --database $db > ${today_dir}${db}-$(date +%Y%m%d).sql
        /bin/nice -n 19 //usr/local/mysql/bin/mysqldump -uroot -pwoyou --database --flush-privileges --master-data --single-transaction --opt $db > ${today_dir}/${db}-${date}.sql
    done
    
    time_now2=`date +%Y%m%d-%H:%M:%S`
    echo "${time_now2} full backup successfull" >> $LOG
    
    
    #get position
    sleep 2
    
    if [ -f "${today_dir}/db_179_act-${date}.sql" ]; then
      sed -n '22p' ${today_dir}/db_179_act-${date}.sql |awk -F"[! ]" '{print $4,$10}' > $today_dir/position
      echo "${time_now2} get position ok!" >> $LOG
    else
      time_now3=`date +%Y%m%d-%H:%M:%S`
      echo "${time_now3} get position faild" >> $LOG
      #$SEND_MSG
    fi
    
    #remove 1 days ago backup.
    find $BACKUP_DIR  -mtime +7 -delete
    #echo "remove 1 days ago full backup!" >> $LOG

    增量备份

    #!/bin/bash
    #crete by hexm at 2016-10-03
    #scripte name : increment_backup.sh
    #descriptioni :  mysql increment backup. 6 hours at a time.
    
    
    #time info
    date=`date +%Y%m%d`
    time_hour=`date +%Y%m%d%H`
    #time_now1=`date +%Y%m%d-%H:%M:%S`
    
    #dir info
    BACKUP_DIR=/app/dbbackup/increment_backup/
    LOG=/app/dbbackup/logs/dumpincr_${date}.txt
    #TIME_DIR=/app/dbbackup/full_backup/${time_hour}
    if [ ! -d "${BACKUP_DIR}" ]; then
      mkdir -p "${BACKUP_DIR}"
    fi
    
    time_now1=`date +%Y%m%d-%H:%M:%S`
    echo "${time_now1} start to increment backup" >> ${LOG}
    
    
    cd /app/mysql.bin
    tar cf ${time_hour}_mysql_bin.tar.gz *
    wait
    mv /app/mysql.bin/*.tar.gz ${BACKUP_DIR}
    cd
    
    ls ${BACKUP_DIR}/${time_hour}_mysql_bin.tar.gz &> /dev/null
    
    if [ $? -eq 0 ];then
      time_now2=`date +%Y%m%d-%H:%M:%S`
      echo "${time_now2} increment backup successfull" >> $LOG
    else
      echo "${time_now2} increment backup faild" >> $LOG
      #SEND_MSG
    fi
    
    #remove 1 days ago backup.
    #find $BACKUP_DIR  -mtime +7 -delete
    find $BACKUP_DIR -type f -name "*.tar.gz" -mtime +7 -delete
    #echo "remove 1 days ago increment backup!" >> $LOG

    定时任务

    #mysqldump backup mysql 
    00 02 * * * /bin/bash /app/bin/full_backup.sh & &>/dev/null
    00 08 * * * /bin/bash /app/bin/increment_backup.sh & &>/dev/null
    00 14 * * * /bin/bash /app/bin/increment_backup.sh & &>/dev/null
    00 20 * * * /bin/bash /app/bin/increment_backup.sh & &>/dev/null
  • 相关阅读:
    HDU 5273 Dylans loves sequence 暴力递推
    HDU 5285 wyh2000 and pupil 判二分图+贪心
    HDU 5281 Senior's Gun 贪心
    HDU 5651 xiaoxin juju needs help 逆元
    HDU 5646 DZY Loves Partition
    HDU 5366 The mook jong
    HDU 5391Z ball in Tina Town 数论
    HDU 5418 Victor and World 允许多次经过的TSP
    HDU 5642 King's Order dp
    抽屉原理
  • 原文地址:https://www.cnblogs.com/xiaoming279/p/6178661.html
Copyright © 2011-2022 走看看