阅读:12271回复:3
Python2.7之定期自动备份MySQL数据库
配合Windows计划任务或者linux的/etc/crontab,可以实现Python自动备份MySQL数据库
#-*- coding:utf-8 -*- import os import time import tarfile import zipfile db_host="MYSQL地址" db_user="MYSQL账号" db_passwd="MYSQL密码" db_name="MYSQL数据库名称" db_charset="utf8" db_backup_name=r"备份后文件名" 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("D:\Backup_Program\mysqldump -h%s -P 3336 -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!") |
|
沙发#
发布于:2016-11-22 10:34
|
|
板凳#
发布于:2018-07-18 21:44
支持谢分享
|
|
地板#
发布于:2018-07-18 21:45
辛苦分享
|
|