Python Mysql自动备份脚本
程序员文章站
2022-04-28 20:09:28
...
测试系统环境 Windows 2003 python 2.5.1 mysql 5.0.1
应该只适用于Win,因为调用了CMD。
增量备份,因为自用,数据库不大。
回头有了需求加上自检测,5天前的自动删除。
#!/usr/bin/env python
#encoding=utf-8
#Mysql auto backup
#Author: vane
import os, sys, datetime
reload(sys)
sys.setdefaultencoding('utf-8')
backup_path = """d:\\mysql_backup_files"""
dbhost = "localhost"
dbname = "dabatase name" # 数据库名
dbuser = "root" # 用户名
dbuserpw = "123456" # 密码
dbcharset = 'utf8' # 输出文件编码,默认UTF8
now = str(datetime.datetime.now())[:10]
backup_command = """mysqldump -B %s -h%s -u%s -p%s --default_character-set=%s --opt>%s\dbbackup_%s_%s.sql\n""" % (dbname, dbhost, dbuser, dbuserpw, dbcharset, backup_path, dbname, now)
a, b = os.popen2('cmd')
a.write(backup_command)
a.close()
b.read()
b.close()
print "Done!"
应该只适用于Win,因为调用了CMD。
增量备份,因为自用,数据库不大。
回头有了需求加上自检测,5天前的自动删除。
#!/usr/bin/env python
#encoding=utf-8
#Mysql auto backup
#Author: vane
import os, sys, datetime
reload(sys)
sys.setdefaultencoding('utf-8')
backup_path = """d:\\mysql_backup_files"""
dbhost = "localhost"
dbname = "dabatase name" # 数据库名
dbuser = "root" # 用户名
dbuserpw = "123456" # 密码
dbcharset = 'utf8' # 输出文件编码,默认UTF8
now = str(datetime.datetime.now())[:10]
backup_command = """mysqldump -B %s -h%s -u%s -p%s --default_character-set=%s --opt>%s\dbbackup_%s_%s.sql\n""" % (dbname, dbhost, dbuser, dbuserpw, dbcharset, backup_path, dbname, now)
a, b = os.popen2('cmd')
a.write(backup_command)
a.close()
b.read()
b.close()
print "Done!"