From 2abc8bb5374b9aea68858662ff1bfc4cdaaed6c8 Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Mon, 15 May 2017 18:57:39 +0200 Subject: [PATCH] Write log if cannot get lock --- mongodb_consistent_backup/Logger.py | 27 +++++++++++++-------------- mongodb_consistent_backup/Main.py | 4 +++- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/mongodb_consistent_backup/Logger.py b/mongodb_consistent_backup/Logger.py index 2f87b92a..ac2a91f0 100644 --- a/mongodb_consistent_backup/Logger.py +++ b/mongodb_consistent_backup/Logger.py @@ -16,10 +16,9 @@ def __init__(self, config, backup_time): self.do_file_log = False if self.config.log_dir is not '': - if os.path.isdir(self.config.log_dir): - self.do_file_log = True - else: - print("ERROR: Log directory: %s does not exist! Skipping file-based logging" % self.config.log_dir) + self.do_file_log = True + if not os.path.isdir(self.config.log_dir): + os.mkdir(self.config.log_dir) self.log_format = '[%(asctime)s] [%(levelname)s] [%(processName)s] [%(module)s:%(funcName)s:%(lineno)d] %(message)s' self.file_log = None @@ -35,15 +34,12 @@ def start(self): def start_file_logger(self): if self.do_file_log: try: - if os.path.isdir(self.config.log_dir): - os.mkdir(self.config.log_dir) self.current_log_file = os.path.join(self.config.log_dir, "backup.%s.log" % self.backup_name) self.backup_log_file = os.path.join(self.config.log_dir, "backup.%s.%s.log" % (self.backup_name, self.backup_time)) self.file_log = logging.FileHandler(self.backup_log_file) self.file_log.setLevel(self.log_level) self.file_log.setFormatter(logging.Formatter(self.log_format)) logging.getLogger('').addHandler(self.file_log) - self.update_symlink() except OSError, e: logging.warning("Could not start file log handler, writing to stdout only") pass @@ -52,18 +48,21 @@ def close(self): if self.file_log: self.file_log.close() - def compress(self): + def compress(self, current=False): gz_log = None try: - if not os.path.isfile(self.last_log) or self.last_log == self.backup_log_file: - return - logging.info("Compressing previous log file") - gz_file = "%s.gz" % self.last_log + compress_file = self.backup_log_file + if not current: + compress_file = self.last_log + if not os.path.isfile(self.last_log) or self.last_log == self.backup_log_file: + return + logging.info("Compressing log file: %s" % compress_file) + gz_file = "%s.gz" % compress_file gz_log = GzipFile(gz_file, "w+") - with open(self.last_log) as f: + with open(compress_file) as f: for line in f: gz_log.write(line) - os.remove(self.last_log) + os.remove(compress_file) finally: if gz_log: gz_log.close() diff --git a/mongodb_consistent_backup/Main.py b/mongodb_consistent_backup/Main.py index 479455ff..23f001db 100644 --- a/mongodb_consistent_backup/Main.py +++ b/mongodb_consistent_backup/Main.py @@ -55,7 +55,7 @@ def __init__(self, prog_name="mongodb-consistent-backup"): self.setup_logger() self.setup_signal_handlers() self.get_lock() - self.logger.start_file_logger() + self.logger.update_symlink() self.init() self.set_backup_dirs() self.get_db_conn() @@ -74,6 +74,7 @@ def setup_logger(self): try: self.logger = Logger(self.config, self.backup_time) self.logger.start() + self.logger.start_file_logger() except Exception, e: self.exception("Could not start logger: %s" % e, e) @@ -117,6 +118,7 @@ def get_lock(self): self.lock = Lock(self.config.lock_file) except Exception: logging.fatal("Could not acquire lock: '%s'! Is another %s process running? Exiting" % (self.config.lock_file, self.program_name)) + self.logger.compress(True) sys.exit(1) def release_lock(self):