Skip to content

Commit 52fc5d9

Browse files
Use local cache for pip+pex builds (faster), use methods to change config settings instead of directly changing vars in Main.py
1 parent b6b4223 commit 52fc5d9

File tree

9 files changed

+93
-53
lines changed

9 files changed

+93
-53
lines changed

conf/example.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ production:
1010
location: /opt/mongodb/backup
1111
mongodump:
1212
binary: /usr/bin/mongodump
13-
compression: gzip
13+
# compression: gzip
1414
replication:
1515
max_lag_secs: 5
1616
min_priority: 0
@@ -21,12 +21,13 @@ production:
2121
wait_secs: 300
2222
ping_secs: 3
2323
oplog:
24-
# compression: gzip
25-
# resolver_threads: 4
24+
#compression: gzip
25+
#resolver_threads: 4
2626
archive:
2727
method: tar
28-
compression: gzip
29-
# threads: 2
28+
#tar:
29+
# compression: gzip
30+
# threads: 2
3031
notify:
3132
method: none
3233
#nsca:

mongodb_consistent_backup/Archive/Archive.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,39 @@ def __init__(self, config, backup_dir):
88
self.config = config
99
self.backup_dir = backup_dir
1010

11+
self.method = None
1112
self._archiver = None
1213
self.init()
1314

