#创建基表
CREATE TABLE `table_sum` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`table_name` varchar(50) DEFAULT NULL,
`table_rows` int(11) DEFAULT NULL,
`total_size` int(11) DEFAULT NULL,
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
#创建统计表
CREATE TABLE `table_day_growth` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`table_name` varchar(50) DEFAULT NULL,
`table_rows` int(11) DEFAULT NULL,
`total_size` int(11) DEFAULT NULL,
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
#每天向基表里插入数据
cat insert_table_bak.sh
#!/bin/bash
source /etc/profile
mysql_login='/usr/local/mysql/bin/mysql'
$mysql_login -uroot -pxxx -h10.10.88.168 -P3306 -BNe "SELECT CONCAT(table_schema, '.', table_name) table_name, table_rows,CONCAT(ROUND(ROUND(data_length + index_length) / (1024 * 1024))) total_size FROM information_schema.tables WHERE table_schema NOT IN ('information_schema' , 'performance_schema', 'sys', 'mysql','zabbix','test') and table_rows >=1000;" > /tmp/table_sum.txt
$mysql_login -e "load data infile '/tmp/table_sum.txt' ignore into table test.table_sum character set utf8 lines terminated by '
' (table_name,table_rows,total_size);"
/bin/rm -rf /tmp/table_sum.txt
#统计基表里当天的数据
#!/bin/bash
mysql_login='/usr/local/mysql/bin/mysql'
$mysql_login -NBe "select table_rows from test.table_sum where create_time>=date_format(date_sub(now(),interval 1 day),'%Y-%m-%d %H:%i:00');" > s1
$mysql_login -NBe "select table_rows from test.table_sum where create_time<=date_format(date_sub(now(),interval 1 day),'%Y-%m-%d %H:%i:00') and create_time>=date_format(date_sub(now(),interval 2 day),'%Y-%m-%d %H:%i:00');" > s2
$mysql_login -NBe "select total_size from test.table_sum where create_time>=date_format(date_sub(now(),interval 1 day),'%Y-%m-%d %H:%i:00');" > s3
$mysql_login -NBe "select total_size from test.table_sum where create_time<=date_format(date_sub(now(),interval 1 day),'%Y-%m-%d %H:%i:00') and create_time>=date_format(date_sub(now(),interval 2 day),'%Y-%m-%d %H:%i:00');" > s4
$mysql_login -NBe "select table_name from test.table_sum where create_time>=date_format(date_sub(now(),interval 1 day),'%Y-%m-%d %H:%i:00') ;" > s10
n=1
for i in `cat s1`;do # 循环s1
num=`sed -n "${n}p" s2` # 获取s2对行的数字
minus=$(($i - $num)) # 两数相减
echo $minus >> s5 # 打印结果
n=$(($n + 1))
done
n=1
for i in `cat s3`;do # 循环s3
num=`sed -n "${n}p" s4` # 获取s4对行的数字
minus=$(($i - $num)) # 两数相减
echo $minus >> s6 # 打印结果
n=$(($n + 1))
done
/root/dba/calculate_table.py
/bin/rm -rf s1 s2 s3 s4 s5 s6 s10 table_info.txt
数据合并脚本
cat calculate_table.py
#!/usr/bin/env python
# coding:utf-8
import sys,os,MySQLdb
file1 = open("s5", "rb")
file2 = open("s6", "rb")
file3 = open("s10", "rb")
file_list1 = file1.readlines() # 将所有变量读入列表file_list1
file_list2 = file2.readlines() # 将所有变量读入列表file_list2
file_list3 = file3.readlines() # 将所有变量读入列表file_list2
# print(type(file1))
# 定义各属性数据存储列表
file_list1_name = []
file_list2_name = []
file_list3_name = []
# 遍历file_list1 列表 将得到的信息进行下列操作
for message in file_list1:
temp_list = message.split()
# 将txt文件中的第一行 也就是file_list1 列表的第一项 用split方法操作 以空格为分隔符 分成两部分继续放到temp_list列表里
file_list1_name.append(str(temp_list[0]))
# 操作与file_list1列表完全相同
for message in file_list2:
temp_list = message.split()
file_list2_name.append(str(temp_list[0]))
# 操作与file_list1列表完全相同
for message in file_list3:
temp_list = message.split()
file_list3_name.append(str(temp_list[0]))
file_list4 = []
for i in range(len(file_list3_name)):
s = ''
s = ' '.join([file_list3_name[i], file_list1_name[i], file_list2_name[i]])
s += '
'
file_list4.append(s)
# 将数据写入file4
file4 = open("table_info.txt", "w")
file4.writelines(file_list4)
# 关闭文件
file1.close()
file2.close()
file3.close()
file4.close()
#table_info = open("/root/dba/table_info.txt", mode="r")
table_info = open("table_info.txt", mode="r")
for info in table_info.readlines():
info_array = info.split()
arg1 = info_array[0]
arg2 = info_array[1]
arg3 = info_array[2]
# 打开数据库连接
conn = MySQLdb.connect(host='10.10.88.18', port=3306, user='root', passwd='123456', db='test')
# 使用cursor()方法获取操作游标
cursor = conn.cursor()
# SQL 插入语句
sql = "INSERT INTO table_day_growth(`table_name`,`table_rows`,`total_size`) values('%s','%s','%s')" % (arg1, arg2, arg3)
try:
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
except Exception as error:
print(error)
# 发生错误时回滚
conn.rollback()
# 关闭数据库连接
conn.close()
table_info.close()