borgmatic.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from __future__ import print_function
  2. from argparse import ArgumentParser
  3. import os
  4. from subprocess import CalledProcessError
  5. import sys
  6. from borgmatic import borg
  7. from borgmatic.config import convert, validate
  8. LEGACY_CONFIG_FILENAME = '/etc/borgmatic/config'
  9. DEFAULT_CONFIG_FILENAME = '/etc/borgmatic/config.yaml'
  10. DEFAULT_EXCLUDES_FILENAME = '/etc/borgmatic/excludes'
  11. def parse_arguments(*arguments):
  12. '''
  13. Given command-line arguments with which this script was invoked, parse the arguments and return
  14. them as an ArgumentParser instance.
  15. '''
  16. parser = ArgumentParser()
  17. parser.add_argument(
  18. '-c', '--config',
  19. dest='config_filename',
  20. default=DEFAULT_CONFIG_FILENAME,
  21. help='Configuration filename',
  22. )
  23. parser.add_argument(
  24. '--excludes',
  25. dest='excludes_filename',
  26. help='Excludes filename, deprecated in favor of exclude_patterns within configuration',
  27. )
  28. parser.add_argument(
  29. '-v', '--verbosity',
  30. type=int,
  31. help='Display verbose progress (1 for some, 2 for lots)',
  32. )
  33. return parser.parse_args(arguments)
  34. def main(): # pragma: no cover
  35. try:
  36. args = parse_arguments(*sys.argv[1:])
  37. convert.guard_configuration_upgraded(LEGACY_CONFIG_FILENAME, args.config_filename)
  38. config = validate.parse_configuration(args.config_filename, validate.schema_filename())
  39. repository = config.location['repository']
  40. remote_path = config.location['remote_path']
  41. borg.initialize(config.storage)
  42. borg.create_archive(args.verbosity, config.storage, **config.location)
  43. borg.prune_archives(args.verbosity, repository, config.retention, remote_path=remote_path)
  44. borg.check_archives(args.verbosity, repository, config.consistency, remote_path=remote_path)
  45. except (ValueError, OSError, CalledProcessError) as error:
  46. print(error, file=sys.stderr)
  47. sys.exit(1)