Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from __future__ import print_function
- from multiprocessing import Process, Queue
- import subprocess
- import mysql.connector
- import os.path
- import argparse
- def dump_create_info(q, db):
- cmd = 'mysqldump --no-data --routines --triggers --loose-set-gtid-purged=OFF "{0}" > "{0}.struct.sql"'.format(db)
- try:
- output = subprocess.check_output(
- cmd, stderr=subprocess.STDOUT, shell=True,
- universal_newlines=True)
- except subprocess.CalledProcessError as exc:
- msg = "Status : FAIL {0} {1}".format(exc.returncode, exc.output);
- else:
- msg = "Status : OK {}".format(output)
- q.put(msg)
- def copy_innodb_tables(q, path):
- cmd = 'cp -a {0}/*.ibd {0}/*.cfg ./'.format(path)
- try:
- output = subprocess.check_output(
- cmd, stderr=subprocess.STDOUT, shell=True,
- universal_newlines=True)
- except subprocess.CalledProcessError as exc:
- msg = "Status : FAIL {0} {1}".format(exc.returncode, exc.output);
- else:
- msg = "Status : OK {}".format(output)
- q.put(msg)
- def get_list_of_innodb_tables(host, user, password, db):
- cnx = mysql.connector.connect(user=user, host=host, password=password)
- cursor = cnx.cursor()
- cursor.execute("select @@datadir");
- datadir = '/var/lib/mysql/'
- for (dbpath,) in cursor:
- datadir = dbpath
- query = ("SELECT CONCAT('`', TABLE_SCHEMA, '`.`', TABLE_NAME, '`') tbl FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = %s AND ENGINE='InnoDB'")
- cursor.execute(query,(db,))
- tables_list = []
- for (tbl,) in cursor:
- tables_list.append(tbl)
- lock_query = 'FLUSH TABLES {0} FOR EXPORT'.format(','.join(tables_list))
- print(lock_query)
- cursor.execute(lock_query)
- queue = Queue()
- p = Process(target=copy_innodb_tables, args=(queue, os.path.join(datadir, db)))
- p.start()
- p.join() # this blocks until the process terminates
- result = queue.get()
- print(result)
- cursor.close()
- cnx.close()
- if __name__ == '__main__':
- parser = argparse.ArgumentParser(
- description='Backup database using transportable tablespaces')
- parser.add_argument('db', help='Database to backup')
- parser.add_argument(
- '-H',
- '--host',
- required=False,
- help='mysql host name or ip')
- parser.add_argument(
- '-u',
- '--user',
- required=False,
- help='mysql user name')
- parser.add_argument(
- '-p',
- '--password',
- required=False,
- help='mysql password')
- host = 'localhost'
- user = 'root'
- password = ''
- db = 'test'
- args = parser.parse_args()
- if args.db is not None:
- db = args.db
- if args.host is not None:
- host = args.host
- if args.user is not None:
- user = args.user
- if args.password is not None:
- password = args.password
- queue = Queue()
- p = Process(target=dump_create_info, args=(queue, db))
- p.start()
- p.join() # this blocks until the process terminates
- result = queue.get()
- print(result)
- get_list_of_innodb_tables(host, user, password, db)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement