-
Notifications
You must be signed in to change notification settings - Fork 0
/
couchbase-migrator.py
executable file
·149 lines (120 loc) · 3.3 KB
/
couchbase-migrator.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
import sys
import getopt
import couchmigrator
#sources: json, csv, sqlite, couchdb, couchbase, membase, memcached, membase-sqlite
#dest: json (chunked), couchbase
def usage(err=None):
if err:
print "Error: %s\n" % err
r = 1
else:
r = 0
print """\
Syntax: couchbase-migrator [options]
Options:
-h, --help
-s <source>, --source=<source>
Data source that will be imported from
-d <destination>, --destination=<destination>
Data destination that will be exported to
"""
"""
-o, --overwrite
Overwrite any items that are in the destination
--dry-run
Do not write any data, just show what would be written
-v, --verbose
-q, --quiet
"""
print "Sources:"
for source in couchmigrator.sources:
print " " + source['type']
print " " + source['example']
print
print
print "Destinations:"
for destination in couchmigrator.destinations:
print " " + destination['type']
print " " + destination['example']
print
"""
Sources:
json
[json:]<filename>
csv
[csv:]<filename>
sqlite
[sqlite:]<filename>
couchdb
[couchdb:]example.com:5984/database
couchbase
[couchbase:]username:[email protected]:8091/bucket
membase
[membase:]username:[email protected]:8091/bucket
membase-sqlite
[membase-sqlite:]<dirname>
memcached
[memcached:]bucket:[email protected]:11211
Destinations:
json
[json:]<filename>
couchbase
[couchbase:]username:[email protected]:8091/bucket
"""
sys.exit(r)
class Config(object):
def __init__(self):
self.source = ''
self.destination = ''
self.overwrite = False
self.dryrun = False
self.verbose = False
self.quiet = False
def parse_args(argv):
config = Config()
try:
opts, args = getopt.getopt(argv[1:],
'hs:d:ovq', [
'help',
'source=',
'destination=',
'overwrite',
'dry-run',
'verbose',
'quiet',
])
for o, a in opts:
if o == '-h' or o == '--help':
usage()
elif o == '-s' or o == '--source':
config.source = a
elif o == '-d' or o == '--destination':
config.destination = a
elif o == '-o' or o == '--overwrite':
config.overwrite = True
elif o == '--dry-run':
config.dryrun = True
elif o == '-v' or o == '--verbose':
config.verbose = True
elif o == '-q' or o == '--quiet':
config.quiet = True
msg = ""
if not config.source or not config.destination:
usage("missing source or destination")
except IndexError:
usage()
except getopt.GetoptError, err:
usage(err)
return config
if __name__ == "__main__":
config = parse_args(sys.argv)
count = 0
reader = couchmigrator.reader(config.source)
writer = couchmigrator.writer(config.destination)
for record in reader:
writer.write(record)
count += 1
reader.close()
writer.close()
print 'migrated {0} items'.format(count)