指令碼再次更新,共用一下!
#!/bin/sh
#
# 使用方法:
# ./restore.sh /你備份檔案的全路徑
#ocpyang@126.com
INNOBACKUPEX=innobackupex
INNOBACKUPEX_PATH=/usr/bin/$INNOBACKUPEX
TMP_LOG="/var/log/restore.$$.log"
MY_CNF=/usr/local/mysql/my.cnf
BACKUP_DIR=/backup # 你的備份主目錄
FULLBACKUP_DIR=$BACKUP_DIR/full # 全庫備份的目錄
INCRBACKUP_DIR=$BACKUP_DIR/incre # 增量備份的目錄
MEMORY=4096M # 還原的時候使用的記憶體限制數
ERRORLOG=`grep -i "^log-error" $MY_CNF |cut -d = -f 2`
MYSQLD_SAFE=/usr/local/mysql/bin/mysqld_safe
MYSQL_PORT=3306
#############################################################################
#顯示錯誤
#############################################################################
error()
{
echo "$1" 1>&2
exit 1
}
#############################################################################
# 檢查innobackupex錯誤輸出
#############################################################################
check_innobackupex_fail()
{
if [ -z "`tail -2 $TMP_LOG | grep 'completed OK!'`" ] ; then
echo "$INNOBACKUPEX命令執行失敗:"; echo
echo "---------- $INNOBACKUPEX的錯誤輸出 ----------"
cat $TMP_LOG
#保留一份備份的詳細日誌
logfiledate=restore.`date +%Y%m%d%H%M`.txt
cat $TMP_LOG>/backup/$logfiledate
rm -f $TMP_LOG
exit 1
fi
}
# 選項檢測
if [ ! -x $INNOBACKUPEX_PATH ]; then
error "$INNOBACKUPEX_PATH在指定路徑不存在,請確認是否安裝或核實連結是否正確."
fi
if [ ! -d $BACKUP_DIR ]; then
error "備份目錄$BACKUP_DIR不存在."
fi
if [ $# != 1 ] ; then
error "使用方法: $0 使用還原目錄的絕對路徑"
fi
if [ ! -d $1 ]; then
error "還原到:$1不存在."
fi
# Some info output
echo "----------------------------"
echo
echo "$0: MySQL還原指令碼"
START_RESTORE_TIME=`date +%F' '%T' '%w`
echo "資料庫還原開始於: $START_RESTORE_TIME"
echo
PARENT_DIR=`dirname $1`
if [ $PARENT_DIR = $FULLBACKUP_DIR ]; then
FULLBACKUP=$1
echo "還原`basename $FULLBACKUP`"
echo
else
if [ `dirname $PARENT_DIR` = $INCRBACKUP_DIR ]; then
INCR=`basename $1`
FULL=`basename $PARENT_DIR`
FULLBACKUP=$FULLBACKUP_DIR/$FULL
if [ ! -d $FULLBACKUP ]; then
error "全備:$FULLBACKUP不存在."
fi
echo "還原$FULL到增量$INCR"
echo
echo "Prepare完整備份集..........."
echo "*****************************"
$INNOBACKUPEX_PATH --defaults-file=$MY_CNF --apply-log --redo-only --use-memory=$MEMORY $FULLBACKUP > $TMP_LOG 2>&1
check_innobackupex_fail
#判斷lsn
check_incre_file=`find $PARENT_DIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n " | sort -nr |grep -v '^$' | head -n 1`
check_incre_lastlsn=${PARENT_DIR}/${check_incre_file}/xtrabackup_checkpoints
fetch_incre_lastlsn=`grep -i "^last_lsn" ${check_incre_lastlsn} |cut -d = -f 2`
check_full_file=`find $FULLBACKUP/ -mindepth 1 -maxdepth 1 -type d -printf "%P\n " | sort -nr |grep -v '^$' | head -n 1`
check_full_lastlsn=$FULLBACKUP/${check_full_file}/xtrabackup_checkpoints
fetch_full_lastlsn=`grep -i "^last_lsn" ${check_incre_lastlsn} |cut -d = -f 2`
# Prepare增量備份集,即將增量備份應用到全備目錄中
for i in `find $PARENT_DIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -n`;
do
######判斷LSN
if [ "${fetch_incre_lastlsn}"="${fetch_full_lastlsn}" ];then
echo "*****************************************"
echo "LSN不需要prepare!"
echo "*****************************************"
echo
break
else
######判斷LSN
echo "Prepare增量備份集$i........"
echo "*****************************"
$INNOBACKUPEX_PATH --defaults-file=$MY_CNF --apply-log --redo-only --use-memory=$MEMORY $FULLBACKUP --incremental-dir=$PARENT_DIR/$i > $TMP_LOG 2>&1
check_innobackupex_fail
if [ $INCR = $i ]; then
break
fi
fi
######判斷LSN
done
else
error "未知的備份類型"
fi
fi
echo "prepare全備集,復原那些未提交的事務..........."
$INNOBACKUPEX_PATH --defaults-file=$MY_CNF --apply-log --use-memory=$MEMORY $FULLBACKUP > $TMP_LOG 2>&1
check_innobackupex_fail
echo "*****************************"
echo "1.資料庫還原中 ...請稍等"
echo "*****************************"
$INNOBACKUPEX_PATH --defaults-file=$MY_CNF --copy-back $FULLBACKUP > $TMP_LOG 2>&1
check_innobackupex_fail
rm -f $TMP_LOG
echo "2.恭喜,還原成功!."
echo "*****************************"
#修改目錄許可權
echo "修改mysql目錄的許可權."
mysqlcnf="/usr/local/mysql/my.cnf"
mysqldatadir=`grep -i "^basedir" $mysqlcnf |cut -d = -f 2`
`echo 'chown -R mysql:mysql' ${mysqldatadir}`
echo "3.許可權修改成功!"
echo "*****************************"
#自動啟動mysql
INIT_NUM=1
if [ ! -x $MYSQLD_SAFE ]; then
echo "mysql安裝時開機檔案未安裝到$MYSQLD_SAFE或無執行許可權"
exit 1 #0是執行成功,1是執行不成功
else
echo "啟動本機mysql連接埠為:$MYSQL_PORT的服務"
$MYSQLD_SAFE --defaults-file=$MY_CNF > /dev/null &
while [ $INIT_NUM -le 6 ]
do
PORTNUM=`netstat -lnt|grep ${MYSQL_PORT}|wc -l`
echo "mysql啟動中....請稍等..."
sleep 5
if [ $PORTNUM = 1 ];
then
echo "mysql ****啟動成功****"
exit 0
fi
INIT_NUM=$(($INIT_NUM +1))
done
echo -e "mysql啟動失敗或啟動時間過長,請檢查錯誤記錄檔`echo 'cat ' ${ERRORLOG}`"
echo "*****************************************"
exit 0
fi
END_RESTORE_TIME=`date +%F' '%T' '%w`
echo "資料庫還原完成於: $END_RESTORE_TIME"
exit 0