import os
import time
import tarfile
import zipfile
‘‘‘
mysqldump
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help
‘‘‘
db_host="数据库地址"
db_user="数据库登录用户名"
db_passwd="登录密码"
db_name="数据库名称"
db_charset="utf8"
db_backup_name=r"E:MYSQL-beifenmysql_%s.sql" %(time.strftime("%Y%m%d%H%M"))
aav=r"e:mysql-beifen"
if not os.path.exists(aav):
os.makedirs(aav) #如果不存在则建立相应的文件夹
zip_src = db_backup_name
zip_dest = zip_src ".zip"
def zip_files():
f = zipfile.ZipFile(zip_dest, ‘w‘ ,zipfile.ZIP_DEFLATED)
f.write(zip_src)
f.close()
if __name__ == "__main__":
print("begin to dump mysql database crm...");
os.system("mysqldump -h%s -u%s -p%s %s --default_character-set=%s > %s" %(db_host, db_user, db_passwd, db_name, db_charset, db_backup_name))
print("begin zip files...")
zip_files()
print("done, pyhon is great!")