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. I wrote the previous article about Xtrabackup's hot spare tools, see http://www.linuxidc.com/Linux/2015-02/113058.htm. 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 datetime
Import subprocess
Import OS
Import Sys
Import logging
Logging.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 '
#转储老的备份数据, move to the folder named after the current month and date, directory such as 150209-bak
Def 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 for more than 7 days in the Dump directory
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 = = ' 23 ':
Storebefore ()
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] does not exist, terminating execution '%increbase_dir ')
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 ()
MySQL management uses xtrabackup for hot standby http://www.linuxidc.com/Linux/2014-04/99671.htm
MySQL Open source Backup tool xtrabackup Backup Deployment http://www.linuxidc.com/Linux/2013-06/85627.htm
MySQL xtrabackup Backup and Recovery http://www.linuxidc.com/Linux/2011-12/50275.htm
Use Xtrabackup to implement MySQL master-slave replication quickly deploy "main unlocked table" http://www.linuxidc.com/Linux/2012-10/71919p2.htm
Install and use Percona launched Xtrabackup backup MySQL http://www.linuxidc.com/Linux/2011-10/44451.htm
xtrabackup Details : please click here
xtrabackup : please click here.
This article permanently updates the link address : http://www.linuxidc.com/Linux/2015-02/113057.htm
Python production Environment MySQL database incremental backup script