2021-06-18 03:41:44 +00:00
|
|
|
import logging
|
|
|
|
|
2023-05-14 16:59:28 +00:00
|
|
|
import borgmatic.commands.arguments
|
2022-12-02 20:12:10 +00:00
|
|
|
import borgmatic.logger
|
2022-08-16 16:30:00 +00:00
|
|
|
from borgmatic.borg import environment, flags
|
2023-04-30 22:43:41 +00:00
|
|
|
from borgmatic.execute import DO_NOT_CAPTURE, execute_command
|
2021-06-18 03:41:44 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-08-16 16:30:00 +00:00
|
|
|
BORG_SUBCOMMANDS_WITH_SUBCOMMANDS = {'key', 'debug'}
|
2021-06-18 03:41:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run_arbitrary_borg(
|
2023-03-26 18:22:25 +00:00
|
|
|
repository_path,
|
2023-07-09 06:14:30 +00:00
|
|
|
config,
|
2022-08-16 16:30:00 +00:00
|
|
|
local_borg_version,
|
|
|
|
options,
|
|
|
|
archive=None,
|
|
|
|
local_path='borg',
|
|
|
|
remote_path=None,
|
2021-06-18 03:41:44 +00:00
|
|
|
):
|
|
|
|
'''
|
2023-07-09 06:14:30 +00:00
|
|
|
Given a local or remote repository path, a configuration dict, the local Borg version, a
|
2022-08-16 16:30:00 +00:00
|
|
|
sequence of arbitrary command-line Borg options, and an optional archive name, run an arbitrary
|
2023-06-26 21:37:23 +00:00
|
|
|
Borg command, passing in REPOSITORY and ARCHIVE environment variables for optional use in the
|
|
|
|
command.
|
2021-06-18 03:41:44 +00:00
|
|
|
'''
|
2022-12-02 20:12:10 +00:00
|
|
|
borgmatic.logger.add_custom_log_levels()
|
2023-07-09 06:14:30 +00:00
|
|
|
lock_wait = config.get('lock_wait', None)
|
2021-06-18 03:41:44 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
options = options[1:] if options[0] == '--' else options
|
2022-04-23 21:03:15 +00:00
|
|
|
|
2022-05-29 22:43:03 +00:00
|
|
|
# Borg commands like "key" have a sub-command ("export", etc.) that must follow it.
|
2022-08-16 16:30:00 +00:00
|
|
|
command_options_start_index = 2 if options[0] in BORG_SUBCOMMANDS_WITH_SUBCOMMANDS else 1
|
2022-04-23 21:03:15 +00:00
|
|
|
borg_command = tuple(options[:command_options_start_index])
|
|
|
|
command_options = tuple(options[command_options_start_index:])
|
2023-05-14 16:59:28 +00:00
|
|
|
|
2023-06-19 23:18:47 +00:00
|
|
|
if borg_command and borg_command[0] in borgmatic.commands.arguments.ACTION_ALIASES.keys():
|
2023-05-14 16:59:28 +00:00
|
|
|
logger.warning(
|
|
|
|
f"Borg's {borg_command[0]} subcommand is supported natively by borgmatic. Try this instead: borgmatic {borg_command[0]}"
|
|
|
|
)
|
2021-06-18 03:41:44 +00:00
|
|
|
except IndexError:
|
2022-04-23 21:03:15 +00:00
|
|
|
borg_command = ()
|
2021-06-18 03:41:44 +00:00
|
|
|
command_options = ()
|
|
|
|
|
|
|
|
full_command = (
|
|
|
|
(local_path,)
|
2022-04-23 21:03:15 +00:00
|
|
|
+ borg_command
|
2021-06-18 03:41:44 +00:00
|
|
|
+ (('--info',) if logger.getEffectiveLevel() == logging.INFO else ())
|
|
|
|
+ (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ())
|
2022-08-16 16:30:00 +00:00
|
|
|
+ flags.make_flags('remote-path', remote_path)
|
|
|
|
+ flags.make_flags('lock-wait', lock_wait)
|
2023-06-26 21:35:07 +00:00
|
|
|
+ command_options
|
2021-06-18 03:41:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return execute_command(
|
2022-06-30 20:42:17 +00:00
|
|
|
full_command,
|
2023-04-30 22:43:41 +00:00
|
|
|
output_file=DO_NOT_CAPTURE,
|
2022-06-30 20:42:17 +00:00
|
|
|
borg_local_path=local_path,
|
2023-06-26 21:35:07 +00:00
|
|
|
shell=True,
|
|
|
|
extra_environment=dict(
|
2023-07-09 06:14:30 +00:00
|
|
|
(environment.make_environment(config) or {}),
|
2023-06-26 21:35:07 +00:00
|
|
|
**{
|
2023-07-03 07:08:54 +00:00
|
|
|
'BORG_REPO': repository_path,
|
2023-06-26 21:35:07 +00:00
|
|
|
'ARCHIVE': archive if archive else '',
|
|
|
|
},
|
|
|
|
),
|
2021-06-18 03:41:44 +00:00
|
|
|
)
|