recreate.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import logging
  2. import borgmatic.borg.environment
  3. import borgmatic.config.paths
  4. import borgmatic.execute
  5. from borgmatic.borg.create import make_exclude_flags
  6. from borgmatic.borg.flags import make_flags_from_arguments, make_repository_archive_flags
  7. logger = logging.getLogger(__name__)
  8. def make_recreate_command(
  9. repository,
  10. archive,
  11. config,
  12. local_borg_version,
  13. recreate_arguments,
  14. global_arguments,
  15. local_path,
  16. remote_path=None,
  17. ):
  18. '''
  19. Given a local or remote repository path, an archive name, a configuration dict,
  20. the local Borg version string, an argparse.Namespace of recreate arguments,
  21. an argparse.Namespace of global arguments, optional local and remote Borg paths.
  22. Returns the recreate command as a tuple of strings ready for execution.
  23. '''
  24. verbosity_flags = (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ()) + (
  25. ('--info',) if logger.isEnabledFor(logging.INFO) else ()
  26. )
  27. # handle both the recreate and global arguments
  28. recreate_flags = make_flags_from_arguments(
  29. recreate_arguments, excludes=('repository', 'archive')
  30. )
  31. global_flags = make_flags_from_arguments(global_arguments)
  32. repo_archive_flags = make_repository_archive_flags(repository, archive, local_borg_version)
  33. exclude_flags = make_exclude_flags(config)
  34. return (
  35. (local_path, 'recreate')
  36. + repo_archive_flags
  37. + verbosity_flags
  38. + global_flags
  39. + recreate_flags
  40. + exclude_flags
  41. )
  42. def recreate_archive(
  43. repository,
  44. archive,
  45. config,
  46. local_borg_version,
  47. recreate_arguments,
  48. global_arguments,
  49. local_path='borg',
  50. remote_path=None,
  51. ):
  52. '''
  53. Given a local or remote repository path, an archive name, a configuration dict,
  54. the local Borg version string, an argparse.Namespace of recreate arguments,
  55. an argparse.Namespace of global arguments, optional local and remote Borg paths.
  56. Executes the recreate command with the given arguments.
  57. '''
  58. command = make_recreate_command(
  59. repository,
  60. archive,
  61. config,
  62. local_borg_version,
  63. recreate_arguments,
  64. global_arguments,
  65. local_path,
  66. remote_path,
  67. )
  68. borgmatic.execute.execute_command(
  69. command,
  70. output_log_level=logging.ANSWER,
  71. environment=borgmatic.borg.environment.make_environment(config),
  72. working_directory=borgmatic.config.paths.get_working_directory(config),
  73. remote_path=remote_path,
  74. borg_local_path=local_path,
  75. borg_exit_codes=config.get('borg_exit_codes'),
  76. )