From 33558610a6fbc5413e367db8f99fa1bac23c5355 Mon Sep 17 00:00:00 2001 From: brent s Date: Tue, 24 Oct 2017 06:04:54 -0400 Subject: [PATCH] fix for sksdump and adding journald support for the backup script --- gpg/sksdump.py | 5 ++++- storage/backups/borg/backup2.py | 28 ++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/gpg/sksdump.py b/gpg/sksdump.py index 51b8d65..771211a 100755 --- a/gpg/sksdump.py +++ b/gpg/sksdump.py @@ -120,7 +120,10 @@ def destPrep(args): _dir = os.path.join(thisdir, d) if os.path.isdir(_dir): if len(os.listdir(_dir)) == 0: - os.rmdir(os.path.join(thisdir, d)) + try: + os.rmdir(os.path.join(thisdir, d)) + except NotADirectoryError: + pass # in case it grabs the "current" symlink #try: # os.removedirs(sks['destdir']) # Remove empty dirs #except: diff --git a/storage/backups/borg/backup2.py b/storage/backups/borg/backup2.py index a34c5a3..98b8f60 100755 --- a/storage/backups/borg/backup2.py +++ b/storage/backups/borg/backup2.py @@ -35,23 +35,38 @@ loglvls = {'critical': logging.CRITICAL, class Backup(object): def __init__(self, args): self.args = args - # Set up logging + ### LOGGING ### + # Thanks to: + # https://web.archive.org/web/20170726052946/http://www.lexev.org/en/2013/python-logging-every-day/ + # https://stackoverflow.com/a/42604392 + # https://plumberjack.blogspot.com/2010/10/supporting-alternative-formatting.html + # and user K900_ on r/python for entertaining my very silly question. + self.logger = logging.getLogger(__name__) + self.logger.setLevel(loglvls[self.args['loglevel']]) _logfmt = logging.Formatter(fmt = '{levelname}:{name}: {message} ({asctime}; {filename}:{lineno})', style = '{', datefmt = '%Y-%m-%d %H:%M:%S') + _journalfmt = logging.Formatter(fmt = '{levelname}:{name}: {message} ({filename}:{lineno})', + style = '{', + datefmt = '%Y-%m-%d %H:%M:%S') handlers = [] if self.args['disklog']: handlers.append(logging.handlers.RotatingFileHandler(self.args['logfile'], encoding = 'utf8', maxBytes = 100000, backupCount = 1)) - handlers.append(logging.StreamHandler()) - self.logger = logging.getLogger(__name__) - self.logger.setLevel(logging.DEBUG) + if self.args['verbose']: + handlers.append(logging.StreamHandler()) + if has_systemd: + handlers.append(journal.JournalHandler()) for h in handlers: h.setFormatter(_logfmt) h.setLevel(loglvls[self.args['loglevel']]) self.logger.addHandler(h) + ### CONFIG ### + if not os.path.isfile(self.args['cfgfile']): + self.logger.error('{0} does not exist'.format(self.args['cfgfile'])) + exit(1) def create(self): pass @@ -113,6 +128,11 @@ def parseArgs(): default = _logfile, help = ('The path to the logfile, only used if -Ld/--log-to-disk ' + 'is specified. Default: \033[1m{0}\033[0m (dynamic)').format(_logfile)) + args.add_argument('-v', '--verbose', + dest = 'verbose', + action = 'store_true', + help = ('If specified, log messages will be printed to STDOUT/STDERR ' + + 'in addition to the other configured log systems.')) ### ARGS FOR ALL OPERATIONS ### commonargs = argparse.ArgumentParser(add_help = False) commonargs.add_argument('-r', '--repo',