备份恢复脚本
Posted allenhu320
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了备份恢复脚本相关的知识,希望对你有一定的参考价值。
# 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
以上是关于备份恢复脚本的主要内容,如果未能解决你的问题,请参考以下文章