MySQL backup script

Source: Internet
Author: User
Tags mysql backup timedelta

1. 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 threadhost =  ' 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 ']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}    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}     excute (format % kv) def excute (cmd):     os.system (cmd) def main ():     thread_list = []    for i in range (Len ( DBS)):         backup_t =&nbsp Thread (target=backup, args= (Dbs[i],))         clean_t =  Thread (target=clean, args= (Dbs[i],))         thread_list.append ( backup_t)         thread_list.append (clean_t)      For t in thread_list:        t.start ()          t.join () if __name__ ==  "__main__":     Main ()

3. Delete the backup 5 days ago

#!/usr/bin/env pythonimport osimport datetimefrom threading import threadpath  =  '/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}   &Nbsp;excute ( format % kv) def excute (cmd):     os.system (cmd) def  Main ():     thread_list = []    for i in range (Len (DBS)):         clean_t = thread (target=clean, args= (Dbs[i],))         thread_list.append (clean_t)     for t  in thread_list:        t.start ()          t.join () if __name__ ==  "__main__":     main ()

3. The backup SCP generated on the same day to another place

#!/usr/bin/env python#_*_coding:utf-8_*_ "" "@File:  backup_db.py@author: oldtan@email: [ Email protected] @Last  modified: 20180408 "" "import osimport datetimefrom  threading import threadremote_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  =  ' {DBS} '. Format (dbs=db) &Nbsp;   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}     excute ( format % kv) def excute (cmd):     os.system ( CMD) def main ():    thread_list = []    for i  In range (Len (DBS)):         scp_t = thread (target=scp_db _file, args= (Dbs[i],)         thread_list.append (scp_t)      for t in thread_list:        t.start ( )         t.join () if __name__ ==  "__main__":     main () 


MySQL backup script

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.