码迷,mamicode.com
首页 > 其他好文 > 详细

备份恢复脚本

时间:2019-08-06 18:26:33      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:The   div   tle   direct   incr   启动失败   pre   查错   star   

# cat my_full_backup.sh 
#!/bin/bash 
BEGINTIME=`date +"%Y-%m-%d %H:%M:%S"` 
format_time=`date +"%Y-%m-%d_%H:%M:%S"` 
week=`date +%w` 
port=3306 
ip=192.168.1.101 
backdir=/tmp/xtrabackup 
file_cnf=/app/mysqldata/3306/my.cnf 
user_name=xtrabk
password=onlybackup
out_log=$backdir/Xtrabackup_log_$format_time 
time_cost=$backdir/Xtrabackup_time.txt
mysql_path=/usr/bin

mkdir -p $backdir

#full 
if [ ! -d "$backdir/full" ];then
        echo "#####start full backup at $BEGINTIME to directory full" >>$time_cost
        ${mysql_path}/innobackupex --defaults-file=$file_cnf --no-timestamp --parallel=4 --throttle=200 --kill-long-queries-timeout=300 --kill-long-query-type=select --user=$user_name --password=$password   --host=$ip --port=$port  $backdir/full 1> $out_log 2>&1
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

 

# cat my_full_recover.sh 
#NOTE:恢复开始前请确保mysql服务停止以及数据和日志目录清空,如
rm -rf /app/mysqldata/3306/data/*
rm -rf /app/mysqldata/3306/binlog/*
rm -rf /app/mysqldata/3306/redolog/*
rm -rf /app/mysqldata/3306/tmp/*
rm -rf /app/mysqldata/3306/undolog/*
#rm -rf /app/mysqldata/3306/error.log /app/mysqldata/3306/slow-queries.log

INNOBACKUPEX=innobackupex
INNOBACKUPEX_PATH=/usr/bin/$INNOBACKUPEX
TMP_LOG="/tmp/restore.$$.log"
MY_CNF=/etc/my.cnf
BACKUP_DIR=/tmp/xtrabackup # 你的备份主目录
FULLBACKUP_DIR=$BACKUP_DIR/full # 全库备份的目录
INCRBACKUP_DIR=$BACKUP_DIR/incre # 增量备份的目录
MEMORY=2048M # 还原的时候使用的内存限制数
ERRORLOG=`grep -i "^log-error" $MY_CNF |cut -d = -f 2`
MYSQLD_SAFE=/usr/local/mysql/bin/mysqld_safe
MYSQL_PORT=3306


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>/tmp/$logfiledate  
    rm -f $TMP_LOG
    exit 1
  fi
}


echo "*****************************"
echo "Prepare:使用完整备份进行恢复..........."
echo "*****************************"
$INNOBACKUPEX_PATH --defaults-file=$MY_CNF --apply-log --redo-only --use-memory=$MEMORY $FULLBACKUP_DIR > $TMP_LOG 2>&1
check_innobackupex_fail


echo "*****************************"
echo "Copyback:数据库还原中 ...请稍等"
echo "*****************************"
$INNOBACKUPEX_PATH --defaults-file=$MY_CNF --copy-back $FULLBACKUP_DIR > $TMP_LOG 2>&1
chown -R mysql:mysql /app/mysqldata/3306
check_innobackupex_fail

rm -f $TMP_LOG
echo "1.恭喜,还原成功!."
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 10 ]
      do
        PORTNUM=`netstat -lnt|grep ${MYSQL_PORT}|wc -l`
        echo "mysql启动中....请稍等..."
        sleep 10
        if [ $PORTNUM = 1  ];
        then
             echo -e "\e[32m mysql****启动成功**** \e[m"
        exit 0
        fi  
        INIT_NUM=$(($INIT_NUM +1))
      done
echo -e "\e[31m mysql启动失败或启动时间过长,请检查错误日志 `echo ‘cat ‘ ${ERRORLOG}` \e[m"
echo "*****************************************"
exit 0
fi


END_RESTORE_TIME=`date +%F %T %w`
echo "数据库还原完成于: $END_RESTORE_TIME"
exit 0

 

备份恢复脚本

标签:The   div   tle   direct   incr   启动失败   pre   查错   star   

原文地址:https://www.cnblogs.com/allenhu320/p/11310805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!