这两天用python谢了个表数据的归档脚本,记录一下。[root@monitor python_scripts]# crontab -l# 表数据归档30 21 * * * cd /root/python3/ && source bin/activate && cd /opt/sh/python_scripts/ && python mysql_pigeonhole.py---------------------[root@monitor python_scripts]# cat execute_mysql_pigeonhole.sh#!/bin/shcd /root/python3/ && source bin/activate && cd /opt/sh/python_scripts/ && python mysql_pigeonhole.py---------------------[root@monitor python_scripts]# cat mysql_pigeonhole.py#!/usr/bin/env python3#-*- coding:utf8 -*-#author:import pymysql,time,loggingfrom datetime import datetime#设置日志import logging.configlogging.config.fileConfig("logging.conf")logger = logging.getLogger("dev")#date_time = datetime.now().strftime("%Y-%m-%d")#设置间隔时间,目前单位为天:dayinterval = 7logger.info("任务开始...")logger.info(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))conn = pymysql.connect(host="",user="",password="",database="",port=,charset="utf8")cursor = conn.cursor()#查询迁移前的最后一条数据idsql_select_begin = "select id from t_rank_log where created < unix_timestamp(current_date() - interval %d day) order by id desc limit 1;" % (interval)#迁移数据sqlsql_insert = "insert into t_rank_log_pigeonhole select * from t_rank_log where created < unix_timestamp(current_date() - interval %d day) ;" % (interval)#迁移后新表查询sql_select_new = "select id from t_rank_log_pigeonhole order by id desc limit 1;"#原表数据清除、空间整理sql_delete = "delete from t_rank_log where created < unix_timestamp(current_date() - interval %d day) ;" % (interval)sql_alter = "alter table t_rank_log engine=innodb"#依次执行SQL#sql_select_begincursor.execute(sql_select_begin)result_tmp1 = cursor.fetchall()if result_tmp1 is (): result_select_begin = None logger.info("result_select_begin 查询无数值 请调节 interval 参数")else: result_select_begin = result_tmp1[0][0] logger.info("result_select_begin :%s" % result_select_begin)try: cursor.execute(sql_insert)except pymysql.err.IntegrityError as Duplicate: logger.info("MySQL data Error: %s" % Duplicate)#sql_select_newcursor.execute(sql_select_new)result_select_new = cursor.fetchall()[0][0]logger.info("result_select_new :%s" % result_select_new)if result_select_begin == result_select_new: logger.info("数据迁移成功,开始清除原表历史数据") #sql_delete,sql_alter cursor.execute(sql_delete) logger.info("原表数据清理完成,开始回收空间碎片") cursor.execute(sql_alter) logger.info("空间碎片回收完毕。。。。") logger.info("任务完毕。。。\n")else: logger.info("数据迁移失败,请查看!!!!\n")conn.commit()conn.close()---------------