python os 模块

cnbird 2009-11-30

python 模块 数据库 os path ftp open file html

Python 3 教程二:文件,目录和路径

http://www.cnitblog.com/yunshichen/archive/2009/04/01/55931.html

 

python os模块

http://hi.baidu.com/happynp/blog/item/729243f902d5a751242df2c2.html

http://hi.baidu.com/fiber212121/blog/item/6e07ec03c97b6982d53f7c27.html

 

python getopt模块

http://www.tsnc.edu.cn/default/tsnc_wgrj/doc/pythonhtml/html/scripts_and_streams/command_line_arguments.html

http://www.sqlite.com.cn/MySqlite/11/416.Html

http://docs.python.org/library/getopt.html

http://docs.python.org/library/optparse.html

 

mysqldbback.py

#!/usr/bin/python









import os




import time




import ftplib




import traceback









#config vars




systempathchr="/" #路径分割符,*nix用"/" win32用"//"









dbuser="root" #数据库用户名




dbpwd="dbpwd" #数据库密码




dbnamelist=["dbone","dbtwo","dbthree"] #需要备份那些数据库









workdir="/path/to/backup/" #本地备份文件夹




errlogfile="databack.log" #错误日志名




ftp_addr="192.168.0.2" #ftp地址




ftp_port="2102" #ftp端口




ftp_user="databack" #ftp用户名




ftp_pwd="backpwd" #ftp密码




ftp_path="/" #存放到ftp路径









ftpqueue=[]














def ftpstor():




#login




bufsize=1024




ftp=ftplib.FTP()




try:




ftp.connect(ftp_addr,ftp_port)




ftp.login(ftp_user,ftp_pwd)




ftp.cwd(ftp_path)




for filepath in ftpqueue:









#open file for input as binary




f=open(filepath,"rb")




#store file as binary




print getfilename(filepath)




ftp.storbinary("STOR "+getfilename(filepath),f,bufsize)




f.close()




ftp.quit()




except:




path=os.path.join(workdir,errlogfile)




traceback.print_exc(file=open(path,"a"))



















def dumpdb(dbname):




global ftpqueue




timeformat="%Y%m%d"




sqlvalformat="mysqldump -u%s -p/"%s/" /"%s/" >/"%s/""




tarvalformat="tar --directory=/"%s/" -zcf /"%s/" /"%s/""




nowdate=time.strftime(timeformat)




dumpfile=os.path.join(workdir,dbname+".dump")




zipfile=os.path.join(workdir,dbname+nowdate+".tar.gz")




sqlval=sqlvalformat % (dbuser,dbpwd,dbname,dumpfile)









result=os.system(sqlval)




tarval=tarvalformat % (workdir,zipfile,dbname+".dump")









result=os.system(tarval)




os.remove(dumpfile)




ftpqueue.append(zipfile)









def getfilename(path):









pt=path.rfind(systempathchr)




return path[pt+1:]









def main():




for dbname in dbnamelist:




dumpdb(dbname)









ftpstor()









main()
登录 后评论
下一篇
云栖号资讯小编
594人浏览
2020-04-07
相关推荐
python标准库学习7
994人浏览
2016-05-06 10:20:50
python GUI初步
739人浏览
2016-03-04 18:24:00
python之常用模块
985人浏览
2018-07-26 09:47:00
Python——模块
733人浏览
2017-05-04 20:50:00
关于python 模块导入
790人浏览
2014-06-03 20:51:00
python中的文件操作
489人浏览
2015-04-09 17:16:00
Python-装饰器详解
791人浏览
2017-10-18 12:10:00
python常用模块初始
660人浏览
2017-11-09 16:01:00
Python基础
841人浏览
2017-11-28 16:48:00
0
0
0
563