Guest User

Untitled

a guest
Jun 20th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import sys
  3.  
  4. # create mysqldump commands to create multiple sql files for a single (large) table
  5. # specify dump_file_count for desired number of files
  6.  
  7. db_host = 'HOST'
  8. db_user = 'USER'
  9. db_name = 'DBNAME'
  10. db_pass = 'PASS'
  11. db_table = 'TABLE'
  12. table_id = 'id' # primary key column for this table
  13. dump_file_count = 10 # desired number of dump files
  14. # max_id = 0 means we query the DB and generate equal-size sql files
  15. # specify max ID to avoid MySQL query, but sql files may vary in size
  16. # it should be reasonably close to max PRIMARY KEY id but need not be exact
  17. max_id = 0
  18.  
  19. try:
  20. import MySQLdb
  21. introspect_db = True
  22. except ModuleNotFoundError:
  23. introspect_db = False
  24.  
  25. mysqldump = f'mysqldump --opt --order-by-primary --compress -h {db_host} -u {db_user} -p{db_pass}'
  26.  
  27. if max_id:
  28. # there may be gaps in IDs so sql files may not have equal numbers of INSERTs
  29. chunk_size = int(max_id / dump_file_count)
  30. chunks = list(range(0, max_id, chunk_size))
  31. else:
  32. if not introspect_db:
  33. print("Unable to load python MySQLdb module")
  34. print("Either specify max_id or install the module")
  35. print("Possibly install with:")
  36. print("pip install --user mysqlclient ")
  37. sys.exit(1)
  38. # else
  39. db = MySQLdb.connect(host=db_host, db=db_name, user=db_user, passwd=db_pass).cursor()
  40. db.execute(f'SELECT {table_id} FROM {db_table} ORDER BY {table_id}')
  41. row_ids = [x[0] for x in db.fetchall()]
  42. row_count = len(row_ids)
  43. chunk_size = int(row_count / dump_file_count)
  44. chunks = [0]
  45. try:
  46. # put equal numbers of INSERTs in each file
  47. i = 1
  48. while True:
  49. chunks.append(row_ids[i * chunk_size])
  50. i += 1
  51. except IndexError:
  52. chunks.append(row_ids[-1])
  53. print('# %s rows inserted per file' % (chunk_size,))
  54.  
  55. for i in range(len(chunks)):
  56. no_create = ''
  57. if i:
  58. no_create = '--skip-add-drop-table --no-create-info'
  59. file_count = str(i).zfill(4)
  60. start = chunks[i]
  61. try:
  62. end = chunks[i+1]
  63. id_range = f'-w"{db_table}.{table_id}>={start} AND {db_table}.{table_id}<{end}"'
  64. except IndexError:
  65. # include new rows inserted since our db query
  66. id_range = f'-w"{db_table}.{table_id}>={end}"'
  67. print(f'{mysqldump} {no_create} {id_range} -r {db_table}.{file_count}.sql {db_name} {db_table}')
Add Comment
Please, Sign In to add comment