Skip to content

Commit 2abc8bb

Browse files
Write log if cannot get lock
1 parent a4373d1 commit 2abc8bb

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

mongodb_consistent_backup/Logger.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ def __init__(self, config, backup_time):
1616

1717
self.do_file_log = False
1818
if self.config.log_dir is not '':
19-
if os.path.isdir(self.config.log_dir):
20-
self.do_file_log = True
21-
else:
22-
print("ERROR: Log directory: %s does not exist! Skipping file-based logging" % self.config.log_dir)
19+
self.do_file_log = True
20+
if not os.path.isdir(self.config.log_dir):
21+
os.mkdir(self.config.log_dir)
2322

2423
self.log_format = '[%(asctime)s] [%(levelname)s] [%(processName)s] [%(module)s:%(funcName)s:%(lineno)d] %(message)s'
2524
self.file_log = None
@@ -35,15 +34,12 @@ def start(self):
3534
def start_file_logger(self):
3635
if self.do_file_log:
3736
try:
38-
if os.path.isdir(self.config.log_dir):
39-
os.mkdir(self.config.log_dir)
4037
self.current_log_file = os.path.join(self.config.log_dir, "backup.%s.log" % self.backup_name)
4138
self.backup_log_file = os.path.join(self.config.log_dir, "backup.%s.%s.log" % (self.backup_name, self.backup_time))
4239
self.file_log = logging.FileHandler(self.backup_log_file)
4340
self.file_log.setLevel(self.log_level)
4441
self.file_log.setFormatter(logging.Formatter(self.log_format))
4542
logging.getLogger('').addHandler(self.file_log)
46-
self.update_symlink()
4743
except OSError, e:
4844
logging.warning("Could not start file log handler, writing to stdout only")
4945
pass
@@ -52,18 +48,21 @@ def close(self):
5248
if self.file_log:
5349
self.file_log.close()
5450

55-
def compress(self):
51+
def compress(self, current=False):
5652
gz_log = None
5753
try:
58-
if not os.path.isfile(self.last_log) or self.last_log == self.backup_log_file:
59-
return
60-
logging.info("Compressing previous log file")
61-
gz_file = "%s.gz" % self.last_log
54+
compress_file = self.backup_log_file
55+
if not current:
56+
compress_file = self.last_log
57+
if not os.path.isfile(self.last_log) or self.last_log == self.backup_log_file:
58+
return
59+
logging.info("Compressing log file: %s" % compress_file)
60+
gz_file = "%s.gz" % compress_file
6261
gz_log = GzipFile(gz_file, "w+")
63-
with open(self.last_log) as f:
62+
with open(compress_file) as f:
6463
for line in f:
6564
gz_log.write(line)
66-
os.remove(self.last_log)
65+
os.remove(compress_file)
6766
finally:
6867
if gz_log:
6968
gz_log.close()

mongodb_consistent_backup/Main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def __init__(self, prog_name="mongodb-consistent-backup"):
5555
self.setup_logger()
5656
self.setup_signal_handlers()
5757
self.get_lock()
58-
self.logger.start_file_logger()
58+
self.logger.update_symlink()
5959
self.init()
6060
self.set_backup_dirs()
6161
self.get_db_conn()
@@ -74,6 +74,7 @@ def setup_logger(self):
7474
try:
7575
self.logger = Logger(self.config, self.backup_time)
7676
self.logger.start()
77+
self.logger.start_file_logger()
7778
except Exception, e:
7879
self.exception("Could not start logger: %s" % e, e)
7980

@@ -117,6 +118,7 @@ def get_lock(self):
117118
self.lock = Lock(self.config.lock_file)
118119
except Exception:
119120
logging.fatal("Could not acquire lock: '%s'! Is another %s process running? Exiting" % (self.config.lock_file, self.program_name))
121+
self.logger.compress(True)
120122
sys.exit(1)
121123

122124
def release_lock(self):

0 commit comments

Comments
 (0)