[1230] | 1 | #!/usr/bin/env python
|
---|
| 2 |
|
---|
| 3 | from __future__ import print_function
|
---|
| 4 | import argparse
|
---|
| 5 | import bareos.bsock
|
---|
| 6 | import logging
|
---|
| 7 | import sys
|
---|
| 8 |
|
---|
| 9 | def get_job_names(director):
|
---|
| 10 | result=director.call('.jobs')['jobs']
|
---|
| 11 | jobs = [ job['name'] for job in result ]
|
---|
| 12 | return jobs
|
---|
| 13 |
|
---|
| 14 | def get_connected_clients(director):
|
---|
| 15 | result=director.call('status director')['client-connection']
|
---|
[1231] | 16 | clients = [ client['name'] for client in result ]
|
---|
[1230] | 17 | return clients
|
---|
| 18 |
|
---|
| 19 | def trigger(director, jobnames, clients):
|
---|
| 20 | for client in clients:
|
---|
| 21 | jobname = 'backup-{}'.format(client)
|
---|
| 22 | if not jobname in jobnames:
|
---|
| 23 | print('{} skipped, no matching job ({}) found'.format(client, jobname))
|
---|
| 24 | else:
|
---|
| 25 | jobs = director.call('list jobs client={} hours=24'.format(client))['jobs']
|
---|
| 26 | if jobs:
|
---|
[1240] | 27 | job = director.call('list jobs client={} hours=24 last'.format(client))['jobs'][0]
|
---|
[1230] | 28 | jobinfo = '{starttime}: jobid={jobid}, level={level}, status={jobstatus}'.format(**job)
|
---|
| 29 | print('{}: skipped, recent backups available ({})'.format(jobname, jobinfo))
|
---|
| 30 | else:
|
---|
[1231] | 31 | # TODO: check type=backup and failed
|
---|
[1230] | 32 | jobid = director.call('run {} yes'.format(jobname))['run']['jobid']
|
---|
| 33 | print('{}: backup triggered, jobid={}'.format(jobname, jobid))
|
---|
| 34 |
|
---|
| 35 | def getArguments():
|
---|
| 36 | parser = argparse.ArgumentParser(description='Console to Bareos Director.' )
|
---|
| 37 | parser.add_argument('-d', '--debug', action='store_true', help="enable debugging output")
|
---|
| 38 | parser.add_argument('--name', default="*UserAgent*", help="use this to access a specific Bareos director named console. Otherwise it connects to the default console (\"*UserAgent*\")")
|
---|
| 39 | parser.add_argument('-p', '--password', help="password to authenticate to a Bareos Director console", required=True)
|
---|
| 40 | parser.add_argument('--port', default=9101, help="Bareos Director network port")
|
---|
| 41 | parser.add_argument('--dirname', help="Bareos Director name")
|
---|
| 42 | parser.add_argument('address', nargs='?', default="localhost", help="Bareos Director network address")
|
---|
| 43 | args = parser.parse_args()
|
---|
| 44 | return args
|
---|
| 45 |
|
---|
| 46 | if __name__ == '__main__':
|
---|
| 47 | logging.basicConfig(format='%(levelname)s %(module)s.%(funcName)s: %(message)s', level=logging.INFO)
|
---|
| 48 | logger = logging.getLogger()
|
---|
| 49 |
|
---|
| 50 | args=getArguments()
|
---|
| 51 | if args.debug:
|
---|
| 52 | logger.setLevel(logging.DEBUG)
|
---|
| 53 |
|
---|
| 54 | try:
|
---|
| 55 | options = [ 'address', 'port', 'dirname', 'name' ]
|
---|
| 56 | parameter = {}
|
---|
| 57 | for i in options:
|
---|
| 58 | if hasattr(args, i) and getattr(args,i) != None:
|
---|
| 59 | logger.debug( "%s: %s" %(i, getattr(args,i)))
|
---|
| 60 | parameter[i] = getattr(args, i)
|
---|
| 61 | else:
|
---|
| 62 | logger.debug( '%s: ""' %(i))
|
---|
| 63 | logger.debug('options: %s' % (parameter))
|
---|
| 64 | password = bareos.bsock.Password(args.password)
|
---|
| 65 | parameter['password']=password
|
---|
| 66 | director = bareos.bsock.DirectorConsoleJson(**parameter)
|
---|
| 67 | except RuntimeError as e:
|
---|
| 68 | print(str(e))
|
---|
| 69 | sys.exit(1)
|
---|
| 70 | logger.debug( "authentication successful" )
|
---|
| 71 | jobs = get_job_names(director)
|
---|
| 72 | clients = get_connected_clients(director)
|
---|
| 73 | trigger(director, jobs, clients)
|
---|
| 74 |
|
---|
| 75 | #director.interactive()
|
---|