mysql自动备份的脚本

发布时间:2019-12-01编辑:脚本学堂
mysql自动备份的脚本,可以输入参数以实现完整备份或增量备份,供大家学习参考。

mysql自动备份的脚本,可以输入参数以实现完整备份或增量备份,供大家学习参考。

复制代码 代码如下:

#!/bin/bash
echo -e "33[31m please + f or d or i 33[0m"

FULLBK () {
  mysqldump --triggers --lock-all-tables --all-databases >/root/`date "+%Y:%m:%d--%H:%M:%S"`.sql
  if [ $? == 0 ];then
      tar jcf  /root/`date "+%Y:%m:%d--%H:%M:%S"`.tar.bz2 /root/*.sql
      echo -e "33[032m The backup is success....33[0m"
         read -p "DO you want delete source file,y is "yes",other is "no": " A
         case $A in
         y|Y)
             rm -rf /root/*.sql
             echo -e "delete success....."
         ;;
         *)
              echo -e "you do nothing"
              return 2
        ;;
        esac
    else
      echo -e "33[031m The backup is Failure....33[0m"
  fi
}

#------------------
ZLBK (){
 ZB='mysql -e "SHOW MASTER STATUS" |grep mysql-bin |cut -f1'
   if false;then
    mysql -e "FLUSH LOGS;"
    mysql -e   "FLUSH TABLES WITH READ LOCK;"
#      read -p "please input you sql's data postion: " PT
    cp /data/mydata/$ZB /root/zback/
    tar jcf /root/`date "+%Y:%m:%d--%H:%M:%S"`.tar.bz2 /root/$ZB && echo "Failure..."
    mysql -e "UNLOCK TABLES;"
      else
            CYBK
   fi
}

#-------------------
CYBK () {
   rm -rf /root/cyback && mkdir /root/cyback
    mysql -e "FLUSH LOGS;"
    mysql -e "FLUSH TABLES WITH READ LOCK;"
# read -p "please input you sql's data postion: " PT
    cp /data/mydata/mysql-bin* /root/cyback/
#    tar jcf /root/cyback/* /root/cyback/`date "+%Y:%m:%d--%H:%M:%S"`.tar.bz2
    cd /root/cyback/
   tar jcf /mnt/`date "+%Y:%m:%d--%H:%M:%S"`.tar.bz2 ./*
     REL=$?
    rm -rf /root/cyback/mysql-bin*
    mysql -e "UNLOCK TABLES;"   
}


# read -p "please input you sql's data postion: " PT
#-------------------------
while getopts ":fid:" OPTS; do
 case $OPTS in
   f)
        FULLBK
   ;;
   d)
        ZLBK
   ;;
   i)   
        CYBK
   ;;
    *)
          echo "nothing"
  ;;
  esac
done