1415
def init(self):
1516
archive_method = self.config.archive.method
16-
if not archive_method or archive_method.lower() is "none":
17+
if not archive_method or archive_method.lower() == "none":
1718
logging.info("Archiving disabled, skipping")
1819
else:
19-
config_vars = ""
20-
method = archive_method.lower()
21-
for key in self.config.archive:
22-
config_vars += "%s=%s," % (key, self.config.archive[key])
23-
logging.info("Using archiving method: %s (options: %s)" % (method, str(config_vars[:-1])))
20+
self.method = archive_method.lower()
21+
logging.info("Using archiving method: %s" % self.method)
2422
try:
25-
self._archiver = globals()[method.capitalize()](
23+
self._archiver = globals()[self.method.capitalize()](
2624
self.config,
2725
self.backup_dir
2826
)
2927
except Exception, e:
3028
raise e
3129

30+
def compression(self, method=None):
31+
if self._archiver:
32+
return self._archiver.compression(method)
33+
34+
def threads(self, threads=None):
35+
if self._archiver:
36+
return self._archiver.threads(threads)
37+
3238
def archive(self):
3339
if self._archiver:
40+
config_vars = ""
41+
for key in self.config.archive:
42+
config_vars += "%s=%s," % (key, self.config.archive[key])
43+
logging.info("Archiving with method: %s (options: %s)" % (self.method, str(config_vars[:-1])))
3444
return self._archiver.run()
3545

3646
def close(self):

mongodb_consistent_backup/Archive/Tar/Tar.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,44 @@ class Tar:
2222
def __init__(self, config, backup_base_dir):
2323
self.config = config
2424
self.backup_base_dir = backup_base_dir
25-
self.compression = self.config.archive.tar.compression
26-
self.thread_count = self.config.archive.tar.threads
2725
self.verbose = self.config.verbose
2826
self.binary = "tar"
27+
self.do_gzip = False
28+
self._pool = None
2929

30-
if self.thread_count is None or self.thread_count < 1:
31-
self.thread_count = cpu_count()
30+
def compression(self, method=None):
31+
if method:
32+
self.config.archive.tar.compression = method.lower()
33+
return self.config.archive.tar.compression
3234

35+
def threads(self, thread_count=None):
36+
if thread_count:
37+
self.config.archive.tar.threads = int(thread_count)
38+
logging.info("Setting tar thread count to: %i" % self.config.archive.tar.threads)
39+
if self.config.archive.tar.threads is None or self.config.archive.tar.threads < 1:
40+
self.config.archive.tar.threads = cpu_count()
41+
return int(self.config.archive.tar.threads)
42+
43+
def run(self):
3344
try:
34-
self._pool = Pool(processes=self.thread_count)
45+
thread_count = self.threads()
46+
self._pool = Pool(processes=thread_count)
47+
logging.info("Archiving backup directories with pool of %i thread(s)" % thread_count)
3548
except Exception, e:
3649
logging.fatal("Could not start pool! Error: %s" % e)
3750
raise e
3851

39-
def run(self):
40-
logging.info("Archiving backup directories with pool of %i thread(s)" % self.thread_count)
4152
if os.path.isdir(self.backup_base_dir):
4253
try:
4354
for backup_dir in os.listdir(self.backup_base_dir):
4455
subdir_name = "%s/%s" % (self.backup_base_dir, backup_dir)
4556
output_file = "%s.tar" % subdir_name
4657

47-
do_gzip = False
48-
if self.compression == "gzip":
49-
output_file = "%s.tgz" % subdir_name
50-
do_gzip = True
58+
if self.compression() == 'gzip':
59+
output_file = "%s.tgz" % subdir_name
60+
self.do_gzip = True
5161

52-
self._pool.apply_async(TarThread(subdir_name, output_file, do_gzip, self.verbose, self.binary).run)
62+
self._pool.apply_async(TarThread(subdir_name, output_file, self.do_gzip, self.verbose, self.binary).run)
5363
except Exception, e:
5464
self._pool.terminate()
5565
logging.fatal("Could not create archiving thread! Error: %s" % e)
@@ -59,8 +69,8 @@ def run(self):
5969
logging.info("Archiver threads completed")
6070

6171
def close(self):
62-
logging.info("Killing all Archiver threads...")
72+
logging.debug("Stopping Archiver threads")
6373
if self._pool is not None:
6474
self._pool.terminate()
6575
self._pool.join()
66-
logging.info("Killed all Archiver threads")
76+
logging.info("Stopped all Archiver threads")

mongodb_consistent_backup/Backup/Backup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def __init__(self, config, backup_dir, secondaries, config_server=None):
1515

1616
def init(self):
1717
backup_method = self.config.backup.method
18-
if not backup_method or backup_method.lower() is "none":
18+
if not backup_method or backup_method.lower() == "none":
1919
raise Exception, 'Must specify a backup method!', None
2020
method = backup_method.lower()
2121
logging.info("Using backup method: %s" % method)

mongodb_consistent_backup/Main.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,18 @@ def run(self):
152152
except Exception, e:
153153
raise e
154154

155+
# Setup the archiver
156+
try:
157+
self.archive = Archive(
158+
self.config,
159+
self.backup_root_directory,
160+
)
161+
except Exception, e:
162+
raise e
163+
155164
if not self.is_sharded:
156165
logging.info("Running backup of %s:%s in replset mode" % (self.config.host, self.config.port))
157166

158-
self.config.archive.threads = 1
159-
160167
# get shard secondary
161168
try:
162169
self.replset = Replset(
@@ -179,12 +186,13 @@ def run(self):
179186
)
180187
self.backup.backup()
181188
if self.backup.is_compressed():
182-
logging.info("Backup method supports gzip compression, setting config overrides: { archive.compression: 'none' }")
183-
self.config.archive.compression = 'none'
184-
self.config.oplog.compression = 'gzip'
189+
logging.info("Backup method supports gzip compression, disabling compression in archive step")
190+
self.archive.compression('none')
185191
except Exception, e:
186192
self.exception("Problem performing replset mongodump! Error: %s" % e)
187193

194+
# use 1 archive thread for single replset
195+
self.archive.threads(1)
188196
else:
189197
logging.info("Running backup of %s:%s in sharded mode" % (self.config.host, self.config.port))
190198

@@ -215,6 +223,16 @@ def run(self):
215223
except Exception, e:
216224
self.exception("Problem stopping the balancer! Error: %s" % e)
217225

226+
# init the oplogtailers
227+
try:
228+
self.oplogtailer = Tailer(
229+
self.config,
230+
self.secondaries,
231+
self.backup_root_directory
232+
)
233+
except Exception, e:
234+
self.exception("Problem initializing oplog tailer! Error: %s" % e)
235+
218236
# init the backup
219237
try:
220238
self.backup = Backup(
@@ -224,19 +242,14 @@ def run(self):
224242
self.sharding.get_config_server()
225243
)
226244
if self.backup.is_compressed():
227-
logging.info("Backup method supports gzip compression, setting config overrides: { archive.compression: 'none', oplog.compression: 'gzip' }")
228-
self.config.archive.compression = 'none'
229-
self.config.oplog.compression = 'gzip'
245+
logging.info("Backup method supports gzip compression, disabling compression in archive step and enabling oplog compression")
246+
self.archive.compression('none')
247+
self.oplogtailer.compression('gzip')
230248
except Exception, e:
231249
self.exception("Problem initializing backup! Error: %s" % e)
232250

233-
# start the oplog tailer(s)
251+
# start the oplog tailers, before the backups start
234252
try:
235-
self.oplogtailer = Tailer(
236-
self.config,
237-
self.secondaries,
238-
self.backup_root_directory
239-
)
240253
self.oplogtailer.run()
241254
except Exception, e:
242255
self.exception("Failed to start oplog tailing threads! Error: %s" % e)
@@ -264,10 +277,6 @@ def run(self):
264277

265278
# archive backup directories
266279
try:
267-
self.archive = Archive(
268-
self.config,
269-
self.backup_root_directory,
270-
)
271280
self.archive.archive()
272281
except Exception, e:
273282
self.exception("Problem performing archiving! Error: %s" % e)

mongodb_consistent_backup/Notify/Notify.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def __init__(self, config):
99

1010
def init(self):
1111
notify_method = self.config.notify.method
12-
if not notify_method or notify_method.lower() is "none":
12+
if not notify_method or notify_method.lower() == "none":
1313
logging.info("Notifying disabled, skipping")
1414
else:
1515
method = notify_method.lower()

mongodb_consistent_backup/Oplog/Tailer/Tailer.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,20 @@ def __init__(self, config, secondaries, base_dir):
1616
self.password = self.config.password
1717
self.authdb = self.config.authdb
1818

19-
self.dump_gzip = False
20-
if self.config.oplog.compression == 'gzip':
21-
self.dump_gzip = True
22-
2319
self.response_queue = Queue()
2420
self.threads = []
2521
self._summary = {}
2622

23+
def compression(self, method=None):
24+
if method:
25+
self.config.oplog.compression = method.lower()
26+
return self.config.oplog.compression
27+
28+
def do_gzip(self):
29+
if self.compression() == 'gzip':
30+
return True
31+
return False
32+
2733
def summary(self):
2834
return self._summary
2935

@@ -38,7 +44,7 @@ def run(self):
3844
self.base_dir,
3945
host,
4046
port,
41-
self.dump_gzip,
47+
self.do_gzip(),
4248
self.user,
4349
self.password,
4450
self.authdb

mongodb_consistent_backup/Upload/Upload.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def __init__(self, config, base_dir, backup_dir):
1414

1515
def init(self):
1616
upload_method = self.config.upload.method
17-
if not upload_method or upload_method.lower() is "none":
17+
if not upload_method or upload_method.lower() == "none":
1818
logging.info("Uploading disabled, skipping")
1919
else:
2020
#TODO Remove this line and move to S3 Lib for checking

scripts/build.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,12 @@ if [ -d ${srcdir} ]; then
9696
exit 1
9797
fi
9898

99+
if [ ! -d ${pexdir} ]; then
100+
mkdir -p ${pexdir}
101+
else
102+
rm -f ${pexdir}/build/mongodb_consistent_backup-*
103+
fi
99104
[ ! -d ${bindir} ] && mkdir -p ${bindir}
100-
[ ! -d ${pexdir} ] && mkdir -p ${pexdir}
101105
${venvdir}/bin/python2.7 ${venvdir}/bin/pex -o ${output_file} -m ${mod_name} -r ${require_file} --pex-root=${pexdir} ${builddir}
102106
if [ $? -lt 1 ] && [ -x ${output_file} ]; then
103107
echo "pex executable written to '$output_file'"

0 commit comments

Comments
 (0)