原本可以用shell完成的功能,现在学习python,就照抄照改。完成数据库备份。
#!/usr/bin/Python
#-*-coding:utf-8-*-
#Mysql BACK
import string,time,os,datetime
import sys,logging,stat
import subprocess
#os.environ.get(‘PERONA_A’)
os.environ["PATH"]="/usr/local/mysql/bin/:"
# os.putenv("PATH","/usr/local/mysql/bin/:")
#os.putenv(“PATH”,”/usr/bin/”)
# os.environ["PATH"]
year= time.strftime('%Y',time.localtime(time.time()))
moth= time.strftime('%m',time.localtime(time.time()))
data_path ="/mysqlbak/"+year+"/"+moth+"/"
sql_user ="root"
sql_pwd ="sh123456"
sql_ip ="127.0.0.1"
all="--all-databases --events"
def loger(datapath,logfile="mysqlbak.log"):
"""创建日志文件"""
if not os.path.exists(datapath):
os.makedirs(datapath)
log_filename = datapath+logfile
log_fORMat = '%(filename)s [%(asctime)s] [%(levelname)s] %(message)s'
logging.basicConfig(filename=log_filename,format=log_format,datefmt='%Y-%m-%d %H:%M:%S %p',level=logging.DEBUG)
#----------------------------------------------------------------------
def ClearDir(dir,age):
"""删除几天前的文件,age为时间长,如(86400为一天)"""
for file in os.listdir(dir):
now = time.time()
filepath = os.path.join(dir,file)
files = os.stat(filepath).st_mtime
if files