With the knowledge, Python multithreading backs up the database and deletes old backups.

Source: Internet
Author: User

1 #!/usr/bin/python2 #-*-coding=utf-8-*-3 Import Time4 ImportOS5 Importdatetime6 ImportThreading7  fromTimeImportCtime,sleep8 9Date=time.strftime ('%y-%m-%d', Time.localtime (Time.time ()))TenDbname= ('Test','test2')#The definition tuple must have more than one, or the character inside the loop.  OneDbname2= ('test3','test4') ABkdir="/backup/mysqlbk/" -  - #Delete backup files for more than 3 days theNow_time =Datetime.datetime.now () -Yes_time = Now_time + Datetime.timedelta (days=-3) -Out_time = Yes_time.strftime ('%y-%m-%d') -  +  -  +  A defdumpsql (): at      forIinchdbname: -Os.system ("/usr/local/mysql/bin/mysqldump-u%s%s | gzip >%s%s"% ("Root", i,bkdir,date+"."+i+". sql.gz")) -  - defdumpsql2 (): -      forIinchdbname2: -Os.system ("/usr/local/mysql/bin/mysqldump-u%s%s | gzip >%s%s"% ("Root", i,bkdir,date+"."+i+". sql.gz")) in  -  to defOutbk (): +      forIinchdbname: -         ifOs.path.exists (bkdir+out_time+'.'+i+'. sql.gz'): theOs.remove (bkdir+out_time+'.'+i+'. sql.gz') *  $ #Dual ThreadingPanax Notoginsengthreads=[] -T1 = Threading. Thread (target=dumpsql) the threads.append (t1) +T2 = Threading. Thread (target=dumpsql2) A threads.append (T2) the  + if __name__=="__main__": - Outbk () $      forTinchThreads: $T.setdaemon (True)#Start Multithreading - T.start () - T.join () the     Print("All over %s"%ctime ())

With the knowledge, Python multithreading backs up the database and deletes old backups.

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.