MySQL Add-on

Source: Internet
Author: User
Tags filetime mkdir stomp percona

MySQL is ready to be added every 5 minutes

1, logic

2, directory structure diagram

3,producer

#!/usr/local/bin/python3#-*-coding:utf-8-*-# ====================================================#Author:changbo-[email protected]#Last modified:2017-9-3#Filename:mysqlincrement.py#description:backup mysql files,base percona xtrabackup#Http://www.cnblogs.com/changbo# ====================================================ImportStompImport TimeImportThreadingnowtime= Time.strftime ("%h%m", Time.localtime ())defProduct (TIMESTAMP): Conn= Stomp. Connection10 ([('xxxxx', XXXX)]) Conn.start () conn.connect ( )#if time equals 0030, send a ' bakold ', otherwise pass ' startjob '    iftimestamp = ='0030': Conn.send ('Samplequeue','Bakold')    Else: Conn.send ('Samplequeue','Startjob') Conn.disconnect ()if __name__=='__main__': Count=0 whileTrue:ifNowtime = ='0030': T= Threading. Thread (Target=product, args=(Nowtime,)) T.start () Count-= 5elifCount = = 5: T= Threading. Thread (Target=product, args=(Nowtime,)) T.start () Count-= 5Else: Count+ = 1Time.sleep (30L

4,consumer

#!/usr/local/bin/python3#-*-coding:utf-8-*-# ====================================================#Author:changbo-[email protected]#Last modified:2017-9-3#Filename:mysqlincrement.py#description:backup mysql files,base percona xtrabackup#Http://www.cnblogs.com/changbo# ====================================================ImportStompImportMysqlsingleImportThreadingImportOSImportTimenowday= Time.strftime ("%y%m%d", Time.localtime ()) Nowtime= Time.strftime ("%h%m", Time.localtime ()) Basedir=Mysqlbakdir=classSamplelistener (object):defon_message (self, headers, message):Try:            ifMessage = ='Startjob':                if  notos.path.exists (Mysqlbakdir): Mysqlsingle.createdir ()Else: Mysqlsingle.incrembak ()elifMessage = ='Bakold': Mysqlsingle.mvbak ()Else:                Pass        exceptException as E:PassdefResivemessage (): Conn= Stomp. Connection10 ([(xxxxxXxxxx)]) Conn.set_listener ('Samplelistener', Samplelistener ()) Conn.start () Conn.connect () conn.subscribe ('Samplequeue') Conn.disconnect ()if __name__=='__main__':     whileTrue:t= Threading. Thread (target=resivemessage) T.start ()

5,mysql Add script

#!/usr/local/bin/python3#-*-coding:utf-8-*-# ====================================================#Author:changbo-[email protected]#Last modified:2017-9-3#Filename:mysqlincrement.py#description:backup mysql files,base percona xtrabackup#Http://www.cnblogs.com/changbo# ====================================================Import TimeImportdatetimeImportOSImportsubprocessImportLogginglogging.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') Nowday= Time.strftime ("%y%m%d", Time.localtime ()) Nowtime= Time.strftime ("%h%m", Time.localtime ()) Basedir=Mysqlbakdir=Perfectdir=Incremdir=Childdir=#Get yesterday Backup files directorydefGetyesterday (): Today=datetime.date.today () oneday= Datetime.timedelta (Days=1) Yesterday= str ((today-oneday)). Replace ('-',"')    returnYesterdaydefCreatedir ():#Create a backup root directory    if  notos.path.exists (Basedir): Os.mkdir (Basedir)#Create a daily backup directory    if  notos.path.exists (Mysqlbakdir): Os.mkdir (Mysqlbakdir)#create a fully prepared directory and perform a fully prepared    if  notos.path.exists (Perfectdir): Os.mkdir (perfectdir) Command1='Innobackupex--defaults-file=xxxxx--no-timestamp%s'%perfectdir Os.system (Command1)#Create an Add -on directory    if  notos.path.exists (Incremdir): Os.mkdir (Incremdir)#Create a sub-backup directory and add    if  notos.path.exists (Childdir): Os.mkdir (childdir) Command2='Innobackupex--defaults-file=xxxxx--no-timestamp--incremental-basedir=%s--incremental%s'%(Perfectdir, Childdir) Os.system (Command2)defIncrembak ():#Gets the folder directory name that was last createdfilename = (subprocess. Popen ("ls"+ Incremdir +"| head-2 | tail-1 | awk ' {print $9} '", shell=True, stdout=subprocess. PIPE). Stdout.read ()). Decode ()#Create an Add -on directoryOs.mkdir (childdir) Command3='Innobackupex--defaults-file=xxxxx--no-timestamp--incremental-basedir=%s/%s--incremental%s'%(incremdir, filename, childdir) os.system (Command3)defMvbak (): Filetime=Getyesterday ()#Compress yesterday backup directoryCommand4 ="CD%s && tar czf hkmysqlbak%s.tar.gz%s"%(Basedir, FILETIME, filetime) logging.debug (Os.system (command4))#Remove the backup directory from yesterdayCommand5 ="CD%s && rm-rf%s"%(Basedir, FILETIME) logging.debug (Os.system (COMMAND5) )if __name__=='__main__':    if  notos.path.exists (Mysqlbakdir): Createdir () Incrembak ()

Novice python, welcome to the slot

end!

MySQL Add-on

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.