You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
3.8 KiB
86 lines
3.8 KiB
# !/bin/bash
|
|
source ./mysql_config.sh
|
|
|
|
#获取binlog文件
|
|
parse_binlog(){
|
|
#########查找 BINLOG POSITION############
|
|
BINLOG=`head -n 50 $1 |grep "CHANGE MASTER TO MASTER_LOG_FILE"|awk -F "'" '{print $2}'`
|
|
BINLOGPOS=`head -n 50 $1 |grep "CHANGE MASTER TO MASTER_LOG_FILE"|awk -F "=" '{print $3}'|sed 's/;//'`
|
|
echo "$1 $DATABASE $BINLOG $BINLOGPOS"
|
|
}
|
|
#导入备份好的数据
|
|
recover_bybak(){
|
|
####先按备份数据恢复 FULLBAKUP RECOVERY#############
|
|
echo "#`date +'%Y-%m-%d %H:%M:%S'`:1 从备份数据库 $1 恢复 " >> ${RECOVERY_LOG}
|
|
echo "$MYSQL -h$MYSQLIP -P$MYSQLPORT -u$USER -p$PASS -e 'set session sql_log_bin=0;source $1;' >> ${RECOVERY_LOG} 2>&1 " >> ${RECOVERY_LOG}
|
|
|
|
if $MYSQL -h$MYSQLIP -P$MYSQLPORT -u$USER -p$PASS -e "set session sql_log_bin=0;source $1;" >> ${RECOVERY_LOG} 2>&1;then
|
|
echo "================>导入备份数据成功"
|
|
echo "recovery $DATABASE success" >> ${RECOVERY_LOG}
|
|
else
|
|
echo "================>导入备份数据失败"
|
|
echo "recovery $DATABASE fail" >> ${RECOVERY_LOG}
|
|
exit;
|
|
fi
|
|
}
|
|
#通过备份数据的start-position和参数 stop-datetime 指定恢复区间
|
|
recover_bybinlog(){
|
|
echo "#`date +'%Y-%m-%d %H:%M:%S'`: 2 从BingLog 文件 $BINLOG 开始位置 $BINLOGPOS 恢复" >> ${RECOVERY_LOG}
|
|
#####后续的通过binglog恢复##########
|
|
cd $BINLOGDIR
|
|
find . -type f -name "*binlog.[0-9]*" |xargs -i basename {} > $TMPDIR/binlog.tmp
|
|
sort -n $TMPDIR/binlog.tmp > $TMPDIR/binlog.tmp2
|
|
sed '/md5/d' $TMPDIR/binlog.tmp2 > $TMPDIR/binlog.tmp3
|
|
sed -n "/$BINLOG/,\$p" $TMPDIR/binlog.tmp3 > $TMPDIR/binlog.log
|
|
RECBINLOGS=`awk '{printf "%s"," "$1}' $TMPDIR/binlog.log `
|
|
echo "待恢复的binlog $RECBINLOGS endTime $2 $3"
|
|
if [ ! $2 ]; then
|
|
echo " $MYSQLBINLOG --no-defaults --disable-log-bin --start-position=$BINLOGPOS $RECBINLOGS -d $DATABASE |$MYSQL -h$MYSQLIP -P$MYSQLPORT -u$USER -p$PASS -f " >> ${RECOVERY_LOG}
|
|
$MYSQLBINLOG --no-defaults --disable-log-bin --start-position=$BINLOGPOS $RECBINLOGS -d $DATABASE | $MYSQL -h$MYSQLIP -P$MYSQLPORT -u$USER -p$PASS -f >> ${RECOVERY_LOG} 2>&1
|
|
else
|
|
echo " $MYSQLBINLOG --no-defaults --disable-log-bin --start-position=$BINLOGPOS --stop-datetime=$2 $3 $RECBINLOGS -d $DATABASE |$MYSQL -h$MYSQLIP -P$MYSQLPORT -u$USER -p$PASS -f " >> ${RECOVERY_LOG}
|
|
$MYSQLBINLOG --no-defaults --disable-log-bin --start-position=$BINLOGPOS --stop-datetime="$2 $3" $RECBINLOGS -d $DATABASE | $MYSQL -h$MYSQLIP -P$MYSQLPORT -u$USER -p$PASS -f >> ${RECOVERY_LOG} 2>&1
|
|
fi
|
|
|
|
if [ $? -eq 0 ];then
|
|
echo "================>BinLog恢复成功"
|
|
echo "recover $DATABASE binlog success" >> ${RECOVERY_LOG}
|
|
else
|
|
echo "================>BinLog恢复失败"
|
|
echo "recover $DATABASE binlog fail" >> ${RECOVERY_LOG}
|
|
exit;
|
|
fi
|
|
}
|
|
recover_mysql(){
|
|
echo "#`date +'%Y-%m-%d %H:%M:%S'`: start recover $DATABASE from $1 and BinLog" >> ${RECOVERY_LOG}
|
|
if [ ! -f $1 ];then
|
|
echo "$1 文件未找到"
|
|
echo "#`date +'%Y-%m-%d %H:%M:%S'`: 文件未找到 $1" >> ${RECOVERY_LOG}
|
|
exit
|
|
fi
|
|
if [ ! -d $BINLOGDIR ];then
|
|
echo "$BINLOGDIR 未找到binlog"
|
|
exit
|
|
fi
|
|
parse_binlog $1
|
|
if [ ! -f $BINLOGDIR/$BINLOG ];then
|
|
echo "$BINLOGDIR/$BINLOG 文件未找到"
|
|
exit
|
|
fi
|
|
recover_bybak $1
|
|
|
|
recover_bybinlog $1 $2 $3
|
|
|
|
echo "#`date +'%Y-%m-%d %H:%M:%S'`:end recover $DATABASE" >> ${RECOVERY_LOG}
|
|
}
|
|
|
|
#操作
|
|
if [[ $# -ge 1 ]] ; then
|
|
echo "***********************************************************" >> ${RECOVERY_LOG}
|
|
echo "================>从备份 $1 + BinLog 恢复数据"
|
|
DATABASESQL=`echo $1 | awk -F"-" '{print $2}'`
|
|
DATABASE=`echo $DATABASESQL | awk -F"." '{print $1}' `
|
|
recover_mysql $1 $2 $3
|
|
else
|
|
echo "USEARG mysql_recover.sh [数据库备份文件路径] [stoptime 2013-11-29 13:21:53])"
|
|
fi
|