#!/bin/sh
#
# 使用方法:
# ./restore.sh /你備份文件的全路徑
#[email protected]
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 # 還原的時候使用的內存限制數
#############################################################################
#顯示錯誤
#############################################################################
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
# Prepare增量備份集
for i in `find $PARENT_DIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -n`; do
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
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`
`echo 'chown -R mysql:mysql' ${mysqldatadir:8}`
echo "3.權限修改成功!"
echo "*****************************"
#自動啟動mysql
echo -n "請輸入確認啟動mysql[1]:"
read var
echo "你輸入的是$var"
errorlog=`grep -i "^log-error" $MY_CNF`
if [ $var = 1 ] ; then
echo "mysql啟動中............"
`/usr/local/mysql/bin/mysqld_safe > /dev/null & `
sleep 10
PortNum=`netstat -lnt|grep 3306|wc -l`
if [ $PortNum = 1 ];
then
echo "4.mysql啟動成功"
echo "*****************************"
else
echo "4.mysql啟動失敗,請檢查錯誤日志$errorlog"
echo "*****************************************"
fi
exit 0
else
echo "輸入不正確!"
fi
END_RESTORE_TIME=`date +%F' '%T' '%w`
echo "數據庫還原完成於: $END_RESTORE_TIME"
exit 0