#!/bin/bash # ./partition_create.sh DAYS START_DATE TABLE [EXECUTE], for example # ./partition_create.sh 10 today calls EXECUTE # ./partition_create.sh 20 2020-02-20 calls_old EXECUTE # DAYS - number of days in the future from DATE to create partitions # takes into accound already created partitions and skips them # DATE - keyword today or date in format 20xx-xx-xx # TABLE - calls or calls_old # EXECUTE - optional parameter, if NOT present partition SQLs will be generated but not imported . /usr/src/m2/framework/bash_functions.sh if [ "$#" -lt 3 ]; then report "Usage: $0 DAYS today|20xx-xx-xx calls|calls_old [EXECUTE]" 1 exit 1 fi if ! [[ $1 =~ ^[0-9]+$ ]]; then report "Wrong parameter $1. It should be a number" 1 exit 1 fi if ! [[ $2 == "today" || $2 =~ ^20[0-9][0-9]-[0-9]{2}-[0-9]{2}$ ]]; then report "Wrong parameter $2. It should be a either keyword today or date in format 20xx-xx-xx" 1 exit 1 fi if ! [[ $3 == "calls" || $3 == "calls_old" ]]; then report "Wrong parameter $3. It should be keyqord calls or calls_old" 1 exit 1 fi DAYS="$1" START_DATE="$2" TABLE="$3" EXECUTE="0" if [[ -n "$4" && $4 == "EXECUTE" ]]; then EXECUTE="1"; fi mkdir -p /tmp/partition_managment existing_partitions_dates="/tmp/partition_managment/partitions_dates$$.txt" partitions_sql="/tmp/partition_managment/partitions_to_create$$.sql" rm -f "$existing_partitions_dates" rm -f "$partitions_sql" cleanup() { result=$? rm -f $existing_partitions_dates rm -f $partitions_sql exit ${result} } trap cleanup EXIT INT print_empty_partitions_info() { report "To use script make sure that $TABLE structure contains future partition and at least one partition with date" 3 report "Default fresh install partition structure is this" 3 echo "/*!50500 PARTITION BY RANGE COLUMNS(calldate)" echo "(PARTITION d20200101 VALUES LESS THAN ('2020-01-02') ENGINE = InnoDB," echo "PARTITION future VALUES LESS THAN (MAXVALUE) ENGINE = InnoDB) */ " } mysql_connect_data_v2 # Quit if future partition does not exist future_partition_exist=$(MYSQL_PWD="$DB_PASSWORD" /usr/bin/mysql -h "$DB_HOST" -u $DB_USERNAME "$DB_NAME" -sNe "SELECT count(*) FROM information_schema.partitions WHERE TABLE_SCHEMA='m2' AND TABLE_NAME = \"$TABLE\" AND PARTITION_NAME='future'") if [[ $future_partition_exist != 1 ]]; then report "Future partition does not exist for $TABLE. Exiting" 1 print_empty_partitions_info exit 1; fi # Quit if at least one partition with date does not exist partitions_count=$(MYSQL_PWD="$DB_PASSWORD" /usr/bin/mysql -h "$DB_HOST" -u $DB_USERNAME "$DB_NAME" -sNe "SELECT count(*) FROM information_schema.partitions WHERE TABLE_SCHEMA='m2' AND TABLE_NAME = \"$TABLE\" AND PARTITION_NAME!='future'") if [[ -z $partitions_count ]] || ((partitions_count < 1)); then report "Non-future partition does not exist for $TABLE. Exiting" 1 print_empty_partitions_info exit 1 fi MYSQL_PWD="$DB_PASSWORD" /usr/bin/mysql -h "$DB_HOST" -u $DB_USERNAME "$DB_NAME" --disable-column-names -B -e "SELECT PARTITION_NAME FROM information_schema.partitions WHERE TABLE_SCHEMA='m2' AND TABLE_NAME = \"$TABLE\" AND PARTITION_NAME IS NOT NULL;" | grep -v future | sed -e 's/^d//' > $existing_partitions_dates if (( $? != 0 )); then report "Error generating partitions list. Exiting" 1 exit 1 fi # Ruby script will take list from $existing_partitions_dates file # and will generate partitions list to create to into file $partitions_sql ruby /usr/src/m2/mysql/partitions/generate_partitions_list.rb "$DAYS" "$TABLE" "$START_DATE" "$$" > "$partitions_sql" if (( $? != 0 )); then report "Error generating partitions sql statements. Exiting, check errors from ruby script above" 1 exit 1 fi exec < $partitions_sql if [ $EXECUTE == "1" ]; then while read LINE do if ! MYSQL_PWD="$DB_PASSWORD" /usr/bin/mysql -h "$DB_HOST" -u $DB_USERNAME "$DB_NAME" -e "$LINE"; then report "Failed to create partition(s). Check error messages above. Exiting" 1 report "Partitions list which we tried to import are:" 3 cat $partitions_sql exit 1 fi done fi if [ -s "$partitions_sql" ] then if [ $EXECUTE == "1" ]; then report "$(wc -l $partitions_sql | awk -F" " '{print $1}') partition(s) have been created for table [$TABLE]" 0 else report "$(wc -l $partitions_sql | awk -F" " '{print $1}') partition(s) shoud have been created for table [$TABLE] with EXECUTE" 0 fi report "SQLs used to create partitions:" 3 cat $partitions_sql else report "No partition(s) to create to" 0 fi