shared.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. from datetime import datetime
  2. import os
  3. import re
  4. import platform
  5. import subprocess
  6. from atticmatic.config import Section_format, option
  7. from atticmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS
  8. # Common backend functionality shared by Attic and Borg. As the two backup
  9. # commands diverge, these shared functions will likely need to be replaced
  10. # with non-shared functions within atticmatic.backends.attic and
  11. # atticmatic.backends.borg.
  12. CONFIG_FORMAT = (
  13. Section_format(
  14. 'location',
  15. (
  16. option('source_directories'),
  17. option('repository'),
  18. ),
  19. ),
  20. Section_format(
  21. 'storage',
  22. (
  23. option('encryption_passphrase', required=False),
  24. ),
  25. ),
  26. Section_format(
  27. 'retention',
  28. (
  29. option('keep_within', required=False),
  30. option('keep_hourly', int, required=False),
  31. option('keep_daily', int, required=False),
  32. option('keep_weekly', int, required=False),
  33. option('keep_monthly', int, required=False),
  34. option('keep_yearly', int, required=False),
  35. option('prefix', required=False),
  36. ),
  37. ),
  38. Section_format(
  39. 'consistency',
  40. (
  41. option('checks', required=False),
  42. ),
  43. )
  44. )
  45. def initialize(storage_config, command):
  46. passphrase = storage_config.get('encryption_passphrase')
  47. if passphrase:
  48. os.environ['{}_PASSPHRASE'.format(command.upper())] = passphrase
  49. def create_archive(
  50. excludes_filename, verbosity, storage_config, source_directories, repository, command,
  51. one_file_system=None,
  52. ):
  53. '''
  54. Given an excludes filename (or None), a vebosity flag, a storage config dict, a space-separated
  55. list of source directories, a local or remote repository path, and a command to run, create an
  56. attic archive.
  57. '''
  58. sources = tuple(re.split('\s+', source_directories))
  59. exclude_flags = ('--exclude-from', excludes_filename) if excludes_filename else ()
  60. compression = storage_config.get('compression', None)
  61. compression_flags = ('--compression', compression) if compression else ()
  62. umask = storage_config.get('umask', None)
  63. umask_flags = ('--umask', str(umask)) if umask else ()
  64. one_file_system_flags = ('--one-file-system',) if one_file_system else ()
  65. verbosity_flags = {
  66. VERBOSITY_SOME: ('--stats',),
  67. VERBOSITY_LOTS: ('--verbose', '--stats'),
  68. }.get(verbosity, ())
  69. full_command = (
  70. command, 'create',
  71. '{repo}::{hostname}-{timestamp}'.format(
  72. repo=repository,
  73. hostname=platform.node(),
  74. timestamp=datetime.now().isoformat(),
  75. ),
  76. ) + sources + exclude_flags + compression_flags + one_file_system_flags + \
  77. umask_flags + verbosity_flags
  78. subprocess.check_call(full_command)
  79. def _make_prune_flags(retention_config):
  80. '''
  81. Given a retention config dict mapping from option name to value, tranform it into an iterable of
  82. command-line name-value flag pairs.
  83. For example, given a retention config of:
  84. {'keep_weekly': 4, 'keep_monthly': 6}
  85. This will be returned as an iterable of:
  86. (
  87. ('--keep-weekly', '4'),
  88. ('--keep-monthly', '6'),
  89. )
  90. '''
  91. return (
  92. ('--' + option_name.replace('_', '-'), str(retention_config[option_name]))
  93. for option_name, value in retention_config.items()
  94. )
  95. def prune_archives(verbosity, repository, retention_config, command):
  96. '''
  97. Given a verbosity flag, a local or remote repository path, a retention config dict, and a
  98. command to run, prune attic archives according the the retention policy specified in that
  99. configuration.
  100. '''
  101. verbosity_flags = {
  102. VERBOSITY_SOME: ('--stats',),
  103. VERBOSITY_LOTS: ('--verbose', '--stats'),
  104. }.get(verbosity, ())
  105. full_command = (
  106. command, 'prune',
  107. repository,
  108. ) + tuple(
  109. element
  110. for pair in _make_prune_flags(retention_config)
  111. for element in pair
  112. ) + verbosity_flags
  113. subprocess.check_call(full_command)
  114. DEFAULT_CHECKS = ('repository', 'archives')
  115. def _parse_checks(consistency_config):
  116. '''
  117. Given a consistency config with a space-separated "checks" option, transform it to a tuple of
  118. named checks to run.
  119. For example, given a retention config of:
  120. {'checks': 'repository archives'}
  121. This will be returned as:
  122. ('repository', 'archives')
  123. If no "checks" option is present, return the DEFAULT_CHECKS. If the checks value is the string
  124. "disabled", return an empty tuple, meaning that no checks should be run.
  125. '''
  126. checks = consistency_config.get('checks', '').strip()
  127. if not checks:
  128. return DEFAULT_CHECKS
  129. return tuple(
  130. check for check in consistency_config['checks'].split(' ')
  131. if check.lower() not in ('disabled', '')
  132. )
  133. def _make_check_flags(checks, check_last=None):
  134. '''
  135. Given a parsed sequence of checks, transform it into tuple of command-line flags.
  136. For example, given parsed checks of:
  137. ('repository',)
  138. This will be returned as:
  139. ('--repository-only',)
  140. Additionally, if a check_last value is given, a "--last" flag will be added. Note that only
  141. Borg supports this flag.
  142. '''
  143. last_flag = ('--last', check_last) if check_last else ()
  144. if checks == DEFAULT_CHECKS:
  145. return last_flag
  146. return tuple(
  147. '--{}-only'.format(check) for check in checks
  148. ) + last_flag
  149. def check_archives(verbosity, repository, consistency_config, command):
  150. '''
  151. Given a verbosity flag, a local or remote repository path, a consistency config dict, and a
  152. command to run, check the contained attic archives for consistency.
  153. If there are no consistency checks to run, skip running them.
  154. '''
  155. checks = _parse_checks(consistency_config)
  156. check_last = consistency_config.get('check_last', None)
  157. if not checks:
  158. return
  159. verbosity_flags = {
  160. VERBOSITY_SOME: ('--verbose',),
  161. VERBOSITY_LOTS: ('--verbose',),
  162. }.get(verbosity, ())
  163. full_command = (
  164. command, 'check',
  165. repository,
  166. ) + _make_check_flags(checks, check_last) + verbosity_flags
  167. # The check command spews to stdout even without the verbose flag. Suppress it.
  168. stdout = None if verbosity_flags else open(os.devnull, 'w')
  169. subprocess.check_call(full_command, stdout=stdout)