一個簡單的切割shell


#!/bin/bash

source /etc/profile

#mysql執行客戶端
mysql_c='mysql -h 127.0.0.1 -u root -e'

#需要切割或者分析的表名
table=""

#統計哪天數據(默認分析上一天數據)
statDate=`date -d '-1 day' +'%F'`


##切割表、當指定表文件>1GB時執行
function split_table()
{
if [ -z "$table" ];then
echo "table can't be empty"
return 0
fi

tfile="/data/mysql/db_log/$table.ibd"
if [ ! -f "$tfile" ];then
echo "split_table `date +'%F %H:%M:%S'` db file not found $tfile"
return 0
fi

fsize=`ls -l $tfile 2>/dev/null | awk -F ' ' '{print $5}'`
((compGB=1024))#*1024*1024*1))
if [[ -z "$fsize" || $fsize -lt $compGB ]];then
echo "split_table `date +'%F %H:%M:%S'` $tfile < 1GB 不需要切分"
return 1;
fi

bak_table="$table"_`date +'%y%m%d%H'`
tmp_table="$table"_bak;

sql="create table if not exists db_log.$tmp_table like db_log.$table;
alter table db_log.$table rename db_log.$bak_table;
alter table db_log.$tmp_table rename db_log.$table;
"

echo "split_table `date +'%F %H:%M:%S'` start fileSize[$fsize B],rename[$table to $bak_table]"
$mysql_c "$sql" #執行切表
echo "split_table `date +'%F %H:%M:%S'` end fileSize[$fsize B],rename[$table to $bak_table]"
}

case "$1" in
splitTable)
#指定表切割
if [ -n "$2" ];then
table="$2"
split_table
return
fi

table="tb_stats_log"
split_table
;;
*)
"Using :(splitTable)"
;;

esac
exit 0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章