Skip to content

Commit 1fef902

Browse files
Merge pull request #84 from timvaillancourt/MCB_1.0-cleanup
Mcb 1.0 cleanup: changes to get backups running again
2 parents 9572bec + 64d79de commit 1fef902

File tree

16 files changed

+152
-96
lines changed

16 files changed

+152
-96
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
bin
22
build
33
rpmbuild
4+
tmp
45
*.pyc
56
.idea

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ rpm: clean
3434
rpmbuild -D "_topdir $(PWD)/rpmbuild" -D "version $(VERSION)" -bb rpmbuild/SPECS/$(NAME).spec
3535

3636
clean:
37-
rm -rf bin build rpmbuild $(NAME).egg-info 2>/dev/null
37+
rm -rf bin build rpmbuild $(NAME).egg-info tmp 2>/dev/null

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.0
1+
1.0.0-MCB_1.0

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: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,46 @@
11
import logging
22

3+
from Tar import Tar
4+
35

46
class Archive:
57
def __init__(self, config, backup_dir):
68
self.config = config
79
self.backup_dir = backup_dir
810

11+
self.method = None
912
self._archiver = None
1013
self.init()
1114

1215
def init(self):
1316
archive_method = self.config.archive.method
14-
if archive_method is None:
17+
if not archive_method or archive_method.lower() == "none":
1518
logging.info("Archiving disabled, skipping")
1619
else:
17-
config_vars = ""
18-
for key in self.config.archive:
19-
config_vars += "%s=%s," % (key, self.config.archive[key])
20-
logging.info("Using archiving method: %s (options: %s)" % archive_method, config_vars[:-1])
20+
self.method = archive_method.lower()
21+
logging.info("Using archiving method: %s" % self.method)
2122
try:
22-
self._archiver = globals()[archive_method](
23+
self._archiver = globals()[self.method.capitalize()](
2324
self.config,
2425
self.backup_dir
2526
)
2627
except Exception, e:
2728
raise e
2829

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+
2938
def archive(self):
3039
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])))
3144
return self._archiver.run()
3245

3346
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.compression
26-
self.thread_count = self.config.archive.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/Archive/Tar/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33

44
def config(parser):
5-
parser.add_argument("--archive.compression", dest="archive.compression",
5+
parser.add_argument("--archive.tar.compression", dest="archive.tar.compression",
66
help="Archiver compression method (default: gzip)", default='gzip', choices=['gzip', 'none'])
7+
parser.add_argument("--archive.tar.threads", dest="archive.tar.threads",
8+
help="Number of threads to use in archive phase (default: 1-per-CPU)", default=0, type=int)
79
return parser

mongodb_consistent_backup/Archive/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33

44
def config(parser):
55
parser.add_argument("--archive.method", dest="archive.method", help="Archiver method (default: tar)", default='tar', choices=['tar','none'])
6-
parser.add_argument("--archive.threads", dest="archive.threads", help="Number of threads to use in archive phase (default: 1-per-CPU)", default=0, type=int)
76
return parser

mongodb_consistent_backup/Backup/Backup.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import logging
22

3+
from Mongodump import Mongodump
4+
35

46
class Backup:
57
def __init__(self, config, backup_dir, secondaries, config_server=None):
@@ -13,18 +15,19 @@ def __init__(self, config, backup_dir, secondaries, config_server=None):
1315

1416
def init(self):
1517
backup_method = self.config.backup.method
16-
if backup_method is None:
18+
if not backup_method or backup_method.lower() == "none":
1719
raise Exception, 'Must specify a backup method!', None
18-
logging.info("Using backup method: %s" % backup_method)
20+
method = backup_method.lower()
21+
logging.info("Using backup method: %s" % method)
1922
try:
20-
self._method = globals()[backup_method](
23+
self._method = globals()[method.capitalize()](
2124
self.config,
2225
self.backup_dir,
2326
self.secondaries,
2427
self.config_server
2528
)
2629
except Exception, e:
27-
raise Exception, "Problem performing %s! Error: %s" % (backup_method, e), None
30+
raise Exception, "Problem performing %s! Error: %s" % (method, e), None
2831

2932
def is_compressed(self):
3033
if self._method:

mongodb_consistent_backup/Backup/Mongodump/Mongodump.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,16 @@ def run(self):
124124

125125
# start all threads and wait
126126
logging.info(
127-
"Starting backups using mongodump %s (inline gzip: %s, threads per dump: %i)" % (self.version, str(self.dump_gzip), self.threads_per_dump))
127+
"Starting backups using mongodump %s (inline gzip: %s, threads per dump: %i)" % (self.version, str(self.do_gzip), self.threads_per_dump))
128128
for thread in self.threads:
129129
thread.start()
130130
self.wait()
131131

132132
# backup a single non-replset config server, if exists:
133133
if not self.config_replset and isinstance(self.config_server, dict):
134134
logging.info("Using non-replset backup method for config server mongodump")
135-
config_server = self.config_server['host']
136-
if not ":" in config_server:
135+
config_server = self.config_server['host']
136+
if not ":" in config_server:
137137
config_server = config_server+":27019"
138138
self.threads = [MongodumpThread(
139139
self.response_queue,

0 commit comments

Comments
 (0)