本文实例为大家分享了python定时按日期备份mysql数据并压缩的具体代码,供大家参考,具体内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
#-*- 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!" )
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/KimSoft/article/details/7461982