2014-10-31 06:34:03 +01:00
|
|
|
from datetime import datetime
|
2015-02-14 18:23:40 +01:00
|
|
|
import os
|
2014-10-31 06:34:03 +01:00
|
|
|
import platform
|
|
|
|
import subprocess
|
|
|
|
|
2015-07-28 06:47:52 +02:00
|
|
|
from atticmatic.config import Section_format, option
|
2015-07-18 06:58:50 +02:00
|
|
|
from atticmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS
|
2014-10-31 06:34:03 +01:00
|
|
|
|
2015-07-18 06:58:50 +02:00
|
|
|
|
2015-07-19 03:35:29 +02:00
|
|
|
# Common backend functionality shared by Attic and Borg. As the two backup
|
|
|
|
# commands diverge, these shared functions will likely need to be replaced
|
|
|
|
# with non-shared functions within atticmatic.backends.attic and
|
|
|
|
# atticmatic.backends.borg.
|
|
|
|
|
|
|
|
|
2015-07-28 06:47:52 +02:00
|
|
|
CONFIG_FORMAT = (
|
|
|
|
Section_format(
|
|
|
|
'location',
|
|
|
|
(
|
|
|
|
option('source_directories'),
|
|
|
|
option('repository'),
|
|
|
|
),
|
|
|
|
),
|
2015-09-03 07:48:07 +02:00
|
|
|
Section_format(
|
|
|
|
'storage',
|
|
|
|
(
|
|
|
|
option('encryption_passphrase', required=False),
|
|
|
|
),
|
|
|
|
),
|
2015-07-28 06:47:52 +02:00
|
|
|
Section_format(
|
|
|
|
'retention',
|
|
|
|
(
|
|
|
|
option('keep_within', required=False),
|
|
|
|
option('keep_hourly', int, required=False),
|
|
|
|
option('keep_daily', int, required=False),
|
|
|
|
option('keep_weekly', int, required=False),
|
|
|
|
option('keep_monthly', int, required=False),
|
|
|
|
option('keep_yearly', int, required=False),
|
|
|
|
option('prefix', required=False),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Section_format(
|
|
|
|
'consistency',
|
|
|
|
(
|
|
|
|
option('checks', required=False),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2015-09-03 07:48:07 +02:00
|
|
|
|
|
|
|
def initialize(storage_config, command):
|
|
|
|
passphrase = storage_config.get('encryption_passphrase')
|
|
|
|
|
|
|
|
if passphrase:
|
|
|
|
os.environ['{}_PASSPHRASE'.format(command.upper())] = passphrase
|
|
|
|
|
|
|
|
|
|
|
|
def create_archive(
|
|
|
|
excludes_filename, verbosity, storage_config, source_directories, repository, command,
|
|
|
|
):
|
2014-12-07 03:35:20 +01:00
|
|
|
'''
|
2015-09-03 07:48:07 +02:00
|
|
|
Given an excludes filename (or None), a vebosity flag, a storage config dict, a space-separated
|
|
|
|
list of source directories, a local or remote repository path, and a command to run, create an
|
|
|
|
attic archive.
|
2014-12-07 03:35:20 +01:00
|
|
|
'''
|
2014-10-31 06:34:03 +01:00
|
|
|
sources = tuple(source_directories.split(' '))
|
2015-07-27 06:06:03 +02:00
|
|
|
exclude_flags = ('--exclude-from', excludes_filename) if excludes_filename else ()
|
2015-09-03 07:48:07 +02:00
|
|
|
compression = storage_config.get('compression', None)
|
|
|
|
compression_flags = ('--compression', compression) if compression else ()
|
2015-07-18 06:58:50 +02:00
|
|
|
verbosity_flags = {
|
|
|
|
VERBOSITY_SOME: ('--stats',),
|
|
|
|
VERBOSITY_LOTS: ('--verbose', '--stats'),
|
|
|
|
}.get(verbosity, ())
|
2014-10-31 06:34:03 +01:00
|
|
|
|
2015-07-19 03:35:29 +02:00
|
|
|
full_command = (
|
|
|
|
command, 'create',
|
2014-10-31 06:34:03 +01:00
|
|
|
'{repo}::{hostname}-{timestamp}'.format(
|
|
|
|
repo=repository,
|
|
|
|
hostname=platform.node(),
|
|
|
|
timestamp=datetime.now().isoformat(),
|
|
|
|
),
|
2015-09-03 07:48:07 +02:00
|
|
|
) + sources + exclude_flags + compression_flags + verbosity_flags
|
2014-10-31 06:34:03 +01:00
|
|
|
|
2015-07-19 03:35:29 +02:00
|
|
|
subprocess.check_call(full_command)
|
2014-10-31 06:34:03 +01:00
|
|
|
|
|
|
|
|
2015-05-11 07:00:31 +02:00
|
|
|
def _make_prune_flags(retention_config):
|
2014-12-07 03:35:20 +01:00
|
|
|
'''
|
|
|
|
Given a retention config dict mapping from option name to value, tranform it into an iterable of
|
|
|
|
command-line name-value flag pairs.
|
|
|
|
|
|
|
|
For example, given a retention config of:
|
|
|
|
|
|
|
|
{'keep_weekly': 4, 'keep_monthly': 6}
|
|
|
|
|
|
|
|
This will be returned as an iterable of:
|
|
|
|
|
|
|
|
(
|
|
|
|
('--keep-weekly', '4'),
|
|
|
|
('--keep-monthly', '6'),
|
|
|
|
)
|
|
|
|
'''
|
|
|
|
return (
|
|
|
|
('--' + option_name.replace('_', '-'), str(retention_config[option_name]))
|
|
|
|
for option_name, value in retention_config.items()
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-07-19 03:35:29 +02:00
|
|
|
def prune_archives(verbosity, repository, retention_config, command):
|
2014-12-07 03:35:20 +01:00
|
|
|
'''
|
2015-07-19 03:35:29 +02:00
|
|
|
Given a verbosity flag, a local or remote repository path, a retention config dict, and a
|
|
|
|
command to run, prune attic archives according the the retention policy specified in that
|
|
|
|
configuration.
|
2014-12-07 03:35:20 +01:00
|
|
|
'''
|
2015-07-18 06:58:50 +02:00
|
|
|
verbosity_flags = {
|
|
|
|
VERBOSITY_SOME: ('--stats',),
|
|
|
|
VERBOSITY_LOTS: ('--verbose', '--stats'),
|
|
|
|
}.get(verbosity, ())
|
|
|
|
|
2015-07-19 03:35:29 +02:00
|
|
|
full_command = (
|
|
|
|
command, 'prune',
|
2014-10-31 06:34:03 +01:00
|
|
|
repository,
|
2014-12-07 03:35:20 +01:00
|
|
|
) + tuple(
|
|
|
|
element
|
2015-05-11 07:00:31 +02:00
|
|
|
for pair in _make_prune_flags(retention_config)
|
2014-12-07 03:35:20 +01:00
|
|
|
for element in pair
|
2015-07-18 06:58:50 +02:00
|
|
|
) + verbosity_flags
|
2014-10-31 06:34:03 +01:00
|
|
|
|
2015-07-19 03:35:29 +02:00
|
|
|
subprocess.check_call(full_command)
|
2015-02-14 18:23:40 +01:00
|
|
|
|
|
|
|
|
2015-05-11 07:00:31 +02:00
|
|
|
DEFAULT_CHECKS = ('repository', 'archives')
|
|
|
|
|
|
|
|
|
|
|
|
def _parse_checks(consistency_config):
|
|
|
|
'''
|
|
|
|
Given a consistency config with a space-separated "checks" option, transform it to a tuple of
|
|
|
|
named checks to run.
|
|
|
|
|
|
|
|
For example, given a retention config of:
|
|
|
|
|
|
|
|
{'checks': 'repository archives'}
|
|
|
|
|
|
|
|
This will be returned as:
|
|
|
|
|
|
|
|
('repository', 'archives')
|
|
|
|
|
|
|
|
If no "checks" option is present, return the DEFAULT_CHECKS. If the checks value is the string
|
|
|
|
"disabled", return an empty tuple, meaning that no checks should be run.
|
|
|
|
'''
|
|
|
|
checks = consistency_config.get('checks', '').strip()
|
|
|
|
if not checks:
|
|
|
|
return DEFAULT_CHECKS
|
|
|
|
|
|
|
|
return tuple(
|
|
|
|
check for check in consistency_config['checks'].split(' ')
|
|
|
|
if check.lower() not in ('disabled', '')
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-07-28 06:47:52 +02:00
|
|
|
def _make_check_flags(checks, check_last=None):
|
2015-02-14 18:23:40 +01:00
|
|
|
'''
|
2015-05-11 07:00:31 +02:00
|
|
|
Given a parsed sequence of checks, transform it into tuple of command-line flags.
|
|
|
|
|
|
|
|
For example, given parsed checks of:
|
|
|
|
|
|
|
|
('repository',)
|
|
|
|
|
|
|
|
This will be returned as:
|
|
|
|
|
|
|
|
('--repository-only',)
|
2015-07-28 06:47:52 +02:00
|
|
|
|
|
|
|
Additionally, if a check_last value is given, a "--last" flag will be added. Note that only
|
|
|
|
Borg supports this flag.
|
2015-02-14 18:23:40 +01:00
|
|
|
'''
|
2015-07-28 06:47:52 +02:00
|
|
|
last_flag = ('--last', check_last) if check_last else ()
|
2015-05-11 07:00:31 +02:00
|
|
|
if checks == DEFAULT_CHECKS:
|
2015-07-28 06:47:52 +02:00
|
|
|
return last_flag
|
2015-05-11 07:00:31 +02:00
|
|
|
|
|
|
|
return tuple(
|
|
|
|
'--{}-only'.format(check) for check in checks
|
2015-07-28 06:47:52 +02:00
|
|
|
) + last_flag
|
2015-05-11 07:00:31 +02:00
|
|
|
|
|
|
|
|
2015-07-19 03:35:29 +02:00
|
|
|
def check_archives(verbosity, repository, consistency_config, command):
|
2015-05-11 07:00:31 +02:00
|
|
|
'''
|
2015-07-19 03:35:29 +02:00
|
|
|
Given a verbosity flag, a local or remote repository path, a consistency config dict, and a
|
|
|
|
command to run, check the contained attic archives for consistency.
|
2015-05-11 07:00:31 +02:00
|
|
|
|
|
|
|
If there are no consistency checks to run, skip running them.
|
|
|
|
'''
|
|
|
|
checks = _parse_checks(consistency_config)
|
2015-07-28 06:47:52 +02:00
|
|
|
check_last = consistency_config.get('check_last', None)
|
2015-05-11 07:00:31 +02:00
|
|
|
if not checks:
|
|
|
|
return
|
|
|
|
|
2015-07-18 06:58:50 +02:00
|
|
|
verbosity_flags = {
|
|
|
|
VERBOSITY_SOME: ('--verbose',),
|
|
|
|
VERBOSITY_LOTS: ('--verbose',),
|
|
|
|
}.get(verbosity, ())
|
|
|
|
|
2015-07-19 03:35:29 +02:00
|
|
|
full_command = (
|
|
|
|
command, 'check',
|
2015-02-14 18:23:40 +01:00
|
|
|
repository,
|
2015-07-28 06:47:52 +02:00
|
|
|
) + _make_check_flags(checks, check_last) + verbosity_flags
|
2015-02-14 18:23:40 +01:00
|
|
|
|
2015-07-19 03:35:29 +02:00
|
|
|
# The check command spews to stdout even without the verbose flag. Suppress it.
|
2015-07-18 06:58:50 +02:00
|
|
|
stdout = None if verbosity_flags else open(os.devnull, 'w')
|
2015-02-14 18:23:40 +01:00
|
|
|
|
2015-07-19 03:35:29 +02:00
|
|
|
subprocess.check_call(full_command, stdout=stdout)
|