2018-12-11 06:20:57 +00:00
|
|
|
import logging
|
|
|
|
import subprocess
|
|
|
|
|
2019-09-25 19:03:10 +00:00
|
|
|
from borgmatic.execute import execute_command, execute_command_without_capture
|
2018-12-11 06:20:57 +00:00
|
|
|
|
2019-06-17 18:53:08 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2018-12-11 06:20:57 +00:00
|
|
|
|
|
|
|
|
2019-06-13 17:01:55 +00:00
|
|
|
INFO_REPOSITORY_NOT_FOUND_EXIT_CODE = 2
|
|
|
|
|
|
|
|
|
2018-12-11 06:20:57 +00:00
|
|
|
def initialize_repository(
|
|
|
|
repository,
|
2019-12-04 23:48:10 +00:00
|
|
|
storage_config,
|
2018-12-11 06:20:57 +00:00
|
|
|
encryption_mode,
|
|
|
|
append_only=None,
|
|
|
|
storage_quota=None,
|
|
|
|
local_path='borg',
|
|
|
|
remote_path=None,
|
|
|
|
):
|
|
|
|
'''
|
2019-12-04 23:48:10 +00:00
|
|
|
Given a local or remote repository path, a storage configuration dict, a Borg encryption mode,
|
|
|
|
whether the repository should be append-only, and the storage quota to use, initialize the
|
|
|
|
repository. If the repository already exists, then log and skip initialization.
|
2018-12-11 06:20:57 +00:00
|
|
|
'''
|
2018-12-25 06:28:02 +00:00
|
|
|
info_command = (local_path, 'info', repository)
|
|
|
|
logger.debug(' '.join(info_command))
|
|
|
|
|
2019-06-13 03:56:20 +00:00
|
|
|
try:
|
|
|
|
execute_command(info_command, output_log_level=None)
|
2018-12-25 06:28:02 +00:00
|
|
|
logger.info('Repository already exists. Skipping initialization.')
|
|
|
|
return
|
2019-06-13 03:56:20 +00:00
|
|
|
except subprocess.CalledProcessError as error:
|
2019-06-13 17:01:55 +00:00
|
|
|
if error.returncode != INFO_REPOSITORY_NOT_FOUND_EXIT_CODE:
|
2019-06-13 03:56:20 +00:00
|
|
|
raise
|
2018-12-25 06:28:02 +00:00
|
|
|
|
2019-12-04 23:48:10 +00:00
|
|
|
extra_borg_options = storage_config.get('extra_borg_options', {}).get('init', '')
|
|
|
|
|
2018-12-25 06:28:02 +00:00
|
|
|
init_command = (
|
2019-09-12 22:27:04 +00:00
|
|
|
(local_path, 'init')
|
2018-12-11 06:20:57 +00:00
|
|
|
+ (('--encryption', encryption_mode) if encryption_mode else ())
|
|
|
|
+ (('--append-only',) if append_only else ())
|
|
|
|
+ (('--storage-quota', storage_quota) if storage_quota else ())
|
|
|
|
+ (('--info',) if logger.getEffectiveLevel() == logging.INFO else ())
|
|
|
|
+ (('--debug',) if logger.isEnabledFor(logging.DEBUG) else ())
|
|
|
|
+ (('--remote-path', remote_path) if remote_path else ())
|
2019-12-04 23:48:10 +00:00
|
|
|
+ (tuple(extra_borg_options.split(' ')) if extra_borg_options else ())
|
2019-09-12 22:27:04 +00:00
|
|
|
+ (repository,)
|
2018-12-11 06:20:57 +00:00
|
|
|
)
|
|
|
|
|
2019-06-13 03:56:20 +00:00
|
|
|
# Don't use execute_command() here because it doesn't support interactive prompts.
|
2019-12-01 00:55:05 +00:00
|
|
|
execute_command_without_capture(init_command, error_on_warnings=False)
|