mysql备份脚本,一天执行一次全量备份,三次增量备份

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 -pwoyoudabaitu --database $db > ${today_dir}${db}-$(date +%Y%m%d).sql
    /bin/nice -n 19 //usr/local/mysql/bin/mysqldump -uroot -pwoyoudabaitu --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

 

以上是关于mysql备份脚本,一天执行一次全量备份,三次增量备份的主要内容,如果未能解决你的问题,请参考以下文章

Mysql备份系列--mysqldump备份(全量+增量)方案操作记录

使用mysqldump进行全量+增量备份方案操作记录

innobackupex自动备份数据库脚本

MYSQL备份与恢复

mysql 数据库定时备份 增量/全备份

mysql自动化(全量+增量)备份脚本