Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1365933
  • 博文数量: 243
  • 博客积分: 888
  • 博客等级: 准尉
  • 技术积分: 2955
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-05 14:33
个人简介

漫漫长路,其修远兮!

文章分类

全部博文(243)

文章存档

2017年(2)

2016年(22)

2015年(32)

2014年(57)

2013年(107)

2012年(23)

分类: Mysql/postgreSQL

2013-04-18 15:23:33

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

 

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