Python Backup Database

Source: Internet
Author: User

March into Python ...

Environment required for operation: PYTHON,MYSQLDB

Paste the Python script you wrote about SQL backup:

#!/usr/bin/env python
Import MySQLdb
Import OS
Import time
Datalist=[]
Remoteaddr= ' xxxx '
User= ' Root '
password= ' xxxx '
Dbconnect = MySQLdb.connect (%s,%s,%s)% (Remoteaddr,user,password)
Cursor=dbconnect.cursor ()
Sql= "Show Databases"
Cursor.execute (SQL)
Dbconnect.commit ()
Data=cursor.fetchall ()
For list in data:

# print List[0]
Datalist.append (List[0])
#print data
Print DataList
Datalist.remove ("Information_schema")
Datalist.remove ("MySQL")
Datalist.remove ("Test")
#print dataList
Dbconnect.close ()
Time=time.strftime ("%y-%m-%d")
Backup_dir= "/tmp/xiao/%s"%time
If not os.path.exists (Backup_dir):
Os.makedirs (Backup_dir)
Print "Backup directory created ..."
For MySQL in dataList:
mysqldump= "mysqldump-u%s-p%s-h%s%s>%s/%s.sql"% (User,password,remoteaddr,mysql,backup_dir)
Os.system (mysqldump)
Print "All databases are backuped ..."

This article from "The Young Master very busy" blog, please be sure to keep this source http://yingzi234.blog.51cto.com/3374280/1430377

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.