Skip to content

Commit c6b3d55

Browse files
Merge pull request #94 from timvaillancourt/MCB_1.0-cleanup_v7
Mcb 1.0 cleanup v7
2 parents 187498f + a63998d commit c6b3d55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1900
-1171
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33

44
NAME=mongodb_consistent_backup
5-
VERSION=$(shell cat VERSION)
5+
VERSION=$(shell cat VERSION | cut -d- -f1)
66
PREFIX?=/usr/local
77
BASEDIR?=$(DESTDIR)$(PREFIX)
88
BINDIR?=$(BASEDIR)/bin
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
production:
22
host: localhost
33
port: 27017
4-
#user:
4+
#username:
55
#password:
66
#authdb: admin
77
backup:
@@ -10,12 +10,12 @@ production:
1010
location: /opt/mongodb/backup
1111
mongodump:
1212
binary: /usr/bin/mongodump
13-
# compression: gzip
13+
# compression: gzip (default: true - if supported)
1414
replication:
1515
max_lag_secs: 5
1616
min_priority: 0
1717
max_priority: 1000
18-
use_hidden: false
18+
hidden_only: false
1919
sharding:
2020
balancer:
2121
wait_secs: 300
@@ -31,16 +31,16 @@ production:
3131
notify:
3232
method: none
3333
#nsca:
34-
# server:
35-
# password:
36-
# check_host:
34+
# server: localhost:5667
35+
# password: secret
36+
# check_host: localhost
3737
# check_name: mongodb_consistent_backup
3838
upload:
3939
method: none
40+
remove_uploaded: false
4041
#s3:
41-
# access_key:
42-
# secret_key:
43-
# bucket_name:
42+
# access_key: <AWS S3 Access Key>
43+
# secret_key: <AWS S3 Secret Key>
44+
# bucket_name: <AWS S3 Bucket Name>
4445
# bucket_prefix: /
45-
# remove_uploaded: true
4646
# threads: 4
Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,9 @@
1-
import logging
1+
from mongodb_consistent_backup.Archive.Tar import Tar
2+
from mongodb_consistent_backup.Pipeline import Stage
23

3-
from Tar import Tar
4-
from mongodb_consistent_backup.Common import Timer, config_to_string, parse_method
54

6-
7-
class Archive:
8-
def __init__(self, config, backup_dir):
9-
self.config = config
10-
self.backup_dir = backup_dir
11-
12-
self.method = None
13-
self._archiver = None
14-
self.timer = Timer()
5+
class Archive(Stage):
6+
def __init__(self, manager, config, timer, base_dir, backup_dir):
7+
super(Archive, self).__init__(self.__class__.__name__, manager, config, timer, base_dir, backup_dir)
8+
self.task = self.config.archive.method
159
self.init()
16-
17-
def init(self):
18-
archive_method = self.config.archive.method
19-
if not archive_method or parse_method(archive_method) == "none":
20-
logging.info("Archiving disabled, skipping")
21-
else:
22-
self.method = parse_method(archive_method)
23-
logging.info("Using archiving method: %s" % self.method)
24-
try:
25-
self._archiver = globals()[self.method.capitalize()](
26-
self.config,
27-
self.backup_dir
28-
)
29-
except LookupError, e:
30-
raise Exception, 'No archiving method: %s' % self.method, None
31-
except Exception, e:
32-
raise e
33-
34-
def compression(self, method=None):
35-
if self._archiver:
36-
return self._archiver.compression(method)
37-
38-
def threads(self, threads=None):
39-
if self._archiver:
40-
return self._archiver.threads(threads)
41-
42-
def archive(self):
43-
if self._archiver:
44-
config_string = config_to_string(self.config.archive[self.method])
45-
logging.info("Archiving with method: %s (options: %s)" % (self.method, config_string))
46-
self.timer.start()
47-
48-
self._archiver.run()
49-
50-
self.timer.stop()
51-
logging.info("Archiving completed in %s seconds" % self.timer.duration())
52-
53-
def close(self):
54-
if self._archiver:
55-
return self._archiver.close()

mongodb_consistent_backup/Archive/Tar/Tar.py

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
import logging
33

