Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bareos-triggerjob: fix parameter handling #1708

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- webui: Backup Unit Report fixes [PR #1696]
- windows: fix calculation of "job_metadata.xml" object size [PR #1695]
- stored: fix storage daemon crash if passive client is unreachable, create better session keys [PR #1688]
- bareos-triggerjob: fix parameter handling [PR #1708]

### Removed
- plugins: remove old deprecated postgres plugin [PR #1606]
Expand Down Expand Up @@ -89,4 +90,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[PR #1688]: https://github.com/bareos/bareos/pull/1688
[PR #1695]: https://github.com/bareos/bareos/pull/1695
[PR #1696]: https://github.com/bareos/bareos/pull/1696
[PR #1708]: https://github.com/bareos/bareos/pull/1708
[unreleased]: https://github.com/bareos/bareos/tree/master
65 changes: 46 additions & 19 deletions contrib/misc/triggerjob/bareos-triggerjob.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,51 @@
#!/usr/bin/env python

bruno-at-bareos marked this conversation as resolved.
Show resolved Hide resolved
from bareos.util import argparse
import bareos.bsock
# BAREOS - Backup Archiving REcovery Open Sourced
#
# Copyright (C) 2019-2024 Bareos GmbH & Co. KG
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of version three of the GNU Affero General Public
# License as published by the Free Software Foundation and included
# in the file LICENSE.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

import logging
import sys
import bareos.bsock
from bareos.util import argparse


def get_job_names(director):
"""Get list of job names"""
result = director.call(".jobs")["jobs"]
jobs = [job["name"] for job in result]
return jobs


def get_connected_clients(director):
"""Get list of connected clients (via client initiated connections)"""
result = director.call("status director")["client-connection"]
clients = [client["name"] for client in result]
return clients


def trigger(director, jobnames, clients, hours):
"""
trigger all jobs that are named "backup-<CLIENTNAME>"
for all clients that are connected via client initiated connection
and did run a successful backup for more than <hours> hours.
"""

for client in clients:
jobname = "backup-{}".format(client)
if not jobname in jobnames:
Expand All @@ -44,7 +71,9 @@ def trigger(director, jobnames, clients, hours):
print("{}: backup triggered, jobid={}".format(jobname, jobid))


def getArguments():
def get_arguments():
"""argparse setup"""

epilog = """
bareos-triggerjob is a Python script that allows you to perform a backup for a connected client if a definable time has passed since the last backup.

Expand All @@ -57,12 +86,20 @@ def getArguments():
"""

argparser = argparse.ArgumentParser(
description=u"Trigger Bareos jobs.", epilog=epilog
description="Trigger Bareos jobs.", epilog=epilog
)
argparser.add_argument(
"-d", "--debug", action="store_true", help="enable debugging output"
)
bareos.bsock.DirectorConsole.argparser_add_default_command_line_arguments(argparser)
argparser.add_argument(
"--hours",
type=int,
default=24,
help="Minimum time (in hours) since the last successful backup. Default: %(default)s",
)
bareos.bsock.DirectorConsoleJson.argparser_add_default_command_line_arguments(
argparser
)
args = argparser.parse_args()
return args

Expand All @@ -73,24 +110,14 @@ def getArguments():
)
logger = logging.getLogger()

args = getArguments()
args = get_arguments()
if args.debug:
logger.setLevel(logging.DEBUG)

bareos_args = bareos.bsock.DirectorConsoleJson.argparser_get_bareos_parameter(args)
try:
bruno-at-bareos marked this conversation as resolved.
Show resolved Hide resolved
options = ["address", "port", "dirname", "name"]
parameter = {}
for i in options:
if hasattr(args, i) and getattr(args, i) != None:
logger.debug("%s: %s" % (i, getattr(args, i)))
parameter[i] = getattr(args, i)
else:
logger.debug('%s: ""' % (i))
logger.debug("options: %s" % (parameter))
password = bareos.bsock.Password(args.password)
parameter["password"] = password
director = bareos.bsock.DirectorConsoleJson(**parameter)
except RuntimeError as e:
director = bareos.bsock.DirectorConsoleJson(**bareos_args)
except bareos.exceptions.Error as e:
print(str(e))
sys.exit(1)
logger.debug("authentication successful")
Expand Down
27 changes: 13 additions & 14 deletions python-bareos/bareos/bsock/directorconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"""

from bareos.bsock.connectiontype import ConnectionType
from bareos.bsock.constants import Constants
from bareos.bsock.lowlevel import LowLevel
from bareos.bsock.protocolmessageids import ProtocolMessageIds
from bareos.bsock.protocolmessages import ProtocolMessages
Expand Down Expand Up @@ -64,73 +63,73 @@ def argparser_add_default_command_line_arguments(argparser):
argparser (ArgParser or ConfigArgParser): (Config)ArgParser instance.
"""

argparser.add_argument(
group = argparser.add_argument_group(title="Bareos Director connection options")

group.add_argument(
"--name",
default="*UserAgent*",
help='use this to access a specific Bareos director named console. Otherwise it connects to the default console ("*UserAgent*").',
dest="BAREOS_name",
)

argparser.add_argument(
group.add_argument(
"-p",
bruno-at-bareos marked this conversation as resolved.
Show resolved Hide resolved
"--password",
help="Password to authenticate to a Bareos Director console.",
required=True,
dest="BAREOS_password",
)

argparser.add_argument(
group.add_argument(
"--port",
default=9101,
help="Bareos Director network port.",
dest="BAREOS_port",
)

# argparser.add_argument('--dirname', help="Bareos Director name")
argparser.add_argument(
group.add_argument(
"--address",
default="localhost",
help="Bareos Director network address.",
dest="BAREOS_address",
)

argparser.add_argument(
group.add_argument(
"--timeout",
type=int,
help="Timeout (in seconds) for the connection to the Bareos Director.",
dest="BAREOS_timeout",
)

argparser.add_argument(
group.add_argument(
"--protocolversion",
default=ProtocolVersions.last,
type=int,
help="Specify the Bareos console protocol version. Default: {0} (current).".format(
ProtocolVersions.last
),
help="Specify the Bareos console protocol version. Default: %(default)s (current).",
dest="BAREOS_protocolversion",
)

argparser.add_argument(
group.add_argument(
"--pam-username",
help="Username to authenticate against PAM on top off the normal authentication.",
dest="BAREOS_pam_username",
)

argparser.add_argument(
group.add_argument(
"--pam-password",
help="Password to authenticate against PAM on top off the normal authentication.",
dest="BAREOS_pam_password",
)

argparser.add_argument(
group.add_argument(
"--tls-psk-require",
help="Allow only encrypted connections. Default: False.",
action="store_true",
dest="BAREOS_tls_psk_require",
)

TlsVersionParser().add_argument(argparser)
TlsVersionParser().add_argument(group)

def __init__(
self,
Expand Down