Skip to content

Commit 629de30

Browse files
Prevent concurrent Zbackups with a lock
1 parent d1254e5 commit 629de30

File tree

1 file changed

+13
-1
lines changed
  • mongodb_consistent_backup/Archive/Zbackup

1 file changed

+13
-1
lines changed

mongodb_consistent_backup/Archive/Zbackup/Zbackup.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from select import select
55
from subprocess import Popen, PIPE, call
66

7+
from mongodb_consistent_backup.Common import Lock
78
from mongodb_consistent_backup.Errors import OperationError
89
from mongodb_consistent_backup.Pipeline import Task
910

@@ -24,7 +25,9 @@ def __init__(self, manager, config, timer, base_dir, backup_dir, **kwargs):
2425
self.compression_method = 'lzma'
2526
self.compression_supported = ['lzma']
2627

27-
self.zbackup_dir = os.path.join(self.config.backup.location, self.backup_name, "mongodb-consistent-backup_zbackup")
28+
self.base_dir = os.path.join(self.config.backup.location, self.backup_name)
29+
self.zbackup_dir = os.path.join(self.base_dir, "mongodb-consistent-backup_zbackup")
30+
self.zbackup_lock = os.path.join(self.base_dir, "mongodb-consistent-backup_zbackup.lock")
2831
self.zbackup_backups = os.path.join(self.zbackup_dir, "backups")
2932
self.zbackup_backup_path = os.path.join(self.zbackup_backups, "%s.tar" % self.backup_time)
3033
self.zbackup_bundles = os.path.join(self.zbackup_dir, "bundles")
@@ -50,6 +53,10 @@ def init(self):
5053
else:
5154
raise OperationError("ZBackup dir: %s is not a zbackup storage directory!" % self.zbackup_dir)
5255
else:
56+
if not os.path.isdir(self.base_dir):
57+
os.makedirs(self.base_dir)
58+
lock = Lock(self.zbackup_lock)
59+
lock.acquire()
5360
try:
5461
cmd_line = [self.zbackup_binary]
5562
if self.zbackup_passwd_file:
@@ -65,6 +72,8 @@ def init(self):
6572
raise OperationError("ZBackup initialization failed! Exit code: %i" % exit_code)
6673
except Exception, e:
6774
raise OperationError("Error creating ZBackup storage directory! Error: %s" % e)
75+
finally:
76+
lock.release()
6877

6978
def version(self):
7079
if self._version:
@@ -148,6 +157,8 @@ def get_commands(self, base_dir, sub_dir):
148157

149158
def run(self):
150159
if self.has_zbackup():
160+
lock = Lock(self.zbackup_lock)
161+
lock.acquire()
151162
try:
152163
logging.info("Starting ZBackup version: %s (options: compression=%s, encryption=%s, threads=%i, cache_mb=%i)" %
153164
(self.version(), self.compression(), self.encrypted, self.threads(), self.zbackup_cache_mb)
@@ -171,5 +182,6 @@ def run(self):
171182
finally:
172183
self.running = False
173184
self.stopped = True
185+
lock.release()
174186
else:
175187
raise OperationError("Cannot find ZBackup at %s!" % self.zbackup_binary)

0 commit comments

Comments
 (0)