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.