2018-12-11 07:20:57 +01:00
|
|
|
import logging
|
|
|
|
import subprocess
|
|
|
|
|
2019-06-13 05:56:20 +02:00
|
|
|
from borgmatic.execute import execute_command
|
2018-12-11 07:20:57 +01:00
|
|
|
|
2019-06-17 20:53:08 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
2018-12-11 07:20:57 +01:00
|
|
|
|
|
|
|
|
2019-06-13 19:01:55 +02:00
|
|
|
INFO_REPOSITORY_NOT_FOUND_EXIT_CODE = 2
|
|
|
|
|
|
|
|
|
2018-12-11 07:20:57 +01:00
|
|
|
def initialize_repository(
|
|
|
|
repository,
|
|
|
|
encryption_mode,
|
|
|
|
append_only=None,
|
|
|
|
storage_quota=None,
|
|
|
|
local_path='borg',
|
|
|
|
remote_path=None,
|
|
|
|
):
|
|
|
|
'''
|
|
|
|
Given a local or remote repository path, a Borg encryption mode, whether the repository should
|
2018-12-25 07:28:02 +01:00
|
|
|
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 07:20:57 +01:00
|
|
|
'''
|
2018-12-25 07:28:02 +01:00
|
|
|
info_command = (local_path, 'info', repository)
|
|
|
|
logger.debug(' '.join(info_command))
|
|
|
|
|
2019-06-13 05:56:20 +02:00
|
|
|
try:
|
|
|
|
execute_command(info_command, output_log_level=None)
|
2018-12-25 07:28:02 +01:00
|
|
|
logger.info('Repository already exists. Skipping initialization.')
|
|
|
|
return
|
2019-06-13 05:56:20 +02:00
|
|
|
except subprocess.CalledProcessError as error:
|
2019-06-13 19:01:55 +02:00
|
|
|
if error.returncode != INFO_REPOSITORY_NOT_FOUND_EXIT_CODE:
|
2019-06-13 05:56:20 +02:00
|
|
|
raise
|
2018-12-25 07:28:02 +01:00
|
|
|
|
|
|
|
init_command = (
|
2018-12-11 07:20:57 +01:00
|
|
|
(local_path, 'init', repository)
|
|
|
|
+ (('--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-06-13 05:56:20 +02:00
|
|
|
# Don't use execute_command() here because it doesn't support interactive prompts.
|
2018-12-25 07:28:02 +01:00
|
|
|
subprocess.check_call(init_command)
|