Advertisement
Guest User

Create an mysql database backup with transportable tablespac

a guest
Apr 21st, 2018
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.99 KB | None | 0 0
  1. #!/usr/bin/env python                                                                                                                                                                                                                        
  2. from __future__ import print_function                                                                                                                                                                                                        
  3. from multiprocessing import Process, Queue                                                                                                                                                                                                  
  4. import subprocess                                                                                                                                                                                                                            
  5. import mysql.connector                                                                                                                                                                                                                      
  6. import os.path                                                                                                                                                                                                                              
  7. import argparse                                                                                                                                                                                                                              
  8.                                                                                                                                                                                                                                              
  9. def dump_create_info(q, db):                                                                                                                                                                                                                
  10.     cmd = 'mysqldump --no-data --routines --triggers --loose-set-gtid-purged=OFF "{0}" > "{0}.struct.sql"'.format(db)                                                                                                                        
  11.     try:                                                                                                                                                                                                                                    
  12.         output = subprocess.check_output(                                                                                                                                                                                                    
  13.         cmd, stderr=subprocess.STDOUT, shell=True,                                                                                                                                                                                          
  14.         universal_newlines=True)                                                                                                                                                                                                            
  15.     except subprocess.CalledProcessError as exc:
  16.         msg = "Status : FAIL {0} {1}".format(exc.returncode, exc.output);
  17.     else:
  18.         msg = "Status : OK {}".format(output)
  19.     q.put(msg)
  20.  
  21. def copy_innodb_tables(q, path):
  22.     cmd = 'cp -a {0}/*.ibd {0}/*.cfg ./'.format(path)
  23.     try:
  24.         output = subprocess.check_output(
  25.         cmd, stderr=subprocess.STDOUT, shell=True,
  26.         universal_newlines=True)
  27.     except subprocess.CalledProcessError as exc:
  28.         msg = "Status : FAIL {0} {1}".format(exc.returncode, exc.output);
  29.     else:
  30.         msg = "Status : OK {}".format(output)
  31.     q.put(msg)
  32.  
  33. def get_list_of_innodb_tables(host, user, password, db):
  34.     cnx = mysql.connector.connect(user=user, host=host, password=password)
  35.     cursor = cnx.cursor()
  36.  
  37.     cursor.execute("select @@datadir");
  38.     datadir = '/var/lib/mysql/'
  39.     for (dbpath,) in cursor:
  40.         datadir = dbpath
  41.  
  42.     query = ("SELECT CONCAT('`', TABLE_SCHEMA, '`.`', TABLE_NAME, '`') tbl FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = %s AND ENGINE='InnoDB'")
  43.     cursor.execute(query,(db,))
  44.  
  45.     tables_list = []
  46.     for (tbl,) in cursor:
  47.         tables_list.append(tbl)
  48.  
  49.     lock_query = 'FLUSH TABLES {0} FOR EXPORT'.format(','.join(tables_list))
  50.     print(lock_query)
  51.     cursor.execute(lock_query)
  52.  
  53.     queue = Queue()
  54.     p = Process(target=copy_innodb_tables, args=(queue, os.path.join(datadir, db)))
  55.     p.start()
  56.    
  57.     p.join() # this blocks until the process terminates
  58.     result = queue.get()
  59.     print(result)
  60.     cursor.close()
  61.     cnx.close()
  62.  
  63.    
  64. if __name__ == '__main__':
  65.     parser = argparse.ArgumentParser(
  66.         description='Backup database using transportable tablespaces')
  67.     parser.add_argument('db', help='Database to backup')
  68.     parser.add_argument(
  69.         '-H',
  70.         '--host',
  71.         required=False,
  72.         help='mysql host name or ip')
  73.     parser.add_argument(
  74.         '-u',
  75.         '--user',
  76.         required=False,
  77.         help='mysql user name')
  78.     parser.add_argument(
  79.         '-p',
  80.         '--password',
  81.         required=False,
  82.         help='mysql password')
  83.  
  84.     host = 'localhost'
  85.     user = 'root'
  86.     password = ''
  87.     db = 'test'
  88.        
  89.     args = parser.parse_args()
  90.     if args.db is not None:
  91.         db = args.db
  92.     if args.host is not None:
  93.         host = args.host
  94.     if args.user is not None:
  95.         user = args.user
  96.     if args.password is not None:
  97.         password = args.password
  98.        
  99.  
  100.     queue = Queue()
  101.     p = Process(target=dump_create_info, args=(queue, db))
  102.     p.start()
  103.     p.join() # this blocks until the process terminates
  104.     result = queue.get()
  105.     print(result)
  106.     get_list_of_innodb_tables(host, user, password, db)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement