http://www.cnblogs.com/hzhida/archive/2012/08/02/2620525.html


#!/usr/bin/env python
import sys,os,time
mysql_usr='chenjuan'
mysql_pwd='123456'
mysql_db='news'
mysql_charset='utf8'
today='./backup/'
logs_path='Logs.txt'

def writeLogs(filename,contents):
    f=file(filename,'a')
    f.write(contents)
    f.close()

fname=today+os.sep+time.strftime('%H%M%S')+'.txt'

if not os.path.exists(today):
 Msg='-'*30+time.strftime('%Y-%m-%d,%H:%M:%S')+'-'*30+'n'
 if(os.mkdir(today))==None:
	Msg+='**succeed to create dir:'+today+'nn'
	writeLogs(logs_path,Msg)
 else:
	Msg+='!!create backup dir:'+today+'failed,check whether dir can write! nn'
	writeLogs(logs_path,Msg)
 #sys.exit()

cmd_dump='mysqldump -u%s -p%s %s > %s' %(mysql_usr,mysql_pwd,mysql_db,fname)

if os.system(cmd_dump)==0:
    writeLogs(logs_path,'database backup: '+fname+'\n')
else:
    writeLogs(logs_path,'database backup failed! \n')

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