目录
一、目录结构
二、测试样例
三、本地测试代码
四、脚本执行代码
五、设置定时执行
六、参考
一、目录结构
二、测试样例
设置备份时间间隔为60S时,打印信息如下
三、本地测试代码
# -*- coding: utf-8 -*-
# @File : test.py
# @Date : 2021-11-17
# @Author : admin
import os
import shutil
from threading import Timer
import psutil
import time
import datetime
def MonitorSystem(logfile=None):
# cpu监测
cpuper = psutil.cpu_percent()
mem = psutil.virtual_memory()
memper = mem.percent
now = datetime.datetime.now()
ts = now.strftime('%Y-%m-%d %H:%M:%S')
line = '{0} cpu:{1}%, mem:{2}%'.format(ts,cpuper,memper)
print(line)
if logfile:
logfile.write(line)
# 启动定时器任务,每三秒执行一次
Timer(3, MonitorSystem).start()
def MonitorNetWork(logfile=None):
# 字节流监测
netinfo = psutil.net_io_counters()
now = datetime.datetime.now()
ts = now.strftime('%Y-%m-%d %H:%M:%S')
line = '{0} bytessent={1}, bytesrecv={2}'.format(ts,netinfo.bytes_sent,netinfo.bytes_recv)
print(line)
if logfile:
logfile.write(line)
# 启动定时器任务,每秒执行一次
Timer(1, MonitorNetWork).start()
def DatbaseSave(DB_HOST,DB_PORT,DB_USER,DB_USER_PASSWORD,DB_NAMES,BACKUP_PATH):
DATETIME = time.strftime('%Y%m%d')
cwd = os.getcwd() # 获取当前程序文件位置
TODAYBACKUPPATH = cwd + BACKUP_PATH + DATETIME
# Checking if backup folder already exists or not. If not exists will create it.正在检查备份文件夹是否已存在。如果不存在,将创建它。
if not os.path.exists(TODAYBACKUPPATH):
os.makedirs(TODAYBACKUPPATH)
today = datetime.datetime.now()
for item in os.listdir(BACKUP_PATH):
try:
foldername = os.path.split(item)[1]
day = datetime.datetime.strptime(foldername, "%Y%m%d")
diff = today - day
if diff.days >= 60:
print('- - - del folder two months ago: ' + BACKUP_PATH + item)
shutil.rmtree(BACKUP_PATH + item)
except:
pass
# Code for checking if you want to take single database backup or assinged multiple backups in DB_NAME.用于检查是否要在DB_名称中执行单个数据库备份或辅助多个备份的代码。
DBS = DB_NAMES.split(",")
for DB_NAME in DBS:
# Starting actual database backup process.
DB_PATH = TODAYBACKUPPATH + "/" + DB_NAME + ".sql"
print("- - - baking '" + DB_NAME + ".sql'")
dumpcmd = "mysqldump -h" + DB_HOST + " -P" + DB_PORT + " -u" + DB_USER + " -p" + DB_USER_PASSWORD + " --databases " + DB_NAME + " > " + DB_PATH
print(dumpcmd)
os.system(dumpcmd)
print("- - - Your backups has been created in '" + TODAYBACKUPPATH + "' directory")
#1分钟循环60S,一天循环86400
Timer(60, DatbaseSave,(DB_HOST,DB_PORT,DB_USER,DB_USER_PASSWORD,DB_NAMES,BACKUP_PATH)).start()
if __name__ == '__main__':
# 第一次启动时间
starttime = '10:27:00'
# 数据库ip
DB_HOST = ""
# 数据库端口
DB_PORT = ""
# 数据库用户名
DB_USER = ""
# 数据库密码
DB_USER_PASSWORD = ""
# 备份的数据库
DB_NAMES = ""
# 备份到的路径
BACKUP_PATH = "/sqlbak/"
while True:
today = datetime.datetime.now()
print(today.time().strftime('%H:%M:%S'))
if today.time().strftime('%H:%M:%S') == starttime:
#DatbaseSave(DB_HOST,DB_PORT,DB_USER,DB_USER_PASSWORD,DB_NAMES,BACKUP_PATH)
DatbaseSave('127.0.0.1', '3306', 'root', '123456', DB_NAMES, BACKUP_PATH)
# MonitorSystem()
# MonitorNetWork()
break
四、脚本执行代码
下边代码为backips.py文件代码
# -*- coding: utf-8 -*-
# @File : test.py
# @Date : 2021-11-17
# @Author : admin
import os
import shutil
from threading import Timer
import psutil
import time
import datetime
def DatbaseSave(DB_HOST,DB_PORT,DB_USER,DB_USER_PASSWORD,DB_NAMES,BACKUP_PATH):
DATETIME = time.strftime('%Y%m%d')
cwd = os.getcwd() # 获取当前程序文件位置
TODAYBACKUPPATH = cwd + BACKUP_PATH + DATETIME
# 正在检查备份文件夹是否已存在。如果不存在,将创建它。
if not os.path.exists(TODAYBACKUPPATH):
os.makedirs(TODAYBACKUPPATH)
today = datetime.datetime.now()
for item in os.listdir(BACKUP_PATH):
try:
foldername = os.path.split(item)[1]
day = datetime.datetime.strptime(foldername, "%Y%m%d")
diff = today - day
if diff.days >= 60:
print('- - - del folder two months ago: ' + BACKUP_PATH + item)
shutil.rmtree(BACKUP_PATH + item)
except:
pass
# 用于检查是否要在DB_名称中执行单个数据库备份或辅助多个备份的代码。
DBS = DB_NAMES.split(",")
for DB_NAME in DBS:
# Starting actual database backup process.
DB_PATH = TODAYBACKUPPATH + "/" + DB_NAME + ".sql"
print("- - - baking '" + DB_NAME + ".sql'")
dumpcmd = "mysqldump -h" + DB_HOST + " -P" + DB_PORT + " -u" + DB_USER + " -p" + DB_USER_PASSWORD + " --databases " + DB_NAME + " > " + DB_PATH
print(dumpcmd)
os.system(dumpcmd)
print("- - - Your backups has been created in '" + TODAYBACKUPPATH + "' directory")
if __name__ == '__main__':
# 数据库ip
DB_HOST = ""
# 数据库端口
DB_PORT = "3306"
# 数据库用户名
DB_USER = ""
# 数据库密码
DB_USER_PASSWORD = ""
# 备份的数据库
DB_NAMES = "smalloa"
# 备份到的路径
BACKUP_PATH = "/sqlbak/"
DatbaseSave(DB_HOST,DB_PORT,DB_USER,DB_USER_PASSWORD,DB_NAMES,BACKUP_PATH)
五、设置定时执行
Win10环境下python脚本定时运行_圈圈烃的博客-CSDN博客Win10环境下python脚本定时运行简介最近写了个小的python脚本,可是为了更好的实现脚本的功能就需要每隔6个小时运行一次。这时就要考虑如何实现每隔6个小时启动脚本程序。这里大概有两种思路,第一种:在程序里设定延时,当设定的时间到了自动开启主程序;第二种:利用win10系统的计划任务功能,设定触发条件,自动运行程序。其中第一种可以说是比较简单的方法,但是如果使用...https://blog.csdn.net/Q_QuanTing/article/details/81363348
六、参考
python数据库定时备份,并删除以前备份的数据_pcx333354的专栏-CSDN博客_python 定时删除mysqlpython定时备份多个数据库,并删除以前备份的数据# -*- coding: utf-8 -*-# Import required python libraries# pancx 2019-04-10import osimport timeimport datetimeimport shutil# MySQL database details to which backup ...https://blog.csdn.net/pcx333354/article/details/89319324
Python3-定时任务的四种实现方式_chen801090的博客-CSDN博客_python 定时任务最近做一个小程序开发任务,主要负责后台部分开发;根据项目需求,需要实现三个定时任务:定时更新微信token,需要2小时更新一次;商品定时上线;定时检测后台服务是否存活;使用Python去实现这三个任务,这里需要使用定时相关知识点;Python实现定点与定时任务方式比较多,找到下面四中实现方式,每个方式都有自己应用场景;下面来快速介绍Python中常用的定时任务实现方式:1.循环+...https://blog.csdn.net/chen801090/article/details/93335733MySQL定时备份数据库(全库备份) - letcafe - 博客园 (cnblogs.com)https://www.cnblogs.com/letcafe/p/mysqlautodump.html?
|