Chinaunix首页 | 论坛 | 博客
  • 博客访问: 276106
  • 博文数量: 103
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 705
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-02 16:15
文章分类

全部博文(103)

文章存档

2014年(8)

2013年(95)

我的朋友

分类: Mysql/postgreSQL

2013-09-03 15:41:23

注意:
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

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