borgmatic/borgmatic/commands/borgmatic.py

57 lines
1.9 KiB
Python
Raw Normal View History

2016-06-10 18:21:53 +00:00
from __future__ import print_function
from argparse import ArgumentParser
import os
from subprocess import CalledProcessError
import sys
from borgmatic import borg
from borgmatic.config import convert, validate
2016-06-10 18:21:53 +00:00
LEGACY_CONFIG_FILENAME = '/etc/borgmatic/config'
DEFAULT_CONFIG_FILENAME = '/etc/borgmatic/config.yaml'
2016-06-10 18:21:53 +00:00
DEFAULT_EXCLUDES_FILENAME = '/etc/borgmatic/excludes'
def parse_arguments(*arguments):
'''
Given command-line arguments with which this script was invoked, parse the arguments and return
them as an ArgumentParser instance.
2016-06-10 18:21:53 +00:00
'''
parser = ArgumentParser()
parser.add_argument(
'-c', '--config',
dest='config_filename',
default=DEFAULT_CONFIG_FILENAME,
help='Configuration filename',
)
parser.add_argument(
'--excludes',
dest='excludes_filename',
help='Excludes filename, deprecated in favor of exclude_patterns within configuration',
2016-06-10 18:21:53 +00:00
)
parser.add_argument(
'-v', '--verbosity',
type=int,
help='Display verbose progress (1 for some, 2 for lots)',
)
return parser.parse_args(arguments)
def main(): # pragma: no cover
2016-06-10 18:21:53 +00:00
try:
args = parse_arguments(*sys.argv[1:])
convert.guard_configuration_upgraded(LEGACY_CONFIG_FILENAME, args.config_filename)
config = validate.parse_configuration(args.config_filename, validate.schema_filename())
2016-06-10 18:21:53 +00:00
repository = config.location['repository']
remote_path = config.location['remote_path']
2016-06-10 18:21:53 +00:00
borg.initialize(config.storage)
borg.create_archive(args.verbosity, config.storage, **config.location)
borg.prune_archives(args.verbosity, repository, config.retention, remote_path=remote_path)
borg.check_archives(args.verbosity, repository, config.consistency, remote_path=remote_path)
except (ValueError, OSError, CalledProcessError) as error:
2016-06-10 18:21:53 +00:00
print(error, file=sys.stderr)
sys.exit(1)