python定时按日期备份MySQL数据并压缩
本文实例为大家分享了python定时按日期备份MySQL数据并压缩的具体代码,供大家参考,具体内容如下
#-*- coding:utf-8 -*- 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="localhost" db_user="root" db_passwd="root" db_name="crm" db_charset="utf8" db_backup_name=r"c:\crm_%s.sql" %(time.strftime("%Y%m%d%H%M")) 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!")
相关推荐
CoderToy 2020-11-16
emmm00 2020-11-17
王艺强 2020-11-17
ribavnu 2020-11-16
bianruifeng 2020-11-16
wangshuangbao 2020-11-13
苏康申 2020-11-13
vivenwan 2020-11-13
moyekongling 2020-11-13
云中舞步 2020-11-12
要啥自行车一把梭 2020-11-12
aydh 2020-11-12
kuwoyinlehe 2020-11-12
minerk 2020-11-12
vitasfly 2020-11-12
jazywoo在路上 2020-11-11
敏敏张 2020-11-11
世樹 2020-11-11