備份策略:
每周日做一全備,每天做一增量備份。
一:全備指令碼
cat xtrabackup_full.sh
#!/bin/bash
local_ip="$(/sbin/ifconfig eth0|grep 'inet addr'|awk -F : '{print $2}'|cut -d ' ' -f1)"
email='pingping1990429@163.com'
user='root'
passwd='system@123'
my_config='/etc/my.cnf'
log=$(date +%Y%m%d%H%M).log
str=$(date +%Y%m%d%H%M).tar.gz
backup_dir='/backup/mysql/xtrabackup'
source='pingping1990429@163.com'
target='yabingshi@163.com'
title='xtrabackup information'
MailUser='pingping1990429'
MailPassword='jyp20100'
content1='Server_name:'$(hostname)' Server_ip:'$local_ip' '$(date +"%y-%m-%d %H:%M:%S")' mysql full backup Success!'
content2='Server_name:'$(hostname)' Server_ip:'$local_ip' '$(date +"%y-%m-%d %H:%M:%S")' mysql full backup Faild!'
echo "Start to backup at $(date +%Y%m%d%H%M)"
if [ ! -d "$backup_dir" ];then
mkdir -p $backup_dir
fi
#innobackupex --defaults-file=$my_config --user=$user --password=$passwd --database=$database --stream=tar $backup_dir 2>$backup_dir/$log | gzip 1>$backup_dir/$str
innobackupex --defaults-file=$my_config --user=$user --password=$passwd $backup_dir
if [ $? -eq 0 ];then
echo "Backup is finish! at $(date +%Y%m%d%H%M)"
echo "Server_name:$(hostname) Server_ip:$local_ip $(date +"%y-%m-%d %H:%M:%S") mysql full backup Success!"
/usr/local/bin/sendEmail -f $source -t $target -s smtp.163.com -u $title -xu $MailUser -xp $MailPassword -m $content1
exit 0
else
echo "Backup is Fail! at $(date +%Y%m%d%H%M)"
echo "Server_name:$(hostname) Server_ip:$local_ip $(date +"%y-%m-%d %H:%M:%S") mysql full backup Fail!"
/usr/local/bin/sendEmail -f $source -t $target -s smtp.163.com -u $title -xu $MailUser -xp $MailPassword -m $content2
exit 1
fi
echo "Backup Process Done"
二:增量備份
[root@single download]# cat xtrabackup_incremental.sh
#!/bin/bash
#增量備份依賴於前一天的備份,所以假如前一天備份失敗,後續的也會失敗
local_ip="$(/sbin/ifconfig eth0|grep 'inet addr'|awk -F : '{print $2}'|cut -d ' ' -f1)"
email='pingping1990429@163.com'
user='root'
passwd='system@123'
my_config='/etc/my.cnf'
log=$(date +%Y%m%d%H%M).log
str=$(date +%Y%m%d%H%M).tar.gz
backup_dir='/backup/mysql/xtrabackup'
source='pingping1990429@163.com'
target='yabingshi@163.com'
title='xtrabackup information'
MailUser='pingping1990429'
MailPassword='jyp20100'
content1='Server_name:'$(hostname)' Server_ip:'$local_ip' '$(date +"%y-%m-%d %H:%M:%S")' mysql incremental backup Success!'
content2='Server_name:'$(hostname)' Server_ip:'$local_ip' '$(date +"%y-%m-%d %H:%M:%S")' mysql incremental backup Faild!'
last_day=$(date -d "1 days ago" +%Y-%m-%d)
today=$(date +%Y%m%d)
filename=$(find $backup_dir -name "$last_day*" -print|awk -F / '{print $NF}')
echo "Start to backup at $(date +%Y%m%d%H%M)"
if [ ! -d "$backup_dir" ];then
mkdir -p $backup_dir
fi
#innobackupex --defaults-file=$my_config --user=$user --password=$passwd --stream=tar $backup_dir 2>$backup_dir/$log | gzip 1>$backup_dir/$str
innobackupex --defaults-file=$my_config --user=$user --password=$passwd --incremental $backup_dir --incremental-basedir=$backup_dir/$filename
if [ $? -eq 0 ];then
echo "Backup is finish! at $(date +%Y%m%d%H%M)"
echo "Server_name:$(hostname) Server_ip:$local_ip $(date +"%y-%m-%d %H:%M:%S") mysql incremental backup Success!"
/usr/local/bin/sendEmail -f $source -t $target -s smtp.163.com -u $title -xu $MailUser -xp $MailPassword -m $content1
exit 0
else
echo "Backup is Fail! at $(date +%Y%m%d%H%M)"
echo "Server_name:$(hostname) Server_ip:$local_ip $(date +"%y-%m-%d %H:%M:%S") mysql incremental backup Fail!"
/usr/local/bin/sendEmail -f $source -t $target -s smtp.163.com -u $title -xu $MailUser -xp $MailPassword -m $content2
exit 1
fi
echo "Backup Process Done"
三:結合crontab來實現自動備份
0 0 * * 0 /download/xtrabackup_full.sh>> /download/xtrabackup_full.log 2>&1
0 0 * * 1-6 /download/xtrabackup_incremental.sh>>/download/xtrabackup_incremental.log 2>&1
--本篇文章參考自:http://www.aixchina.net/home/space.php?uid=408689&do=blog&id=147879