首页 > 代码库 > mysql备份脚本,一天执行一次全量备份,三次增量备份

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备份脚本,一天执行一次全量备份,三次增量备份