Python backup MySQL Script

Source: Internet
Author: User

It is not very difficult to write a Python backup script for MySQL today. A lot more concise than the shell!

Open Whole:

Comments are written in English, some English-based friends should be able to read!

#!/usr/bin/env python#backup the gtshop#author:ley#encoding=utf8#date:2015-06import os,sys , datetime,timefrom stat import * #mysqlbackup  userUser =  ' root ' #mysqlbackup   passwordpassword =  ' root ' #mysqlbackup  commandMysqlcommand =  '/usr/local/mysql/bin/ Mysqldump ' #gzip  command Gzipcommand =  '/bin/gzip ' #backup  mysql databasemysqldata  = [' gtshop ' #backup  dirTobackup =  '/home/gtshop_backup/' for db in  Mysqldata: #backup  file nameBackupfile = Tobackup + DB +  '-'  +  time.strftime ('%y-%m-%d ')  +  '. sql ' #gzip  file namegzipfile = backupfile  +  '. Gz ' If os.path.isfile (gzipfile):p rint gzipfile +  "is already  Backup "Else: #backup  commandBack_command = Mysqlcommand +  '  -u '  + user  +  '  -p '  + Password +  '  --events  '  +  '  --master-data=2  '  +  '  --single-transaction  '  + DB +  '  >  '  + Backupfileif  Os.system (Back_command)  == 0:         print  ' Sucessful backup gtshop '     else:         print  ' backup failed ' #gzip  commandGzip_command = Gzipcommand +  '   '  + backupfile        if os.system (Gzip_command)  == 0:        print  ' Sucessful gzip gtshop '         else:print  ' gzip failed '

Execution Result:

[[email protected] script]# python mysql_backup.py sucessful backup gtshopsucessful gzip gtshop

Finally, it can be added to the task plan according to the requirements!


This article is from the "Liang Enyu-9527" blog, be sure to keep this source http://liangey.blog.51cto.com/9097868/1662026

Python backup MySQL Script

Related Article

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.