percona xtrabackupd定期做全备,增量备份shell脚本

​备份策略:

每周日做一全备,每天做一增量备份。


一:全备脚本

cat xtrabackup_full.sh

#!/bin/bash  

local_ip="$(/sbin/ifconfig eth0|grep 'inet addr'|awk -F : '{print $2}'|cut -d ' ' -f1)" 

email='[email protected]

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='[email protected]'

target='[email protected]'

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='[email protected]'

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='[email protected]'

target='[email protected]'

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

你可能感兴趣的:(percona xtrabackupd定期做全备,增量备份shell脚本)