percona xtrabackupd定期做全備,增量備份shell指令碼

來源:互聯網
上載者:User

備份策略:

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


一:全備指令碼

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.