Backup MySQL database with Python script

Source: Internet
Author: User
Tags datetime mysql backup mysql database python script backup
#!/usr/bin/env python #coding =utf8 #author: Itnihao #mail: itnihao@qq.com #source: Http://code.taoba o.org/p/python2/src/trunk/#version: 1.0 ' function: MySQL backup. Use Mysqldump to backup the libraries in mysql 1.user,pass,host,path,del parameter is variable 2. The default backup deletion cycle is 5 days, and the backup is in days 3. The default backup path is/mysql_backup, and if it does not exist it will be created Record 4. Use for the execution of permissions, timed task run ' ' Import os,subprocess,datetime ' ' Set variable ' mysql_user = ' root ' mysql_pass = ' pa SS ' Mysql_host = ' localhost ' del_days = 5 Back_path = '/mysql_backup ' ' ' Time set ' ' Cur_time = Datetime.dat
        
        
E.today () Ago_time = Datetime.timedelta (days=del_days) del_time = cur_time-ago_time DATABASE = '    
        ' Backup function ' Def mysqldump (): If Os.path.isdir (Back_path): Os.chdir (Back_path) Else: Os.mkdir (Back_path) os.chdir (Back_path) database_cmd=subprocess. Popen ("Mysql-u%s-p%s-h%s-e ' show databases ' |grep-v database|grep-v information"% (Mysql_user,Mysql_pass,mysql_host), stdout=subprocess.    
        pipe,shell=true) Database_name=database_cmd.stdout.read (). Split () for the DATABASE in database_name: Mysqldump_filename= "/mysql_backup/%s%s.sql"% (cur_time,database) subprocess.call ("mysqldump-u%s-p%s-h%s %s>%s "% (mysql_user,mysql_pass,mysql_host,database,mysqldump_filename), shell=true) if Os.path.isfile (' ${DEL _time}${database}.sql '): Subprocess.call ("rm ${del_time}${database}.sql", shell=true) mysqldum P ()

Script Download Address http://code.taobao.org/p/python2/src/trunk/mysql_backup.py

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.