Test System Environment Windows 2003 Python 2.5.1 MySQL 5.0.1
It should only apply to win, because CMD is called.
Incremental backup, because of your own use, the database is not large.
If you have the need to add a self-detection function, it will be automatically deleted five days ago.
#! /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" # Database Name
Dbuser = "root" # User Name
Dbuserpw = "123456" # Password
Dbcharset = 'utf8' # encoding of the output file. The default value is 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! "