MySQL--简单mysqldump脚本

内容如下:

#!/bin/bash

DATE=`date '+%Y%m%d'`  
TIME=`date '+%H%M%S'`  

BAK_CMD="/data/mysql3306/bin/mysqldump -uroot -proot -hlocalhost -P3306 --default-character-set=utf8 --master-data=2 --single-transaction "
DB_NAME="shop_zp"
DIR_NAME="/data/mysqlbak/db_bak/table_bak/"


#选择备份数据库名  
printf "请输入备份数据库名,默认[${DB_NAME}],同时备份多个库格式[db_name1 db_name2]:\n"  
while read BAK_DB_NAME  
do   
     case ${BAK_DB_NAME} in   
        'Q'|'exit'|'q')  
            printf "退出备份程序,备份结束[`date '+%Y/%m/%d %H:%M:%S'`] ...  \n"  
            exit  
            ;;  
        *)  
         if [ "`echo ^${BAK_DB_NAME}`" = "^" ]; then  
           BAK_DB_NAME=${DB_NAME}  
          fi  
          break  
          ;;  
     esac  
done  
printf "(#提示:你输入备份数据库名是[${BAK_DB_NAME}].)\n"  


#输入备份的表名  
printf "请输入备份的表名,单表即表名,多表格式[table1 table2 table3],全库或多库备份请直接回车:\n"  
while read BAK_TABLE_NAME  
do   
     case ${BAK_TABLE_NAME} in   
        'Q'|'exit'|'q')  
            printf "退出备份程序,备份结束[`date '+%Y/%m/%d %H:%M:%S'`] ...  \n"  
            exit  
            ;;  
        *)  
         if [ "`echo ^${BAK_TABLE_NAME}`" = "^" ]; then  
           BAK_TABLE_NAME="ALL"  
          fi  
          break  
          ;;  
     esac  
done  
printf "(#提示:你输入备份的表名是[${BAK_TABLE_NAME}].)\n"  


#输入备份的目录
printf "输入备份的目录,默认[${DIR_NAME}],注意路径以"/"结束:\n" 
while read TMP  
do   
     case ${TMP} in   
        'Q'|'exit'|'q')  
            printf "退出备份程序,备份结束[`date '+%Y/%m/%d %H:%M:%S'`] ...  \n"    
            exit  
            ;;  
        *)  
         if [ "`echo ^${TMP}`" = "^" ]; then  
            TMP=${DIR_NAME} 
         fi  
         if [ ! -d ${TMP} ]; then   
            printf "备份目录[${TMP}]不存在... \n"  
            printf "请输入备份目录,默认[${DIR_NAME}]:\n"  
         else  
           BAK_DIR_NAME=${TMP}  
           break  
          fi  
          ;;  
     esac  
done  
unset TMP  
printf "(#提示:你输入备份目录是[${BAK_DIR_NAME}].)\n"  


#开始执行相应级别备份脚本  
T_NAME=`echo "${DATE}_${BAK_DB_NAME}@${BAK_TABLE_NAME}_${TIME}.sql"|sed "s/ /@/g"`

if [ ${BAK_TABLE_NAME} == ALL ];then
printf "\n开始备份[${BAK_DB_NAME}]数据库[`date '+%Y/%m/%d %H:%M:%S'`] ...  \n"  
${BAK_CMD} --databases ${BAK_DB_NAME} > ${BAK_DIR_NAME}${T_NAME}
else
printf "\n开始备份[${BAK_DB_NAME}]数据库[${BAK_TABLE_NAME}]表[`date '+%Y/%m/%d %H:%M:%S'`] ...  \n"  
${BAK_CMD} --databases ${BAK_DB_NAME} --tables ${BAK_TABLE_NAME} > ${BAK_DIR_NAME}${T_NAME}
fi

printf "备份完成,备份文件[${BAK_DIR_NAME}${T_NAME}] ...大小为[`du -k ${BAK_DIR_NAME}${T_NAME} | awk '{printf $1}'` Kbytes]\n" 



你可能感兴趣的:(MySQL,Script)