程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python+MySQL定時備份前一天的數據

編輯:Python
import logging
import requests
from apscheduler.schedulers.background import BlockingScheduler
logging.basicConfig(level=logging.INFO)
LOCAL_PORT = 4000
LOCAL_HOST = '127.0.0.1'
def loop_export_task():
url = "http://{}:{}/data/export".format(LOCAL_HOST, LOCAL_PORT)
logging.info(url)
r = requests.get(url)
logging.info(r.text)
if __name__ == '__main__':
scheduler = BlockingScheduler(timezone="Asia/Shanghai")
# 每天凌晨1點執行
scheduler.add_job(loop_export_task, 'cron', day_of_week='*', hour='1', minute='0', second='0')
scheduler.start()
logging.info('The Next Run Time is: ', scheduler.get_jobs()[0].next_run_time)
@bp.route('/data/export')
def export_data():
ipmac_sql = """
select macaddr, recordtime, eventdetail, eventtype, ipaddr, pkttime, CONCAT(packet,'') as packet,
starttime, count_num, action, hashid as hash_id, endtime
from events_ipmac where date(recordtime)=date_sub(curdate(),interval 1 day);
"""
sql_list = [ipmac_sql,]
timestamp = int(time.time())
_, result = pool_connect_db.select_all(sql)
filepath = os.path.join(BASE_PATH, 'ipmac_sql')
if not os.path.exists(filepath):
os.makedirs(filepath)
with open('%s/data.json' % filepath, 'wb') as _file:
json.dump(result, _file)
return jsonify({'code': 200, 'msg': 'export data success'})


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved