mysql備份計劃——xtrabackup

        備份腳本每天凌晨3點通過crontab執行一次,需要注意的是prepare的文件都是加了--redo-only參數的,直接用的話需prepare一次最後的增備文件(不加redo-only)。

#!/bin/bash

##1.每週一、三、五凌晨3點基於上次增備做一次增量備份,並對本次備份文件做prepare合併到之前的prepare文件中
##2.每週日凌晨3點全備
##3.此腳本每天凌晨3點執行,刪除超過30天的全備

user=root
password=Test123456
database=test_db
host=192.168.1.201
cnf=/etc/my.cnf
week=`date +'%w'`
ibf=/xtrabackup/incremental_backup_folder
wbf=/xtrabackup/whole_backup_folder
ipf=/xtrabackup/incremental_prepare_folder
bi=/xtrabackup/backup.info
#上一次的增量備份文件
incremental=`ls $ibf -ltr |tail -1|awk '{print $NF}'`
date=`date +'%Y-%m-%d %H:%M:%S'`
echo " " >> $bi
echo "==============================$date===================================" >> $bi

case $week in
[1,3,5])
   if [ "$incremental" = "0" ]; then
     then
     #第一次先做全備
     innobackupex --defaults-file=$cnf --user=$user --password=$password --host=$host --database=$database  $ibf &>> $bi
     cp -R $ibf/* $ipf
     #prepare
     innobackupex --apply-log --redo-only --use-memory=2G $ipf/* &>> $bi
   else
     #增量備份
     innobackupex --defaults-file=$cnf --user=$user --password=$password --host=$host --incremental $ibf --incremental-basedir=$incremental &>> $bi
     #prepare
     innobackupex --apply-log --redo-only --use-memory=2G $ipf/* --incremental-dir=$incremental &>> $bi
   fi 
   echo "Today is the $week day of the week,backup successful!" >> $bi
;;
7)
   #全備
   innobackupex --defaults-file=$cnf --user=$user --password=$password --host=$host --database=$database  $wbf &>> $bi
   echo "Today is the $week day of the week,backup successful!" >> $bi
;;
*)
   echo "Today is the $week day of the week,not backup!" >> $bi
;;
esac

#刪除超過30天的全備
find $wbf -mtime +30 -exec rm -rf {} \;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章