当前位置: 首页 > 图文教程 > 脚本技术 > Python > Python Mysql自动备份脚本

Python
Python入门教程 超详细1小时学会Python
复制粘贴功能的Python程序
python远程登录代码
Python Mysql自动备份脚本
py中的目录与文件判别代码
python下如何让web元素的生成更简单的分析
Python 文件操作技巧(File operation) 实例代码分析
python备份文件的脚本
Python备份Mysql脚本
rhythmbox中文名乱码问题解决方法
Python交换变量
Python字符遍历的艺术
Python字符转换
Python isinstance判断对象类型
Python ljust rjust center输出
Python strip lstrip rstrip使用方法
Python 连接字符串(join %)
Python 字符串中的字符倒转
Python translator使用实例
Python 除法小技巧

Python Mysql自动备份脚本


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-11   浏览: 266 ::
收藏到网摘: n/a

测试系统环境 Windows 2003 python 2.5.1 mysql 5.0.1 应该只适用于Win,因为调用了CMD。 增量备份,因为自用,数据库不大。


测试系统环境 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!"