|
[root@zabbix_server zabbix_partitions]# cat partition.sh
#!/bin/bash
#
export PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/root/bin
# day * 24 * 60 * 60
# 这个变量决定的是分区的粒度,3 代表每3天一个分区
part_interval=$[1*24*60*60]
# 这个变量决定的是分区的总数量,比如 part_interval 为3,这里为60,那么时间跨度将是180天
# ,分区数量有上限,别超过1024个
part_count=480
# 这个变量表示你的分区表要从哪一天开始,这个值最好是观察一下,history 里面最小的 clock 值
# ,然后转换为相关的时间
part_begin=20160209
part_times=$(date -d "${part_begin}" +%s)
#yet_date=$(date -d ${part_begin} +%Y-%m-%d)
# 生成分区表的字符串
part_string=$(
for i in `seq 1 ${part_count}`;do
# 根据 part_times 这个时间戳生成一个类似 20160901 的可读性好的日期名
part_name=$(date -d @${part_times} +%Y%m%d)
# 同上,这个是在 UNIX_TIMESTAMP() 函数中要用的名字
yet_date=$(date -d @${part_times} +%Y-%m-%d)
if [ $i == $part_count ];then
echo "partition p${part_name} values less than(UNIX_TIMESTAMP(\"${yet_date} 00:00:00\"))"
else
echo "partition p${part_name} values less than(UNIX_TIMESTAMP(\"${yet_date} 00:00:00\")),"
fi
# 开始的时间 加上 间隔时间,生成新的时间戳
part_times=$[part_times+part_interval]
done
)
# 这里如果你不愿意去查一下你数据里最早的时间,那么就把第一个变量打开,第二个注释掉,这样他自己去查
#history_start=`mysql -Bse 'select clock from zabbix.history order by clock limit 1;'`
history_start=1455003811
#echo $history_start
now_date=$(date +%s)
insert_string=''
state=true
> /tmp/insert_zabbix.txt
while $state;do
history_para=$[history_start+part_interval]
diff_time=$[now_date-history_para]
if [ ${diff_time} -ge ${part_interval} ];then
insert_string="insert into zabbix.history select * from zabbix.history_bak where clock between ${history_start} and ${history_para};"
echo "${insert_string}" >> /tmp/insert_zabbix.txt
else
insert_string="insert into zabbix.history select * from zabbix.history_bak where clock >= ${history_start};"
echo "${insert_string}" >> /tmp/insert_zabbix.txt
state=false
fi
history_start=$[history_para+1]
done
echo "$part_string" > /tmp/part_string.txt
echo "${insert_string}"
echo "backup history to history_bak;"
/usr/local/mysql/bin/mysql zabbix -e 'alter table history rename history_bak;'
echo "create partions table history"
/usr/local/mysql/bin/mysql zabbix -e "CREATE TABLE history (
itemid bigint(20) unsigned NOT NULL,
clock int(11) NOT NULL DEFAULT '0',
value double(16,4) NOT NULL DEFAULT '0.0000',
ns int(11) NOT NULL DEFAULT '0',
KEY history_1 (itemid,clock)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 partition by range (clock)(${part_string});"
|
|
|