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']
|
---|
16 | clients = [ result['name'] ]
|
---|
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 | job = director.call('list jobs client={} hours=24 last'.format(client))['jobs'][0]
|
---|
27 | if jobs:
|
---|
28 | jobinfo = '{starttime}: jobid={jobid}, level={level}, status={jobstatus}'.format(**job)
|
---|
29 | print('{}: skipped, recent backups available ({})'.format(jobname, jobinfo))
|
---|
30 | else:
|
---|
31 | jobid = director.call('run {} yes'.format(jobname))['run']['jobid']
|
---|
32 | print('{}: backup triggered, jobid={}'.format(jobname, jobid))
|
---|
33 |
|
---|
34 | def getArguments():
|
---|
35 | parser = argparse.ArgumentParser(description='Console to Bareos Director.' )
|
---|
36 | parser.add_argument('-d', '--debug', action='store_true', help="enable debugging output")
|
---|
37 | 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*\")")
|
---|
38 | parser.add_argument('-p', '--password', help="password to authenticate to a Bareos Director console", required=True)
|
---|
39 | parser.add_argument('--port', default=9101, help="Bareos Director network port")
|
---|
40 | parser.add_argument('--dirname', help="Bareos Director name")
|
---|
41 | parser.add_argument('address', nargs='?', default="localhost", help="Bareos Director network address")
|
---|
42 | args = parser.parse_args()
|
---|
43 | return args
|
---|
44 |
|
---|
45 | if __name__ == '__main__':
|
---|
46 | logging.basicConfig(format='%(levelname)s %(module)s.%(funcName)s: %(message)s', level=logging.INFO)
|
---|
47 | logger = logging.getLogger()
|
---|
48 |
|
---|
49 | args=getArguments()
|
---|
50 | if args.debug:
|
---|
51 | logger.setLevel(logging.DEBUG)
|
---|
52 |
|
---|
53 | try:
|
---|
54 | options = [ 'address', 'port', 'dirname', 'name' ]
|
---|
55 | parameter = {}
|
---|
56 | for i in options:
|
---|
57 | if hasattr(args, i) and getattr(args,i) != None:
|
---|
58 | logger.debug( "%s: %s" %(i, getattr(args,i)))
|
---|
59 | parameter[i] = getattr(args, i)
|
---|
60 | else:
|
---|
61 | logger.debug( '%s: ""' %(i))
|
---|
62 | logger.debug('options: %s' % (parameter))
|
---|
63 | password = bareos.bsock.Password(args.password)
|
---|
64 | parameter['password']=password
|
---|
65 | director = bareos.bsock.DirectorConsoleJson(**parameter)
|
---|
66 | except RuntimeError as e:
|
---|
67 | print(str(e))
|
---|
68 | sys.exit(1)
|
---|
69 | logger.debug( "authentication successful" )
|
---|
70 | jobs = get_job_names(director)
|
---|
71 | clients = get_connected_clients(director)
|
---|
72 | trigger(director, jobs, clients)
|
---|
73 |
|
---|
74 | #director.interactive()
|
---|