MySQL backup script V2 (add log function and post-backup check)

Source: Internet
Author: User
Tags mysql backup timedelta

Backup scripts

#!/usr/bin/env python#_*_coding:utf-8_*_ "" "@File:  backup_db.py@author: oldtan@email: [ Email protected] @Last  modified: 20180408 "" "import osimport datetimefrom  threading import threadfrom logs import loghost =  ' 10.36.1.101 ' USER =   ' root ' pw =  ' 1qaz#edc ' path =  '/mnt/tantianran_mysql_backup/' now =  Datetime.datetime.now () nowstr = now.strftime ('%y%m%d ') cleanstr =  (now -  Datetime.timedelta (days=5)). Strftime ('%y%m%d ') dbs = [     ' Urun_private_cloud ',      ' Urun-asr ',     ' Ambari ',     ' hive ',      ' Oozie ',     ' storm-etl ',     ' urun-sms ',      ' analyzer-etl ',     ' Zabbix ',     ' Zabbix-key ']after_backup  = []def clean (db):   &NBsp; database =  ' {DBS} '. Format (dbs=db)     format =  ' RM -RF  % (path) s% (db) s.% ( Date) S.sql '     kv = {' path ': path,  ' date ': cleanstr,  ' db '  : database}    expiredDbFile =  ('% (db) s.% ( Date) S.sql '  % kv)     if os.path.exists (expireddbfile):         msg =  ' start delete expired database file: % S '  %  (expireddbfile)         log (msg)          excute ( FORMAT % KV) def backup (db):     database =  ' {DBS} '. Format (dbs=db)     format =  ' mysqldump -u  % (user) s -h % (host) s -p% (PW) s % (db) s > % (path) s% (db) s.% ( Date) S.sql '     kv = {'User ': user,  ' host ': host,  ' pw ': pw,  ' path ': path,  ' date ':  nowstr ,  ' db '  : database}    dbname =  ('% (db) s.% ( Date) S.sql '  % kv)     after_backup.append (dbname)     excute ( FORMAT % KV) Def excute (cmd):     os.system (cmd) def check ():     for i in after_backup:        if  Os.path.exists (i):            m =  ' backup success db: %s '  % i             log (m)         else:             m =  ' backup failed db: %s '  % i          &Nbsp;  log (M) def main ():         dbs = range ( Len (DBS))         def exec_task (TH):         for i in dbs:             th[i].start ()         for i in dbs:                      th[i].join ()     backup_t = []    clean_t = [ ]        for i in dbs:         t1 = thread (target=backup, args= (Dbs[i],))          t2 = thread (target=clean, args= (Dbs[i],))          backup_t.append (T1)         clean_t.append (T2)     exec_task (backup_ T)     exec_task (clean_t)         check ()      if __name__ ==  "__main__":     main ()

SCP SQL File script

#!/usr/bin/env python#_*_coding:utf-8_*_ "" "@File:  backup_db.py@author: oldtan@email: [ Email protected] @Last  modified: 20180408 "" "import osimport datetimefrom  threading import threadfrom logs import logremote_path =  '/mnt/tantianran_ mysql_backup/' remote_host =  ' 10.36.1.55 ' remote_user =  ' root ' path =  '/mnt/ tantianran_mysql_backup/' Now = datetime.datetime.now () nowstr = now.strftime ('%Y%m%d ') Cleanstr = now.strftime ('%y%m%d ') dbs = [     ' Urun_private_cloud ',      ' Urun-asr ',     ' Ambari ',     ' hive ',      ' Oozie ',     ' storm-etl ',     ' urun-sms ',      ' analyzer-etl ',     ' Zabbix ',     ' Zabbix-key ']def scp_ Db_file (db):     DATABASE&Nbsp;=  ' {DBS} '. Format (dbs=db)     format =  ' scp % (path) s% (db) s.% ( Date) s.sql % (Remote_user) [email protected]% (Remote_host) s:% (remote_path) s '     kv  = {' path ': path,  ' date ': cleanstr,  ' db '  : database,  ' Remote_ User ': remote_user,  ' remote_host ': remote_host,  ' Remote_path ': remote_path}     d =  ('% (db) s.% ( Date) S.sql '  % kv )     if os.path.exists (d):         m =  "Start scp {} database sql file ...". Format (DB)         log (m)         excute (  FORMAT % KV) Def excute (cmd):     os.system (cmd) def main ():     print ("        --------------------------------------------                Transfer sql backup files        ---------------- ----------------------------     ")     thread_list = []     db_number = range (Len (DBS))          For i in db_number:        scp_t = thread ( Target=scp_db_file, args= (Dbs[i],))         thread_list.append (SCP _t)     for i in db_number:         Thread_list[i].start ()         for i in db_number:             thread_list[i].join () if __name__ = =  "__main__":    main () 

Purge expired SQL file scripts

#!/usr/bin/env pythonimport osimport datetimefrom threading import threadfrom  logs import logPATH =  '/mnt/tantianran_mysql_backup/' now =  Datetime.datetime.now () nowstr = now.strftime ('%y%m%d ') cleanstr =  (now -  Datetime.timedelta (days=5)). Strftime ('%y%m%d ') dbs = [     ' Urun_private_cloud ',      ' Urun-asr ',     ' Ambari ',     ' hive ',      ' Oozie ',     ' storm-etl ',     ' urun-sms ',      ' analyzer-etl ',     ' Zabbix ',     ' Zabbix-key ']def  Clean (db):    database =  ' {DBS} '. Format (dbs=db)     format  =  ' rm -rf % (path) s% (db) s.% ( Date) S.sql '     kv = {' path ': path,  ' date ': cleanstr,  ' db '  : database}    expireddbfile =  ('% (db) s.% ( Date) S.sql '  % kv)     if os.path.exists (expireddbfile):         msg =  ' start delete expired database file: % S '  %  (expireddbfile)         log (msg)          excute ( format % kv) def excute (cmd):     Os.system (CMD) def main ():     thread_list = []    dbs  = range (Len (DBS))     for i in dbs:         clean_t = thread (target=clean, args= (Dbs[i],))          thread_list.append (clean_t)         for i  In dbs:       &nbsP;thread_list[i].start ()         for i in dbs:                 thread_list[i].join ( ) if __name__ ==  "__main__":     main ()


MySQL backup script V2 (add log function and post-backup check)

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.