Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2013425
  • 博文数量: 148
  • 博客积分: 7697
  • 博客等级: 少将
  • 技术积分: 3071
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-10 23:04
个人简介

MiBDP,数据开发、项目团队、数据应用和产品在路上,金融保险、互联网网游、电商、新零售行业、大数据和AI在路上。对数仓、模型、ETL、数据产品应用了解。DTCC 2013演讲嘉宾,曾做过两款大获好评的数据产品平台。知识星球ID:35863277

文章分类
文章存档

2020年(1)

2019年(2)

2017年(2)

2016年(5)

2015年(1)

2014年(1)

2013年(6)

2012年(5)

2011年(24)

2010年(28)

2009年(1)

2008年(6)

2007年(30)

2006年(36)

分类: Mysql/postgreSQL

2010-12-09 16:18:48

  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 "
  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}
阅读(6118) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~