logging added, needs to be modified to write to journald

This commit is contained in:
brent s 2017-10-24 04:57:15 -04:00
parent 6801047d0a
commit 3bcdb408a1

View File

@ -9,6 +9,7 @@ import configparser
import datetime import datetime
import json import json
import logging import logging
import logging.handlers
import os import os
import subprocess import subprocess
import sys import sys
@ -17,6 +18,11 @@ try:
has_mysql = True has_mysql = True
except ImportError: except ImportError:
has_mysql = False has_mysql = False
try:
from systemd import journal
has_systemd = True
except ImportError:
has_systemd = False


### LOG LEVEL MAPPINGS ### ### LOG LEVEL MAPPINGS ###
loglvls = {'critical': logging.CRITICAL, loglvls = {'critical': logging.CRITICAL,
@ -25,52 +31,97 @@ loglvls = {'critical': logging.CRITICAL,
'info': logging.INFO, 'info': logging.INFO,
'debug': logging.DEBUG} 'debug': logging.DEBUG}


### THE GUTS ###
class Backup(object): class Backup(object):
def __init__(self, args): def __init__(self, args):
self.args = args self.args = args
# Set up logging # Set up logging
logging.basicConfig(level = self.args['loglevel']) _logfmt = logging.Formatter(fmt = '{levelname}:{name}: {message} ({asctime}; {filename}:{lineno})',
logger = logging.getLogger(__name__) 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)
for h in handlers:
h.setFormatter(_logfmt)
h.setLevel(loglvls[self.args['loglevel']])
self.logger.addHandler(h)

def create(self):
pass pass


def miscBak(self, pkgr):
pass

def mysqlBak(self):
pass

def listRepos(self):
pass

def printer(self):
pass

def lister(self):
pass

def printMoarHelp():
helpstr = ('\n\tNOTE: Sorting only applies to listing archives, NOT the contents!\n\n' +
'In order to efficiently display results, there are several options to handle it. ' +
'Namely, these are:\n\n\t\t-s/--sort [direction]\n\t\t-l/--limit [number]\n\t\t-x/--invert\n\n' +
'For example, if you want to list the 5 most recently *taken* snapshots, you would use:\n\n\t\t-l 5\n\n' +
'If you would want those SAME results SORTED in the reverse order (i.e. the 5 most recently ' +
'taken snapshots sorted from newest to oldest), then it would be: \n\n\t\t-l 5 -x\n\n' +
'Lastly, if you wanted to list the 7 OLDEST TAKEN snapshots in reverse order (that is, ' +
'sorted from newest to oldest), that\'d be: \n\n\t\t-o -l 7 -x\n')
print(helpstr)
exit(0)

def parseArgs(): def parseArgs():
### DEFAULTS ### ### DEFAULTS ###
_date = datetime.datetime.now().strftime("%Y_%m_%d.%H_%M") _date = datetime.datetime.now().strftime("%Y_%m_%d.%H_%M")
_logfile = '/var/log/borg/{0}'.format(_date) _logfile = '/var/log/borg/{0}'.format(_date)
_mysqldir = os.path.abspath(os.path.join(os.path.expanduser('~'), 'bak', 'mysql')) _mysqldir = os.path.abspath(os.path.join(os.path.expanduser('~'), 'bak', 'mysql'))
_stagedir = '/root/bak/misc' _stagedir = '/root/bak/misc'
_cfgfile = os.path.abspath(os.path.join(os.path.expanduser('~')), _cfgfile = os.path.abspath(os.path.join(os.path.expanduser('~'), '.config', 'optools', 'backup.json'))
'.backcfg.json')
_defloglvl = 'info' _defloglvl = 'info'
###### ######
args = argparse.ArgumentParser(description = 'Backups manager', args = argparse.ArgumentParser(description = 'Backups manager',
epilog = 'TIP: this program has context-specific help. e.g. try "%(prog)s list --help"') epilog = 'TIP: this program has context-specific help. e.g. try "%(prog)s list --help"')
commonargs = argparse.ArgumentParser(add_help = False) args.add_argument('-c', '--config',
commonargs.add_argument('-r', dest = 'cfgfile',
'--repo', default = _cfgfile,
dest = 'repo', help = ('The path to the config file. Default: \033[1m{0}\033[0m'.format(_cfgfile)))
help = ('The repository to perform the operation for. ' + args.add_argument('-Ll', '--loglevel',
'The default is \'all\', a special value that specifies all known ' +
'repositories. Can also accept a comma-separated list.'),
default = 'all')
commonargs.add_argument('-L',
'--loglevel',
dest = 'loglevel', dest = 'loglevel',
default = _defloglvl, default = _defloglvl,
choices = list(loglvls.keys()), choices = list(loglvls.keys()),
help = 'The level of logging to perform. Default: {0}'.format(_defloglvl)) help = 'The level of logging to perform. Default: \033[1m{0}\033[0m'.format(_defloglvl))
commonargs.add_argument('-Ld', args.add_argument('-Ld', '--log-to-disk',
'--log-to-disk',
dest = 'disklog', dest = 'disklog',
action = 'store_true', action = 'store_true',
help = ('If specified, log to a specific file (-Lf/--logfile)' + help = ('If specified, log to a specific file (-Lf/--logfile)' +
' instead of the system logger.')) ' instead of the system logger.'))
commonargs.add_argument('-Lf', args.add_argument('-Lf', '--logfile',
'--logfile',
dest = 'logfile', dest = 'logfile',
default = _logfile, default = _logfile,
help = ('The path to the logfile, only used if -Ld/--log-to-disk ' + help = ('The path to the logfile, only used if -Ld/--log-to-disk ' +
'is specified. Default: {0} (dynamic)').format(_logfile)) 'is specified. Default: \033[1m{0}\033[0m (dynamic)').format(_logfile))
### ARGS FOR ALL OPERATIONS ###
commonargs = argparse.ArgumentParser(add_help = False)
commonargs.add_argument('-r', '--repo',
dest = 'repo',
default = 'all',
help = ('The repository to perform the operation for. ' +
'The default is \033[1mall\033[0m, a special value that specifies all known ' +
'repositories. Can also accept a comma-separated list.'))
### OPERATIONS ###
subparsers = args.add_subparsers(help = 'Operation to perform', subparsers = args.add_subparsers(help = 'Operation to perform',
dest = 'oper') dest = 'oper')
backupargs = subparsers.add_parser('backup', backupargs = subparsers.add_parser('backup',
@ -85,17 +136,20 @@ def parseArgs():
initargs = subparsers.add_parser('init', initargs = subparsers.add_parser('init',
help = 'Initialise a repository.', help = 'Initialise a repository.',
parents = [commonargs]) parents = [commonargs])
### OPERATION-SPECIFIC OPTIONS ###
# CREATE ("backup") #
backupargs.add_argument('-a', backupargs.add_argument('-a',
'--archive', '--archive',
default = _date, default = _date,
dest = 'archive', dest = 'archive',
help = ('The name of the archive. Default: {0} (dynamic)').format(_date)) help = ('The name of the archive. Default: \033[1m{0}\033[0m (dynamic)').format(_date))
backupargs.add_argument('-s', backupargs.add_argument('-s',
'--stagedir', '--stagedir',
default = _stagedir, default = _stagedir,
dest = 'stagedir', dest = 'stagedir',
help = ('The directory used for staging temporary files, ' + help = ('The directory used for staging temporary files, ' +
'if necessary. Default: {0}').format(_stagedir)) 'if necessary. Default: \033[1m{0}\033[0m').format(_stagedir))
# DISPLAY/OUTPUT ("list") #
listargs.add_argument('-a', listargs.add_argument('-a',
'--archive', '--archive',
dest = 'archive', dest = 'archive',
@ -107,39 +161,44 @@ def parseArgs():
type = int, type = int,
default = '5', default = '5',
help = ('If specified, constrain the outout to this number of ' + help = ('If specified, constrain the outout to this number of ' +
'results each repo. Default is 5, 0 for unlimited. See -H/--list-help.')) 'results each repo. Default is \033[1m5\033[0m, use 0 for unlimited. ' +
'See \033[1m-H/--list-help\033[0m'))
listargs.add_argument('-s', listargs.add_argument('-s',
'--sort', '--sort',
dest = 'sortby', dest = 'sortby',
choices = ['newest', 'oldest'], choices = ['newest', 'oldest'],
default = 'oldest', default = 'oldest',
help = 'The order to sort the results by. See -H/--list-help.') help = ('The order to sort the results by. See \033[1m-H/--list-help\033[0m. ' +
'Default: \033[1moldest\033[0m'))
listargs.add_argument('-x', listargs.add_argument('-x',
'--invert', '--invert',
dest = 'invert', dest = 'invert',
action = 'store_true', action = 'store_true',
help = 'Invert the order of *results*. See -H/--list-help.') help = 'Invert the order of results. See \033[1m-H/--list-help\033[0m.')
listargs.add_argument('-o', listargs.add_argument('-o',
'--old', '--old',
dest = 'old', dest = 'old',
action = 'store_true', action = 'store_true',
help = ('Instead of grabbing the latest results, grab the earliest results. ' + help = ('Instead of grabbing the latest results, grab the earliest results. ' +
'This differs from -s/--sort. See -H/--list-help.')) 'This differs from \033[1m-s/--sort\033[0m. See \033[1m-H/--list-help\033[0m.'))
listargs.add_argument('-H', listargs.add_argument('-H',
'--list-help', '--list-help',
dest = 'moarhelp', dest = 'moarhelp',
action = 'store_true', action = 'store_true',
help = 'Print extended information about how to manage the output of listing and exit.') help = 'Print extended information about how to manage the output of listing and exit.')
listargs.add_argument('-v',
'--verbose',
dest = 'verbose',
help = ('Print out detailed information for archive contents ' +
'(only valid if -a is set). WARNING: May include sensitive data.'),
action = 'store_true')
return(args) return(args)


def main(): def main():
args = vars(parseArgs().parse_args()) rawargs = parseArgs()
args = vars(rawargs.parse_args())
import pprint # DEBUG
pprint.pprint(args) # DEBUG
if not args['oper']:
rawargs.print_help()
exit(0)
if 'moarhelp' in args.keys() and args['moarhelp']:
printMoarHelp()
bak = Backup(args)
if __name__ == '__main__': if __name__ == '__main__':
main() main()