44
from copy_reg import pickle
5-
from multiprocessing import Pool, cpu_count
5+
from multiprocessing import Pool
66
from types import MethodType
77

88
from TarThread import TarThread
99
from mongodb_consistent_backup.Common import parse_method
10+
from mongodb_consistent_backup.Errors import Error
11+
from mongodb_consistent_backup.Pipeline import Task
1012

1113

1214
# Allows pooled .apply_async()s to work on Class-methods:
@@ -19,32 +21,13 @@ def _reduce_method(m):
1921
pickle(MethodType, _reduce_method)
2022

2123

22-
class Tar:
23-
def __init__(self, config, backup_base_dir):
24-
self.config = config
25-
self.backup_base_dir = backup_base_dir
26-
self.verbose = self.config.verbose
27-
self.binary = "tar"
28-
self._pool = None
24+
class Tar(Task):
25+
def __init__(self, manager, config, timer, base_dir, backup_dir, **kwargs):
26+
super(Tar, self).__init__(self.__class__.__name__, manager, config, timer, base_dir, backup_dir, **kwargs)
27+
self.compression_method = self.config.archive.tar.compression
28+
self.binary = "tar"
2929

30-
def compression(self, method=None):
31-
if method:
32-
self.config.archive.tar.compression = parse_method(method)
33-
logging.info("Setting tar compression method to: %s" % self.config.archive.tar.compression)
34-
return parse_method(self.config.archive.tar.compression)
35-
36-
def do_gzip(self):
37-
if self.compression() == 'gzip':
38-
return True
39-
return False
40-
41-
def threads(self, thread_count=None):
42-
if thread_count:
43-
self.config.archive.tar.threads = int(thread_count)
44-
logging.info("Setting tar thread count to: %i" % self.config.archive.tar.threads)
45-
if self.config.archive.tar.threads is None or self.config.archive.tar.threads < 1:
46-
self.config.archive.tar.threads = cpu_count()
47-
return int(self.config.archive.tar.threads)
30+
self._pool = None
4831

4932
def run(self):
5033
try:
@@ -53,28 +36,33 @@ def run(self):
5336
logging.info("Archiving backup directories with pool of %i thread(s)" % thread_count)
5437
except Exception, e:
5538
logging.fatal("Could not start pool! Error: %s" % e)
56-
raise e
39+
raise Error(e)
5740

58-
if os.path.isdir(self.backup_base_dir):
41+
if os.path.isdir(self.backup_dir):
5942
try:
60-
for backup_dir in os.listdir(self.backup_base_dir):
61-
subdir_name = "%s/%s" % (self.backup_base_dir, backup_dir)
43+
self.running = True
44+
for backup_dir in os.listdir(self.backup_dir):
45+
subdir_name = os.path.join(self.backup_dir, backup_dir)
46+
if not os.path.isdir(os.path.join(subdir_name, "dump")):
47+
continue
6248
output_file = "%s.tar" % subdir_name
63-
6449
if self.do_gzip():
6550
output_file = "%s.tgz" % subdir_name
66-
6751
self._pool.apply_async(TarThread(subdir_name, output_file, self.do_gzip(), self.verbose, self.binary).run)
6852
except Exception, e:
6953
self._pool.terminate()
7054
logging.fatal("Could not create tar archiving thread! Error: %s" % e)
71-
raise e
72-
self._pool.close()
73-
self._pool.join()
55+
raise Error(e)
56+
finally:
57+
self._pool.close()
58+
self._pool.join()
59+
self.stopped = True
60+
self.completed = True
7461

7562
def close(self):
7663
logging.debug("Stopping tar archiving threads")
77-
if self._pool is not None:
64+
if not self.stopped and self._pool is not None:
7865
self._pool.terminate()
7966
self._pool.join()
80-
logging.info("Stopped all tar archiving threads")
67+
logging.info("Stopped all tar archiving threads")
68+
self.stopped = True

mongodb_consistent_backup/Archive/Tar/TarThread.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import logging
3+
import sys
34

45
from signal import signal, SIGINT, SIGTERM
56

