Python MySQL automatic backup script

Source: Internet
Author: User

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! "

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.