Open bringtree opened 7 years ago
来源 https://git.oschina.net/jaychen007/codes/qbsr5izud1f48pcnekw2j24
import os import time import sched # 第一个参数确定任务的时间,返回从某个特定的时间到现在经历的秒数 # 第二个参数以某种人为的方式衡量时间 schedule = sched.scheduler(time.time, time.sleep) # 数据库端口,用户名,密码,数据库名 dbhost = 'XXX' dbuserName = 'XXX' dbpw = 'XXX' dbname = 'XXX' dbport = 'XXX' # shell命令 def backupsDB(): # 数据库命名 规则:数据库名+年月日+后缀 name = dbname + "_" + str(time.strftime('%Y%m%d', time.localtime(time.time()))) + ".sql.gz" # mysql 备份,压缩 # 压缩命令后面那个也是 地址也要写上 # cmdString = 'mysqldump -u' + dbuserName + ' -p' + dbpw + ' -h' + dbhost + ' -P' + dbport + ' ' + dbuserName + '| gzip > ' + name print(cmdString) # 执行备份命令 os.system(cmdString) # 判断备份文件是否存在 name 要改成路径名字 + name if os.path.isfile(name): return True else: return False def perform_command(inc): if backupsDB(): print("backup success") else: print("backup fail") # 安排inc秒后再次运行自己,即周期运行 schedule.enter(inc, 0, perform_command, (inc,)) def timming_exe(inc=86400): # enter用来安排某事件的发生时间,从现在起第n秒开始启动 schedule.enter(inc, 0, perform_command, (inc,)) # 持续运行,直到计划时间队列变成空为止 schedule.run() timming_exe(86400)
centos7 安装mysqldump yum install mariadb-server mariadb
yum install mariadb-server mariadb
Python 3 任务定时完成http://cstriker1407.info/blog/then-python3-time-to-work-with-scheduled-tasks/
http://cstriker1407.info/blog/then-python3-time-to-work-with-scheduled-tasks/
来源 https://git.oschina.net/jaychen007/codes/qbsr5izud1f48pcnekw2j24