Python MySQL Automatic backup script

Source: Internet
Author: User
Test system Environment Windows 2003 python 2.5.1 MySQL 5.0.1

Should only apply to win because CMD is called.
Incremental backup, because for personal use, the database is small.
Back with the need to add self-detection, 5 days ago automatic deletion.

#!/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 ' # output file encoding, default 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" "% (Dbna Me, 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!"
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.