優勢:熱備份,不影響業務,增量備份,遠程備份。mysql
目的:自動打包備份到遠程10.0.0.111備份服務器sql
好用幫點贊!謝謝小夥伴的支持。bash
前提:服務器
1.安裝xtrabackupssh
yum install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpmide
yum install percona-xtrabackup-22orm
2.建立目錄rem
mkdir -p /data/backupit
mkdir -p /data/backuptar/ast
3.添加mysql公鑰到備份服務器(10.0.0.111),能免密碼遠程登陸。
4.建立備份用戶
GRANT RELOAD,LOCK TABLES,REPLICATION CLIENT ON *.* TO 'bkuser'@'localhost' IDENTIFIED BY 'passwd';
FLUSH PRIVILEGES;
------------------------
腳本:
#!/bin/bash
backdir=/data/backup
backupbin=/usr/bin
begintime=`date +"%Y-%m-%d %H:%M:%S"`
format_time=`date +"%Y-%m-%d_%H_%M_%S"`
time_cost=$backdir/xtrabackup_time.txt
week=`date +%w`
user_name=bkuser
password="passwd"
file_cnf=/etc/my.cnf.d/wsrep.cnf
backtar=/data/backuptar/
remote_host="10.0.0.111"
remote_dir=/data/mysqlbak_remote_23
remote_user="wanzi"
out_log=$backdir/xtrabackup_log_$format_time
if [ -d "$backdir/rec5" ];then
echo "開始打包5次的備份"
cd $backdir
tar -zcf lastweek.gz ./*
mv lastweek.gz $backtar/$format_time.lastweek.gz
echo "完成5次備份打包"
cd $backtar
rm -rf `ls |grep -v "$format_time.lastweek.gz"`
echo "刪除本地上次備份完成"
echo "開始刪除遠程備份....."
ssh $remote_user@$remote_host "cd $remote_dir;rm -rf ./*"
echo "刪除遠程多餘備份完成"
echo "開始拷貝本地備份到遠程服務器..."
scp $backtar/$format_time.lastweek.gz $remote_host:/$remote_dir
echo "遠程備份完成"
rm -rf $backdir
mkdir $backdir
fi
#fullbackup
if [ ! -d "$backdir/fullbackup" ];then
echo "在$begintime開始全量備份" >>$time_cost
$backupbin/innobackupex --defaults-file=$file_cnf --user=$user_name --password=$password --no-timestamp --slave-info $backdir/fullbackup 1>$out_log 2>&1
echo "完成全備"
elif [ ! -d "$backdir/rec0" ];then
echo "#####start 0 incremental backup at $BEGINTIME to directory rec0" >>$time_cost
$backupbin/innobackupex --defaults-file=$file_cnf --no-timestamp --user=$user_name --password=$password --slave-info --incremental --incremental-basedir=$bac
kdir/fullbackup $backdir/rec0 1> $out_log 2>&1
echo "完成第0次增量備份"
elif [ ! -d "$backdir/rec1" ];then
echo "#####start 1 incremental backup at $BEGINTIME to directory rec1" >>$time_cost
$backupbin/innobackupex --defaults-file=$file_cnf --no-timestamp --user=$user_name --password=$password --slave-info --incremental --incremental-basedir=$bac
kdir/rec0 $backdir/rec1 1> $out_log 2>&1
echo "完成第1次增量備份"
elif [ ! -d "$backdir/rec2" ];then
echo "#####start 2 incremental backup at $BEGINTIME to directory rec2" >>$time_cost
$backupbin/innobackupex --defaults-file=$file_cnf --no-timestamp --user=$user_name --password=$password --slave-info --incremental --incremental-basedir=$bac
kdir/rec1 $backdir/rec2 1> $out_log 2>&1
echo "完成第2次增量備份"
elif [ ! -d "$backdir/rec3" ];then
echo "#####start 3 incremental backup at $BEGINTIME to directory rec3" >>$time_cost
$backupbin/innobackupex --defaults-file=$file_cnf --no-timestamp --user=$user_name --password=$password --slave-info --incremental --incremental-basedir=$bac
kdir/rec2 $backdir/rec3 1> $out_log 2>&1
echo "完成第3次增量備份"
elif [ ! -d "$backdir/rec4" ];then
echo "#####start 4 incremental backup at $BEGINTIME to directory rec4" >>$time_cost
$backupbin/innobackupex --defaults-file=$file_cnf --no-timestamp --user=$user_name --password=$password --slave-info --incremental --incremental-basedir=$bac
kdir/rec3 $backdir/rec4 1> $out_log 2>&1
echo "完成第4次增量備份"
elif [ ! -d "$backdir/rec5" ];then
echo "#####start 5 incremental backup at $BEGINTIME to directory rec5" >>$time_cost
$backupbin/innobackupex --defaults-file=$file_cnf --no-timestamp --user=$user_name --password=$password --slave-info --incremental --incremental-basedir=$bac
kdir/rec4 $backdir/rec5 1> $out_log 2>&1
echo "完成第5次增量備份"
fi
ENDTIME=`date +"%Y-%m-%d %H:%M:%S"`
begin_data=`date -d "$BEGINTIME" +%s`
end_data=`date -d "$ENDTIME" +%s`
spendtime=`expr $end_data - $begin_data`
echo "it takes $spendtime sec for packing the data directory" >>$time_cost
echo "備份結束......"`date`