2014-10-31 06:34:03 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
from argparse import ArgumentParser
|
2015-07-19 03:35:29 +02:00
|
|
|
from importlib import import_module
|
|
|
|
import os
|
2014-10-31 06:34:03 +01:00
|
|
|
from subprocess import CalledProcessError
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from atticmatic.config import parse_configuration
|
|
|
|
|
|
|
|
|
2015-07-19 03:35:29 +02:00
|
|
|
DEFAULT_CONFIG_FILENAME_PATTERN = '/etc/{}/config'
|
|
|
|
DEFAULT_EXCLUDES_FILENAME_PATTERN = '/etc/{}/excludes'
|
2014-12-20 20:37:25 +01:00
|
|
|
|
|
|
|
|
2015-07-19 03:35:29 +02:00
|
|
|
def parse_arguments(command_name, *arguments):
|
2014-12-02 07:35:25 +01:00
|
|
|
'''
|
2015-07-19 03:35:29 +02:00
|
|
|
Given the name of the command with which this script was invoked and command-line arguments,
|
|
|
|
parse the arguments and return them as an ArgumentParser instance. Use the command name to
|
|
|
|
determine the default configuration and excludes paths.
|
2014-12-02 07:35:25 +01:00
|
|
|
'''
|
2015-07-27 06:06:03 +02:00
|
|
|
config_filename_default = DEFAULT_CONFIG_FILENAME_PATTERN.format(command_name)
|
|
|
|
excludes_filename_default = DEFAULT_EXCLUDES_FILENAME_PATTERN.format(command_name)
|
|
|
|
|
2014-10-31 06:34:03 +01:00
|
|
|
parser = ArgumentParser()
|
|
|
|
parser.add_argument(
|
2014-12-02 07:47:51 +01:00
|
|
|
'-c', '--config',
|
2014-10-31 06:34:03 +01:00
|
|
|
dest='config_filename',
|
2015-07-27 06:06:03 +02:00
|
|
|
default=config_filename_default,
|
2014-10-31 06:34:03 +01:00
|
|
|
help='Configuration filename',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--excludes',
|
|
|
|
dest='excludes_filename',
|
2015-07-27 06:06:03 +02:00
|
|
|
default=excludes_filename_default if os.path.exists(excludes_filename_default) else None,
|
2014-10-31 06:34:03 +01:00
|
|
|
help='Excludes filename',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2015-07-18 06:58:50 +02:00
|
|
|
'-v', '--verbosity',
|
|
|
|
type=int,
|
|
|
|
help='Display verbose progress (1 for some, 2 for lots)',
|
2014-10-31 06:34:03 +01:00
|
|
|
)
|
|
|
|
|
2014-12-20 20:37:25 +01:00
|
|
|
return parser.parse_args(arguments)
|
2014-12-02 07:35:25 +01:00
|
|
|
|
|
|
|
|
2015-07-19 03:35:29 +02:00
|
|
|
def load_backend(command_name):
|
|
|
|
'''
|
|
|
|
Given the name of the command with which this script was invoked, return the corresponding
|
|
|
|
backend module responsible for actually dealing with backups.
|
|
|
|
'''
|
|
|
|
backend_name = {
|
|
|
|
'atticmatic': 'attic',
|
|
|
|
'borgmatic': 'borg',
|
|
|
|
}.get(command_name, 'attic')
|
|
|
|
|
|
|
|
return import_module('atticmatic.backends.{}'.format(backend_name))
|
|
|
|
|
|
|
|
|
2014-12-02 07:35:25 +01:00
|
|
|
def main():
|
2014-10-31 06:34:03 +01:00
|
|
|
try:
|
2015-07-19 03:35:29 +02:00
|
|
|
command_name = os.path.basename(sys.argv[0])
|
|
|
|
args = parse_arguments(command_name, *sys.argv[1:])
|
|
|
|
backend = load_backend(command_name)
|
2015-07-28 06:47:52 +02:00
|
|
|
config = parse_configuration(args.config_filename, backend.CONFIG_FORMAT)
|
|
|
|
repository = config.location['repository']
|
2014-10-31 06:34:03 +01:00
|
|
|
|
2015-07-19 03:35:29 +02:00
|
|
|
backend.create_archive(args.excludes_filename, args.verbosity, **config.location)
|
|
|
|
backend.prune_archives(args.verbosity, repository, config.retention)
|
|
|
|
backend.check_archives(args.verbosity, repository, config.consistency)
|
2015-03-15 18:44:18 +01:00
|
|
|
except (ValueError, IOError, CalledProcessError) as error:
|
2014-10-31 06:34:03 +01:00
|
|
|
print(error, file=sys.stderr)
|
|
|
|
sys.exit(1)
|