一个简单的切割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

猜你喜欢

转载自j2ee-yohn.iteye.com/blog/2314293