通过shell脚本自动增加mysql分区表的分区

来源:互联网 发布:数控车工编程 编辑:程序博客网 时间:2024/05/22 12:18

http://blog.chinaunix.net/uid-411974-id-88397.html


  1. #!/bin/sh
  2. #
  3. #
  4. :<<BLOCK
  5. ######################################################################
  6. SHELL_NAME:Logdb_Add_Partition.sh
  7.   Functional Description:At the last month auto add the logdb table partition
  8.     Argument:

  9.             $1 USER Mysql Account
  10.             $2 PASS Mysql Account Pass
  11.             $3 DB        Mysql Logdb
  12.     Version:V1.0
  13.     Creater : SongYunkui
  14.               Colin_Song
  15.     Crete_time:2010/12/9
  16.     Modify:1. MODIFY BY
  17.             2. ADD BY ____ ___/__/__ Add:_________
  18. ######################################################################            
  19. BLOCK

  20. #######################################################################################
  21. if [ $# -lt 3 ]; then
  22.   echo "Please Input The Correct Args"
  23.   echo "Usage Logdb_Add_Partition.sh <USER> <PASS> <DB>"
  24.   exit -1
  25. fi

  26. USER=$1
  27. PASS=$2
  28. DB=$3

  29. ##config section begin
  30. CONN_MYSQL="-u$USER -p$PASS -s"
  31. MYSQL_HOME=/opt/modules/mysql
  32. MYSQL_DIR=${MYSQL_HOME}/bin/mysql
  33. SHELL_BASE=/opt/sbin/Logdb
  34. LOG_DIR=${SHELL_BASE}/log
  35. OPT_NAME=add_partition

  36. MKDIR=`whereis-b mkdir|awk'{print $2}'`
  37. TOUCH=`whereis-b touch|awk'{print $2}'`
  38. DATE=`whereis-b date|awk'{print $2}'`
  39. if [ ! -d ${SHELL_BASE}]
  40. then
  41.     ${MKDIR}-p ${SHELL_BASE}
  42. fi

  43. if [ ! -d ${LOG_DIR}]
  44. then
  45.     ${MKDIR}-p ${LOG_DIR}
  46. fi

  47. if [ ! -d ${INI_DIR}]
  48. then
  49.     ${MKDIR}-p ${INI_DIR}
  50. fi

  51. LOG_FILE=${LOG_DIR}/${OPT_NAME}.log

  52. #config section end

  53. #working start
  54. CURRENT_DATE=`${DATE}+'%Y-%m-%d'`
  55. echo "${CURRENT_DATE} everything is ok, runing start">> ${LOG_FILE}

  56. #loop read the partition table and column
  57. while read TAB_NAME COL_NAME
  58. do

  59. COUNTER=1
  60. CURRENT_YEAR=`date+%Y`
  61. #check the next month
  62. NEXT_MONTH=`date-d next-month+%m`

  63. #check the next month has many days
  64. case ${NEXT_MONTH} in
  65.             1|01|3|03|5|05|7|07|8|08|10|12)
  66.                                            CURRENT_DAY=31
  67.             ;;
  68.             4|04|6|06|9|09|11)
  69.                               CURRENT_DAY=30
  70.             ;;
  71.             2|02)
  72.                 if [ `expr ${CURRENT_YEAR}% 4` -eq 0]; then
  73.                         if [ `expr ${CURRENT_YEAR}% 400` -eq 0]; then
  74.                                 CURRENT_DAY=29
  75.                         elif [ `expr ${CURRENT_YEAR}% 100` -eq 0]; then
  76.                                 CURRENT_DAY=28
  77.                         else
  78.                                 CURRENT_DAY=29
  79.                         fi
  80.                 else
  81.                         CURRENT_DAY=28
  82.                 fi
  83.              ;;
  84. esac

  85. #work start add the every day partition
  86. while [${COUNTER}-le ${CURRENT_DAY}]

  87. do

  88. #calculate the current day's next {counter} day
  89. PATNAME_DATE=`date-d "${COUNTER} days"+%Y%m%d`
  90. COUNTER=`expr${COUNTER}+ 1`
  91. PAT_DATE=`date-d "${COUNTER} days"+%Y%m%d`

  92. #change the unix_timestamp
  93. PAT_UNIX_TIMESTAMP=`${MYSQL_DIR}${CONN_MYSQL}<<EOF
  94. use ${DB};
  95. select UNIX_TIMESTAMP('${PAT_DATE}');
  96. EOF`

  97. ##add partition sql
  98. V_SQL="ALTER TABLE ${DB}."${TAB_NAME}" ADD PARTITION (PARTITION P"${PATNAME_DATE}" VALUES LESS THAN ("${PAT_UNIX_TIMESTAMP}"));"
  99. echo $V_SQL

  100. #exec the sql
  101. ${MYSQL_DIR}${CONN_MYSQL}<<EOF
  102. use ${DB};
  103. $V_SQL;
  104. EOF

  105. done
  106. done<./Logdb_Partition.ini

  107. #working end
  108. END_DATE=`${DATE}+'%Y-%m-%d %H:%M:%S'`
  109. echo "${END_DATE} runing finished" >> ${LOG_FILE}
  110. echo -e "\n--------------------------------------------------------------------">> ${LOG_FILE}

原创粉丝点击