Chinaunix首页 | 论坛 | 博客
  • 博客访问: 112635
  • 博文数量: 38
  • 博客积分: 330
  • 博客等级: 一等列兵
  • 技术积分: 270
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-08 23:30
文章分类

全部博文(38)

文章存档

2012年(36)

2011年(2)

我的朋友

分类:

2012-03-31 10:25:26

注意:
1、数据库全备的命名规则 bak_dbname_xxx_sql
2、BAKUPDIR 存放所有的数据全备的文件的目录
3、BINLOGBAKDIR 用与存放所有用到的binlog的目录

点击(此处)折叠或打开

  1. #!/bin/bash
  2. BASEDIR=/data1/xxx/xxx/backuptest
  3. BAKUPDIR=$BASEDIR/tmpbak
  4. #BAKUPDIR=$BASEDIR/2012-03-27
  5. BINLOGBAKDIR=$BASEDIR/binlog
  6. LOGDIR=$BASEDIR/log
  7. RECOVERY_LOG=$LOGDIR/recovery.log
  8. MYSQL=/usr/local/mysql/bin/mysql
  9. MYSQLBINLOG=/usr/local/mysql/bin/mysqlbinlog
  10. USER=root
  11. PASS=123
  12. MYSQLIP=localhost

  13. mkdir -p $LOGDIR
  14. BAKUPSQL=`find $BAKUPDIR -type f -name "*sql" |xargs -i basename {}`

  15. for i in $BAKUPSQL
  16. do
  17.         cd $BAKUPDIR

  18. #########CHECK BINLOG POSITION############
  19.         BINLOG=`head -n 50 $i |grep "CHANGE MASTER TO MASTER_LOG_FILE"|awk -F "'" '{print $2}'`
  20.         BINLOGPOS=`head -n 50 $i |grep "CHANGE MASTER TO MASTER_LOG_FILE"|awk -F "=" '{print $3}'|sed 's/;//'`
  21.         DATABASE=`echo $i|awk -F"_" '{print $2}'`
  22. # echo $i $DATABASE $BINLOG $BINLOGPOS

  23. ####BEGINE FULLBAKUP RECOVERY#############
  24.         echo "#`date +'%Y-%m-%d %H:%M:%S'`: start recover $DATABASE " >> ${RECOVERY_LOG}
  25.         if $MYSQL -h$MYSQLIP -u$USER -p$PASS < $i >> ${RECOVERY_LOG} 2>&1;then
  26.                 echo "recovery $DATABASE sucessfully" >> ${RECOVERY_LOG}
  27.         else
  28.                 echo "recovery $DATABASE fail" >> ${RECOVERY_LOG}
  29.                 exit;
  30.         fi
  31.         echo "#`date +'%Y-%m-%d %H:%M:%S'`: end recover $DATABASE" >> ${RECOVERY_LOG}


  32. #####BEGINE BINLOGBAKUP RECOVERY##########

  33.         cd $BINLOGBAKDIR
  34.         find . -type f -name "*bin*" |xargs -i basename {} > /tmp/binlog.tmp
  35.         sort -n /tmp/binlog.tmp > $LOGDIR/binlog.tmp2
  36.         sed '/md5/d' $LOGDIR/binlog.tmp2 > $LOGDIR/binlog.tmp3
  37.         sed -n "/$BINLOG/,\$p" $LOGDIR/binlog.tmp3 > $LOGDIR/binlog${DATABASE}.log
  38.         RECBINLOGS=`awk '{printf "%s"," "$1}' $LOGDIR/binlog${DATABASE}.log `
  39.         #echo $DATABASE $BINLOGPOS $RECBINLOGS

  40.         echo "#`date +'%Y-%m-%d %H:%M:%S'`: start recover $DATABASE binlog " >> ${RECOVERY_LOG}
  41.         echo " $MYSQLBINLOG --no-defaults --disable-log-bin --start-position=$BINLOGPOS $RECBINLOGS -d $DATABASE |$MYSQL -h$MYSQLIP -u$USER -p$PASS -f " >> ${RECOVERY_LOG}
  42.         if $MYSQLBINLOG --no-defaults --disable-log-bin --start-position=$BINLOGPOS $RECBINLOGS -d $DATABASE |$MYSQL -h$MYSQLIP -u$USER -p$PASS -f >> ${RECOVERY_LOG} 2>&1;then
  43.                 echo "recover $DATABASE binlog sucessfully" >> ${RECOVERY_LOG}
  44.         else
  45.                 echo "recover $DATABASE binlog fail" >> ${RECOVERY_LOG}
  46.                 exit;
  47.         fi
  48.         echo "#`date +'%Y-%m-%d %H:%M:%S'`:end recover $DATABASE binlog" >> ${RECOVERY_LOG}


  49. done

阅读(783) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~