Python production Environment MySQL database incremental backup script

Source: Internet
Author: User

MySQL database is commonly used by the mysqldump export SQL for backup, but not suitable for a large number of databases, speed, lock table is two serious problems. The front has written a blog about Xtrabackup hot spare tools. The following script is the ability to automatically back up a database based on the Xtrabackup implementation.


Requirements Description:

A full backup of the database is done 23 o'clock every night. The next day, 0-22, an incremental backup every hour. Move the last full backup and 23 incremental backups to the specified directory before each backup, preserving 7 days of data.

PS: Don't ask me, why is the 23 point to perform a full backup, 0 points not better processing? Bingo, this is our business needs, hehe, can not say too thin, you know. Don't ask me, why not use the shell, at least do not write subprocess.call to tune the System command. All I can say is, I'm happy, you're in control? Ha ha!



#-*- coding: utf-8 -*-#!/usr/bin/python#====================================================#  Author: gaochenchao - EMail:[email protected]# Last modified:  2015-2-5# filename: innobackup.py# description: backup mysql files,base  percona xtrabackup# blog:gccmx.blog.51cto.com#==================================================== Import datetimeimport subprocessimport osimport sysimport logginglogging.basicconfig ( Level=logging. Debug,                format= ' % (asctime) s % (filename) s[line:% (Lineno) d] % (levelname) s % (message) s ',                 datefmt= '%a, %d %b %Y  %h:%m:%s ',                 filename= ' BaCkup.log ',                 Filemode= ' a ') backuser =  ' bkuser ' backpass =  ' bk2015 ' basedir =  '/mnt/backups ' Tomorrowdate = datetime.date.fromordinal (Datetime.date.today (). Toordinal () +1). Strftime ("%y%m%d") Todaydate = datetime.datetime.now (). Strftime ("%y%m%d") fullback_dir =  "%s/%s"  % ( basedir,tomorrowdate) Cuhour = datetime.datetime.now (). Strftime ("%H") #cuhour  = sys.argv[1] increment_dir =  '%s/%s '  % (basedir,cuhour) increbase_dir= ' stores =  '/mnt/stores ' # Dump old backup data and move it to a folder named after the current month and day, directory such as 150209-bakdef storebefore ():    suffix =  Datetime.datetime.now (). Strftime ("%y%m%d")     storedir =  "%s/%s-bak"  % ( Stores,suffix)     if not os.path.exists (storedir):         subprocess.call ("Mkdir -p %s " % (Storedir), shell=true)     command = " cd %s & & mv * %s " % (basedir,storedir)     subprocess.call (command,shell= True)     #  Delete backup data from the dump directory for more than 7 days Def cleanstore ():     command  =  "FIND %S -TYPE D -MTIME +7 |XARGS RM -FR"  %  Stores    subprocess.call (command,shell=true) #  backup method, 23-point full backup daily, 0-22-point incremental backup on the second day def  Backup ():     if not os.path.exists (Basedir):         subprocess.call ("mkdir -p %s"%basedir,shell=true)     commandfull  =  "innobackupex --user=%s --password=%s --no-timestamp %s"  % (Backuser, Backpass,fullback_dir)     if cuhour ==  ':      '    storebefore () &NBsp;       subprocess.call ("rm -fr %s/*"%basedir,shell=True)         subprocess.call (commandfull,shell=true)          logging.info (commandfull)     else:         if int (Cuhour)  - 1 >= 0:             increbase_dir =  '%s/%s '  % (basedir,str (int (cuhour)  - 1))         else:             increbase_dir =  "%s/%s"  % (basedir,todaydate)          if not os.path.exists (Increbase_dir):             logging.info (' Last incremental backup directory  [%s]  not present, terminating execution '%increbase_dir )       &nbsP;     exit (0)         commandincre =   "innobackupex --user=%s --password=%s --no-timestamp --incremental %s -- incremental-basedir=%s " % (                         backuser,backpass,increment_dir,increbase_ dir)         subprocess.call (commandincre,shell=true)          logging.info (Commandincre)         if  __name__ ==  ' __main__ ':     backup ()     cleanstore ()

This article is from the "Candle Shadow Red" blog, be sure to keep this source http://gccmx.blog.51cto.com/479381/1612100

Python production Environment MySQL database incremental backup 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.