一个简单的切割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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值