Python personal backup MySQL

Source: Internet
Author: User


Named Directory + file name


Inset into



#-*-Coding:utf-8-*-

#!/usr/bin/env python

###########################################################

#

# This Python script was used for MySQL database backup

# using Mysqldump utility.

#

##########################################################


# Import Required Python libraries

Import OS

Import time

Import datetime


#MySQL数据库详细信息要完成的备份. Ensure that the following users have sufficient permissions to make a database backup

#要进行多个数据库备份, create a file like/backup/dbnames.txt and place a database name on each line and assign it to the db_name variable


Db_host = ' 127.0.0.1 '

Db_user = ' USER '

Db_user_password = ' User_password '

db_name = ' table name ' #实例名称

Backup_path = '/backup/'


# Gets the current datetime to create a detached backup folder, such as "12012013-071334".

DATETIME = Time.strftime ('%m%d%y-%h%m%s ' +db_name)


#命名备份目录 + file name

Todaybackuppath = Backup_path


# Check to see if the backup folder already exists. If it does not exist, it will be created.

"" "Print (" Create backup Folder ")" ""

If not os.path.exists (Todaybackuppath):

Os.makedirs (Todaybackuppath)


# used to check if you want to make a single database backup in db_name or to assign multiple backups of code.

# "" "Print (" Check database name file. ")" ""

# if Os.path.exists (db_name):

# file1 = open (db_name)

# multi = 1

#     """"""

# print ("Databases file found ...")

# print ("Starting backup of all DBS listed in file" + db_name)

#

# Else:

#     """"""

# print ("Databases File not found ...")

# print ("Starting backup of database" + db_name)

#

# multi = 0


# Start the actual database backup process.

# if Multi:

# in_file = open (db_name, "R")

# flength = Len (In_file.readlines ())

# In_file.close ()

# p = 1

# dbfile = open (db_name, "R")

#

# while P <= flength:

# db = Dbfile.readline () # Read database name from file

# db = db[:-1] # Delete extra rows

# dumpcmd = "Mysqldump-u" + Db_user + "-P" + Db_user_password + "+ db +" > "+ Todaybackuppath +"/"+ db + ". sql"

# Os.system (Dumpcmd)

# p = p + 1

# Dbfile.close ()

# Else:

Def back_base ():

db = DATETIME

Dumpcmd = "Mysqldump-u" + Db_user + "-P" + Db_user_password + "+ db +" > "+ Todaybackuppath +"/"+ DB +". sql "

Os.system (Dumpcmd)


"Mysqldump-h localhost-uroot-p123456 database table > Dump.sql"


Print ("Backup script completed")

Print ("Your backups have been created in '" + Todaybackuppath + "' Directory")





source./*****sql;



MySQL export table structure and table data mysqldump usage


The following are the specific uses of the command line:


Mysqldump-u User name-p password-d database Name table name > script name;



Export the entire database structure and data

Mysqldump-h localhost-uroot-p123456 Database > Dump.sql


Export a single data table structure and data

Mysqldump-h localhost-uroot-p123456 database table > Dump.sql


Export the entire database structure (no data included)

Mysqldump-h localhost-uroot-p123456-d Database > Dump.sql


Export a single data table structure (no data included)

Mysqldump-h localhost-uroot-p123456-d database table > Dump.sql


Python personal backup MySQL

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.