@@ -30,26 +31,22 @@ def run(self):
3031
if os.path.isdir(self.backup_dir):
3132
if not os.path.isfile(self.output_file):
3233
try:
33-
backup_base_dir = os.path.dirname(self.backup_dir)
34-
backup_base_name = os.path.basename(self.backup_dir)
35-
36-
log_msg = "Archiving and compressing directory: %s" % self.backup_dir
37-
cmd_flags = ["-C", backup_base_dir, "-cf", self.output_file, "--remove-files", backup_base_name]
38-
39-
if self.do_gzip:
40-
log_msg = "Archiving directory: %s" % self.backup_dir
41-
cmd_flags = ["-C", backup_base_dir, "-czf", self.output_file, "--remove-files", backup_base_name]
42-
43-
try:
44-
logging.info(log_msg)
45-
self._command = LocalCommand(self.binary, cmd_flags, self.verbose)
46-
self._command.run()
47-
except Exception, e:
48-
raise e
34+
backup_base_dir = os.path.dirname(self.backup_dir)
35+
backup_base_name = os.path.basename(self.backup_dir)
36+
37+
log_msg = "Archiving and compressing directory: %s" % self.backup_dir
38+
cmd_flags = ["-C", backup_base_dir, "-cf", self.output_file, "--remove-files", backup_base_name]
39+
40+
if self.do_gzip:
41+
log_msg = "Archiving directory: %s" % self.backup_dir
42+
cmd_flags = ["-C", backup_base_dir, "-czf", self.output_file, "--remove-files", backup_base_name]
43+
44+
logging.info(log_msg)
45+
self._command = LocalCommand(self.binary, cmd_flags, self.verbose)
46+
self._command.run()
4947
except Exception, e:
5048
logging.fatal("Failed archiving file: %s! Error: %s" % (self.output_file, e))
51-
raise e
52-
elif os.path.isfile(self.output_file):
49+
sys.exit(1)
50+
else:
5351
logging.fatal("Output file: %s already exists!" % self.output_file)
54-
raise Exception, "Output file %s already exists!" % self.output_file, None
55-
52+
sys.exit(1)
Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,9 @@
1-
import logging
1+
from mongodb_consistent_backup.Backup.Mongodump import Mongodump
2+
from mongodb_consistent_backup.Pipeline import Stage
23

3-
from Mongodump import Mongodump
4-
from mongodb_consistent_backup.Common import Timer, config_to_string, parse_method
54

6-
7-
class Backup:
8-
def __init__(self, config, backup_dir, secondaries, config_server=None):
9-
self.config = config
10-
self.backup_dir = backup_dir
11-
self.secondaries = secondaries
12-
self.config_server = config_server
13-
14-
self.method = None
15-
self._method = None
16-
self.timer = Timer()
5+
class Backup(Stage):
6+
def __init__(self, manager, config, timer, base_dir, backup_dir, replsets, sharding=None):
7+
super(Backup, self).__init__(self.__class__.__name__, manager, config, timer, base_dir, backup_dir, replsets=replsets, sharding=sharding)
8+
self.task = self.config.backup.method
179
self.init()
18-
19-
def init(self):
20-
backup_method = self.config.backup.method
21-
if not backup_method or parse_method(backup_method) == "none":
22-
raise Exception, 'Must specify a backup method!', None
23-
self.method = parse_method(backup_method)
24-
try:
25-
self._method = globals()[self.method.capitalize()](
26-
self.config,
27-
self.backup_dir,
28-
self.secondaries,
29-
self.config_server
30-
)
31-
except LookupError, e:
32-
raise Exception, 'No backup method: %s' % self.method, None
33-
except Exception, e:
34-
raise Exception, "Problem performing %s! Error: %s" % (self.method, e), None
35-
36-
def is_compressed(self):
37-
if self._method:
38-
return self._method.is_compressed()
39-
40-
def backup(self):
41-
if self._method:
42-
config_string = config_to_string(self.config.backup[self.method])
43-
logging.info("Using backup method: %s (options: %s)" % (self.method, config_string))
44-
self.timer.start()
45-
46-
info = self._method.run()
47-
48-
self.timer.stop()
49-
logging.info("Backup completed in %s seconds" % self.timer.duration())
50-
51-
return info
52-
53-
def close(self):
54-
if self._method:
55-
return self._method.close()

0 commit comments

Comments
 (0)