2021-06-18 03:41:44 +00:00
|
|
|
import logging
|
|
|
|
|
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__)
|
|
|
|
|
|
|
|
|
|
|
|
REPOSITORYLESS_BORG_COMMANDS = {'serve', None}
|
2022-08-16 16:30:00 +00:00
|
|
|
BORG_SUBCOMMANDS_WITH_SUBCOMMANDS = {'key', 'debug'}
|
|
|
|
BORG_SUBCOMMANDS_WITHOUT_REPOSITORY = (('debug', 'info'), ('debug', 'convert-profile'), ())
|
2021-06-18 03:41:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run_arbitrary_borg(
|
2023-03-26 18:22:25 +00:00
|
|
|
repository_path,
|
2022-08-16 16:30:00 +00:00
|
|
|
storage_config,
|
|
|
|
local_borg_version,
|
|
|
|
options,
|
|
|
|
archive=None,
|
|
|
|
local_path='borg',
|
|
|
|
remote_path=None,
|
2021-06-18 03:41:44 +00:00
|
|
|
):
|
|
|
|
'''
|
2022-08-16 16:30:00 +00:00
|
|
|
Given a local or remote repository path, a storage config dict, the local Borg version, a
|
|
|
|
sequence of arbitrary command-line Borg options, and an optional archive name, run an arbitrary
|
|
|
|
Borg command on the given repository/archive.
|
2021-06-18 03:41:44 +00:00
|
|
|
'''
|
2022-12-02 20:12:10 +00:00
|
|
|
borgmatic.logger.add_custom_log_levels()
|
2021-06-18 03:41:44 +00:00
|
|
|
lock_wait = storage_config.get('lock_wait', None)
|
|
|
|
|
|
|
|
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:])
|
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 = ()
|
|
|
|
|
2022-05-29 22:43:03 +00:00
|
|
|
if borg_command in BORG_SUBCOMMANDS_WITHOUT_REPOSITORY:
|
2022-08-16 16:30:00 +00:00
|
|
|
repository_archive_flags = ()
|
|
|
|
elif archive:
|
|
|
|
repository_archive_flags = flags.make_repository_archive_flags(
|
2023-03-26 18:22:25 +00:00
|
|
|
repository_path, archive, local_borg_version
|
2022-05-29 22:43:03 +00:00
|
|
|
)
|
2022-08-16 16:30:00 +00:00
|
|
|
else:
|
2023-03-26 18:22:25 +00:00
|
|
|
repository_archive_flags = flags.make_repository_flags(repository_path, local_borg_version)
|
2021-06-18 03:41:44 +00:00
|
|
|
|
|
|
|
full_command = (
|
|
|
|
(local_path,)
|
2022-04-23 21:03:15 +00:00
|
|
|
+ borg_command
|
2022-08-16 16:30:00 +00:00
|
|
|
+ repository_archive_flags
|
2021-06-18 03:41:44 +00:00
|
|
|
+ command_options
|
|
|
|
+ (('--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)
|
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,
|
|
|
|
extra_environment=environment.make_environment(storage_config),
|
2021-06-18 03:41:44 +00:00
|
|
|
)
|