forked from lalinsky/mbslave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbslave-import.py
executable file
·43 lines (37 loc) · 1.53 KB
/
mbslave-import.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
import tarfile
import sys
import os
from mbslave import Config, connect_db, parse_name, check_table_exists, fqn
def load_tar(filename, db, config, ignored_schemas, ignored_tables):
print "Importing data from", filename
tar = tarfile.open(filename, 'r:bz2')
cursor = db.cursor()
for member in tar:
if not member.name.startswith('mbdump/'):
continue
name = member.name.split('/')[1].replace('_sanitised', '')
schema, table = parse_name(config, name)
fulltable = fqn(schema, table)
if schema in ignored_schemas:
print " - Ignoring", name
continue
if table in ignored_tables:
print " - Ignoring", name
continue
if not check_table_exists(db, schema, table):
print " - Skipping %s (table %s does not exist)" % (name, fulltable)
continue
cursor.execute("SELECT 1 FROM %s LIMIT 1" % fulltable)
if cursor.fetchone():
print " - Skipping %s (table %s already contains data)" % (name, fulltable)
continue
print " - Loading %s to %s" % (name, fulltable)
cursor.copy_from(tar.extractfile(member), fulltable)
db.commit()
config = Config(os.path.dirname(__file__) + '/mbslave.conf')
db = connect_db(config)
ignored_schemas = set(config.get('schemas', 'ignore').split(','))
ignored_tables = set(config.get('TABLES', 'ignore').split(','))
for filename in sys.argv[1:]:
load_tar(filename, db, config, ignored_schemas, ignored_tables)