check.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. import calendar
  2. import datetime
  3. import hashlib
  4. import itertools
  5. import logging
  6. import os
  7. import pathlib
  8. import random
  9. import shutil
  10. import borgmatic.actions.pattern
  11. import borgmatic.borg.check
  12. import borgmatic.borg.create
  13. import borgmatic.borg.environment
  14. import borgmatic.borg.extract
  15. import borgmatic.borg.list
  16. import borgmatic.borg.repo_list
  17. import borgmatic.borg.state
  18. import borgmatic.config.paths
  19. import borgmatic.config.validate
  20. import borgmatic.execute
  21. import borgmatic.hooks.command
  22. DEFAULT_CHECKS = (
  23. {'name': 'repository', 'frequency': '1 month'},
  24. {'name': 'archives', 'frequency': '1 month'},
  25. )
  26. logger = logging.getLogger(__name__)
  27. def parse_checks(config, only_checks=None):
  28. '''
  29. Given a configuration dict with a "checks" sequence of dicts and an optional list of override
  30. checks, return a tuple of named checks to run.
  31. For example, given a config of:
  32. {'checks': ({'name': 'repository'}, {'name': 'archives'})}
  33. This will be returned as:
  34. ('repository', 'archives')
  35. If no "checks" option is present in the config, return the DEFAULT_CHECKS. If a checks value
  36. has a name of "disabled", return an empty tuple, meaning that no checks should be run.
  37. '''
  38. checks = only_checks or tuple(
  39. check_config['name'] for check_config in (config.get('checks', None) or DEFAULT_CHECKS)
  40. )
  41. checks = tuple(check.lower() for check in checks)
  42. if 'disabled' in checks:
  43. logger.warning(
  44. 'The "disabled" value for the "checks" option is deprecated and will be removed from a future release; use "skip_actions" instead'
  45. )
  46. if len(checks) > 1:
  47. logger.warning(
  48. 'Multiple checks are configured, but one of them is "disabled"; not running any checks'
  49. )
  50. return ()
  51. return checks
  52. def parse_frequency(frequency):
  53. '''
  54. Given a frequency string with a number and a unit of time, return a corresponding
  55. datetime.timedelta instance or None if the frequency is None or "always".
  56. For instance, given "3 weeks", return datetime.timedelta(weeks=3)
  57. Raise ValueError if the given frequency cannot be parsed.
  58. '''
  59. if not frequency:
  60. return None
  61. frequency = frequency.strip().lower()
  62. if frequency == 'always':
  63. return None
  64. try:
  65. number, time_unit = frequency.split(' ')
  66. number = int(number)
  67. except ValueError:
  68. raise ValueError(f"Could not parse consistency check frequency '{frequency}'")
  69. if not time_unit.endswith('s'):
  70. time_unit += 's'
  71. if time_unit == 'months':
  72. number *= 30
  73. time_unit = 'days'
  74. elif time_unit == 'years':
  75. number *= 365
  76. time_unit = 'days'
  77. try:
  78. return datetime.timedelta(**{time_unit: number})
  79. except TypeError:
  80. raise ValueError(f"Could not parse consistency check frequency '{frequency}'")
  81. WEEKDAY_DAYS = calendar.day_name[0:5]
  82. WEEKEND_DAYS = calendar.day_name[5:7]
  83. def filter_checks_on_frequency(
  84. config,
  85. borg_repository_id,
  86. checks,
  87. force,
  88. archives_check_id=None,
  89. datetime_now=datetime.datetime.now,
  90. ):
  91. '''
  92. Given a configuration dict with a "checks" sequence of dicts, a Borg repository ID, a sequence
  93. of checks, whether to force checks to run, and an ID for the archives check potentially being
  94. run (if any), filter down those checks based on the configured "frequency" for each check as
  95. compared to its check time file.
  96. In other words, a check whose check time file's timestamp is too new (based on the configured
  97. frequency) will get cut from the returned sequence of checks. Example:
  98. config = {
  99. 'checks': [
  100. {
  101. 'name': 'archives',
  102. 'frequency': '2 weeks',
  103. },
  104. ]
  105. }
  106. When this function is called with that config and "archives" in checks, "archives" will get
  107. filtered out of the returned result if its check time file is newer than 2 weeks old, indicating
  108. that it's not yet time to run that check again.
  109. Raise ValueError if a frequency cannot be parsed.
  110. '''
  111. if not checks:
  112. return checks
  113. filtered_checks = list(checks)
  114. if force:
  115. return tuple(filtered_checks)
  116. for check_config in config.get('checks', DEFAULT_CHECKS):
  117. check = check_config['name']
  118. if checks and check not in checks:
  119. continue
  120. only_run_on = check_config.get('only_run_on')
  121. if only_run_on:
  122. # Use a dict instead of a set to preserve ordering.
  123. days = dict.fromkeys(only_run_on)
  124. if 'weekday' in days:
  125. days = {
  126. **dict.fromkeys(day for day in days if day != 'weekday'),
  127. **dict.fromkeys(WEEKDAY_DAYS),
  128. }
  129. if 'weekend' in days:
  130. days = {
  131. **dict.fromkeys(day for day in days if day != 'weekend'),
  132. **dict.fromkeys(WEEKEND_DAYS),
  133. }
  134. if calendar.day_name[datetime_now().weekday()] not in days:
  135. logger.info(
  136. f"Skipping {check} check due to day of the week; check only runs on {'/'.join(day.title() for day in days)} (use --force to check anyway)"
  137. )
  138. filtered_checks.remove(check)
  139. continue
  140. frequency_delta = parse_frequency(check_config.get('frequency'))
  141. if not frequency_delta:
  142. continue
  143. check_time = probe_for_check_time(config, borg_repository_id, check, archives_check_id)
  144. if not check_time:
  145. continue
  146. # If we've not yet reached the time when the frequency dictates we're ready for another
  147. # check, skip this check.
  148. if datetime_now() < check_time + frequency_delta:
  149. remaining = check_time + frequency_delta - datetime_now()
  150. logger.info(
  151. f'Skipping {check} check due to configured frequency; {remaining} until next check (use --force to check anyway)'
  152. )
  153. filtered_checks.remove(check)
  154. return tuple(filtered_checks)
  155. def make_archives_check_id(archive_filter_flags):
  156. '''
  157. Given a sequence of flags to filter archives, return a unique hash corresponding to those
  158. particular flags. If there are no flags, return None.
  159. '''
  160. if not archive_filter_flags:
  161. return None
  162. return hashlib.sha256(' '.join(archive_filter_flags).encode()).hexdigest()
  163. def make_check_time_path(config, borg_repository_id, check_type, archives_check_id=None):
  164. '''
  165. Given a configuration dict, a Borg repository ID, the name of a check type ("repository",
  166. "archives", etc.), and a unique hash of the archives filter flags, return a path for recording
  167. that check's time (the time of that check last occurring).
  168. '''
  169. borgmatic_state_directory = borgmatic.config.paths.get_borgmatic_state_directory(config)
  170. if check_type in ('archives', 'data'):
  171. return os.path.join(
  172. borgmatic_state_directory,
  173. 'checks',
  174. borg_repository_id,
  175. check_type,
  176. archives_check_id if archives_check_id else 'all',
  177. )
  178. return os.path.join(
  179. borgmatic_state_directory,
  180. 'checks',
  181. borg_repository_id,
  182. check_type,
  183. )
  184. def write_check_time(path): # pragma: no cover
  185. '''
  186. Record a check time of now as the modification time of the given path.
  187. '''
  188. logger.debug(f'Writing check time at {path}')
  189. os.makedirs(os.path.dirname(path), mode=0o700, exist_ok=True)
  190. pathlib.Path(path, mode=0o600).touch()
  191. def read_check_time(path):
  192. '''
  193. Return the check time based on the modification time of the given path. Return None if the path
  194. doesn't exist.
  195. '''
  196. logger.debug(f'Reading check time from {path}')
  197. try:
  198. return datetime.datetime.fromtimestamp(os.stat(path).st_mtime)
  199. except FileNotFoundError:
  200. return None
  201. def probe_for_check_time(config, borg_repository_id, check, archives_check_id):
  202. '''
  203. Given a configuration dict, a Borg repository ID, the name of a check type ("repository",
  204. "archives", etc.), and a unique hash of the archives filter flags, return the corresponding
  205. check time or None if such a check time does not exist.
  206. When the check type is "archives" or "data", this function probes two different paths to find
  207. the check time, e.g.:
  208. ~/.borgmatic/checks/1234567890/archives/9876543210
  209. ~/.borgmatic/checks/1234567890/archives/all
  210. ... and returns the maximum modification time of the files found (if any). The first path
  211. represents a more specific archives check time (a check on a subset of archives), and the second
  212. is a fallback to the last "all" archives check.
  213. For other check types, this function reads from a single check time path, e.g.:
  214. ~/.borgmatic/checks/1234567890/repository
  215. '''
  216. check_times = (
  217. read_check_time(group[0])
  218. for group in itertools.groupby(
  219. (
  220. make_check_time_path(config, borg_repository_id, check, archives_check_id),
  221. make_check_time_path(config, borg_repository_id, check),
  222. )
  223. )
  224. )
  225. try:
  226. return max(check_time for check_time in check_times if check_time)
  227. except ValueError:
  228. return None
  229. def upgrade_check_times(config, borg_repository_id):
  230. '''
  231. Given a configuration dict and a Borg repository ID, upgrade any corresponding check times on
  232. disk from old-style paths to new-style paths.
  233. One upgrade performed is moving the checks directory from:
  234. {borgmatic_source_directory}/checks (e.g., ~/.borgmatic/checks)
  235. to:
  236. {borgmatic_state_directory}/checks (e.g. ~/.local/state/borgmatic)
  237. Another upgrade is renaming an archive or data check path that looks like:
  238. {borgmatic_state_directory}/checks/1234567890/archives
  239. to:
  240. {borgmatic_state_directory}/checks/1234567890/archives/all
  241. '''
  242. borgmatic_source_checks_path = os.path.join(
  243. borgmatic.config.paths.get_borgmatic_source_directory(config), 'checks'
  244. )
  245. borgmatic_state_path = borgmatic.config.paths.get_borgmatic_state_directory(config)
  246. borgmatic_state_checks_path = os.path.join(borgmatic_state_path, 'checks')
  247. if os.path.exists(borgmatic_source_checks_path) and not os.path.exists(
  248. borgmatic_state_checks_path
  249. ):
  250. logger.debug(
  251. f'Upgrading archives check times directory from {borgmatic_source_checks_path} to {borgmatic_state_checks_path}'
  252. )
  253. os.makedirs(borgmatic_state_path, mode=0o700, exist_ok=True)
  254. shutil.move(borgmatic_source_checks_path, borgmatic_state_checks_path)
  255. for check_type in ('archives', 'data'):
  256. new_path = make_check_time_path(config, borg_repository_id, check_type, 'all')
  257. old_path = os.path.dirname(new_path)
  258. temporary_path = f'{old_path}.temp'
  259. if not os.path.isfile(old_path) and not os.path.isfile(temporary_path):
  260. continue
  261. logger.debug(f'Upgrading archives check time file from {old_path} to {new_path}')
  262. try:
  263. shutil.move(old_path, temporary_path)
  264. except FileNotFoundError:
  265. pass
  266. os.mkdir(old_path)
  267. shutil.move(temporary_path, new_path)
  268. def collect_spot_check_source_paths(
  269. repository,
  270. config,
  271. local_borg_version,
  272. global_arguments,
  273. local_path,
  274. remote_path,
  275. borgmatic_runtime_directory,
  276. ):
  277. '''
  278. Given a repository configuration dict, a configuration dict, the local Borg version, global
  279. arguments as an argparse.Namespace instance, the local Borg path, and the remote Borg path,
  280. collect the source paths that Borg would use in an actual create (but only include files).
  281. '''
  282. stream_processes = any(
  283. borgmatic.hooks.dispatch.call_hooks(
  284. 'use_streaming',
  285. config,
  286. borgmatic.hooks.dispatch.Hook_type.DATA_SOURCE,
  287. ).values()
  288. )
  289. working_directory = borgmatic.config.paths.get_working_directory(config)
  290. (create_flags, create_positional_arguments, pattern_file) = (
  291. borgmatic.borg.create.make_base_create_command(
  292. dry_run=True,
  293. repository_path=repository['path'],
  294. # Omit "progress" because it interferes with "list_details".
  295. config=dict(
  296. {option: value for option, value in config.items() if option != 'progress'},
  297. list_details=True,
  298. ),
  299. patterns=borgmatic.actions.pattern.process_patterns(
  300. borgmatic.actions.pattern.collect_patterns(config),
  301. working_directory,
  302. ),
  303. local_borg_version=local_borg_version,
  304. global_arguments=global_arguments,
  305. borgmatic_runtime_directory=borgmatic_runtime_directory,
  306. local_path=local_path,
  307. remote_path=remote_path,
  308. stream_processes=stream_processes,
  309. )
  310. )
  311. working_directory = borgmatic.config.paths.get_working_directory(config)
  312. paths_output = borgmatic.execute.execute_command_and_capture_output(
  313. create_flags + create_positional_arguments,
  314. capture_stderr=True,
  315. environment=borgmatic.borg.environment.make_environment(config),
  316. working_directory=working_directory,
  317. borg_local_path=local_path,
  318. borg_exit_codes=config.get('borg_exit_codes'),
  319. )
  320. paths = tuple(
  321. path_line.split(' ', 1)[1]
  322. for path_line in paths_output.splitlines()
  323. if path_line and path_line.startswith('- ') or path_line.startswith('+ ')
  324. )
  325. return tuple(
  326. path for path in paths if os.path.isfile(os.path.join(working_directory or '', path))
  327. )
  328. BORG_DIRECTORY_FILE_TYPE = 'd'
  329. BORG_PIPE_FILE_TYPE = 'p'
  330. def collect_spot_check_archive_paths(
  331. repository,
  332. archive,
  333. config,
  334. local_borg_version,
  335. global_arguments,
  336. local_path,
  337. remote_path,
  338. borgmatic_runtime_directory,
  339. ):
  340. '''
  341. Given a repository configuration dict, the name of the latest archive, a configuration dict, the
  342. local Borg version, global arguments as an argparse.Namespace instance, the local Borg path, the
  343. remote Borg path, and the borgmatic runtime directory, collect the paths from the given archive
  344. (but only include files and symlinks and exclude borgmatic runtime directories).
  345. These paths do not have a leading slash, as that's how Borg stores them. As a result, we don't
  346. know whether they came from absolute or relative source directories.
  347. '''
  348. borgmatic_source_directory = borgmatic.config.paths.get_borgmatic_source_directory(config)
  349. return tuple(
  350. path
  351. for line in borgmatic.borg.list.capture_archive_listing(
  352. repository['path'],
  353. archive,
  354. config,
  355. local_borg_version,
  356. global_arguments,
  357. path_format='{type} {path}{NUL}', # noqa: FS003
  358. local_path=local_path,
  359. remote_path=remote_path,
  360. )
  361. for (file_type, path) in (line.split(' ', 1),)
  362. if file_type not in (BORG_DIRECTORY_FILE_TYPE, BORG_PIPE_FILE_TYPE)
  363. if pathlib.Path('borgmatic') not in pathlib.Path(path).parents
  364. if pathlib.Path(borgmatic_source_directory.lstrip(os.path.sep))
  365. not in pathlib.Path(path).parents
  366. if pathlib.Path(borgmatic_runtime_directory.lstrip(os.path.sep))
  367. not in pathlib.Path(path).parents
  368. )
  369. SAMPLE_PATHS_SUBSET_COUNT = 10000
  370. def compare_spot_check_hashes(
  371. repository,
  372. archive,
  373. config,
  374. local_borg_version,
  375. global_arguments,
  376. local_path,
  377. remote_path,
  378. source_paths,
  379. ):
  380. '''
  381. Given a repository configuration dict, the name of the latest archive, a configuration dict, the
  382. local Borg version, global arguments as an argparse.Namespace instance, the local Borg path, the
  383. remote Borg path, and spot check source paths, compare the hashes for a sampling of the source
  384. paths with hashes from corresponding paths in the given archive. Return a sequence of the paths
  385. that fail that hash comparison.
  386. '''
  387. # Based on the configured sample percentage, come up with a list of random sample files from the
  388. # source directories.
  389. spot_check_config = next(check for check in config['checks'] if check['name'] == 'spot')
  390. sample_count = max(
  391. int(len(source_paths) * (min(spot_check_config['data_sample_percentage'], 100) / 100)), 1
  392. )
  393. source_sample_paths = tuple(random.sample(source_paths, sample_count))
  394. working_directory = borgmatic.config.paths.get_working_directory(config)
  395. hashable_source_sample_path = {
  396. source_path
  397. for source_path in source_sample_paths
  398. for full_source_path in (os.path.join(working_directory or '', source_path),)
  399. if os.path.exists(full_source_path)
  400. if not os.path.islink(full_source_path)
  401. }
  402. logger.debug(
  403. f'Sampling {sample_count} source paths (~{spot_check_config["data_sample_percentage"]}%) for spot check'
  404. )
  405. source_sample_paths_iterator = iter(source_sample_paths)
  406. source_hashes = {}
  407. archive_hashes = {}
  408. # Only hash a few thousand files at a time (a subset of the total paths) to avoid an "Argument
  409. # list too long" shell error.
  410. while True:
  411. # Hash each file in the sample paths (if it exists).
  412. source_sample_paths_subset = tuple(
  413. itertools.islice(source_sample_paths_iterator, SAMPLE_PATHS_SUBSET_COUNT)
  414. )
  415. if not source_sample_paths_subset:
  416. break
  417. hash_output = borgmatic.execute.execute_command_and_capture_output(
  418. (spot_check_config.get('xxh64sum_command', 'xxh64sum'),)
  419. + tuple(
  420. path for path in source_sample_paths_subset if path in hashable_source_sample_path
  421. ),
  422. working_directory=working_directory,
  423. )
  424. source_hashes.update(
  425. **dict(
  426. (reversed(line.split(' ', 1)) for line in hash_output.splitlines()),
  427. # Represent non-existent files as having empty hashes so the comparison below still
  428. # works. Same thing for filesystem links, since Borg produces empty archive hashes
  429. # for them.
  430. **{
  431. path: ''
  432. for path in source_sample_paths_subset
  433. if path not in hashable_source_sample_path
  434. },
  435. )
  436. )
  437. # Get the hash for each file in the archive.
  438. archive_hashes.update(
  439. **dict(
  440. reversed(line.split(' ', 1))
  441. for line in borgmatic.borg.list.capture_archive_listing(
  442. repository['path'],
  443. archive,
  444. config,
  445. local_borg_version,
  446. global_arguments,
  447. list_paths=source_sample_paths_subset,
  448. path_format='{xxh64} {path}{NUL}', # noqa: FS003
  449. local_path=local_path,
  450. remote_path=remote_path,
  451. )
  452. if line
  453. )
  454. )
  455. # Compare the source hashes with the archive hashes to see how many match.
  456. failing_paths = []
  457. for path, source_hash in source_hashes.items():
  458. archive_hash = archive_hashes.get(path.lstrip(os.path.sep))
  459. if archive_hash is not None and archive_hash == source_hash:
  460. continue
  461. failing_paths.append(path)
  462. return tuple(failing_paths)
  463. def spot_check(
  464. repository,
  465. config,
  466. local_borg_version,
  467. global_arguments,
  468. local_path,
  469. remote_path,
  470. borgmatic_runtime_directory,
  471. ):
  472. '''
  473. Given a repository dict, a loaded configuration dict, the local Borg version, global arguments
  474. as an argparse.Namespace instance, the local Borg path, the remote Borg path, and the borgmatic
  475. runtime directory, perform a spot check for the latest archive in the given repository.
  476. A spot check compares file counts and also the hashes for a random sampling of source files on
  477. disk to those stored in the latest archive. If any differences are beyond configured tolerances,
  478. then the check fails.
  479. '''
  480. logger.debug('Running spot check')
  481. try:
  482. spot_check_config = next(
  483. check for check in config.get('checks', ()) if check.get('name') == 'spot'
  484. )
  485. except StopIteration:
  486. raise ValueError('Cannot run spot check because it is unconfigured')
  487. if spot_check_config['data_tolerance_percentage'] > spot_check_config['data_sample_percentage']:
  488. raise ValueError(
  489. 'The data_tolerance_percentage must be less than or equal to the data_sample_percentage'
  490. )
  491. source_paths = collect_spot_check_source_paths(
  492. repository,
  493. config,
  494. local_borg_version,
  495. global_arguments,
  496. local_path,
  497. remote_path,
  498. borgmatic_runtime_directory,
  499. )
  500. logger.debug(f'{len(source_paths)} total source paths for spot check')
  501. archive = borgmatic.borg.repo_list.resolve_archive_name(
  502. repository['path'],
  503. 'latest',
  504. config,
  505. local_borg_version,
  506. global_arguments,
  507. local_path,
  508. remote_path,
  509. )
  510. logger.debug(f'Using archive {archive} for spot check')
  511. archive_paths = collect_spot_check_archive_paths(
  512. repository,
  513. archive,
  514. config,
  515. local_borg_version,
  516. global_arguments,
  517. local_path,
  518. remote_path,
  519. borgmatic_runtime_directory,
  520. )
  521. logger.debug(f'{len(archive_paths)} total archive paths for spot check')
  522. if len(source_paths) == 0:
  523. logger.debug(
  524. f'Paths in latest archive but not source paths: {", ".join(set(archive_paths)) or "none"}'
  525. )
  526. raise ValueError(
  527. 'Spot check failed: There are no source paths to compare against the archive'
  528. )
  529. # Calculate the percentage delta between the source paths count and the archive paths count, and
  530. # compare that delta to the configured count tolerance percentage.
  531. count_delta_percentage = abs(len(source_paths) - len(archive_paths)) / len(source_paths) * 100
  532. if count_delta_percentage > spot_check_config['count_tolerance_percentage']:
  533. rootless_source_paths = set(path.lstrip(os.path.sep) for path in source_paths)
  534. logger.debug(
  535. f'Paths in source paths but not latest archive: {", ".join(rootless_source_paths - set(archive_paths)) or "none"}'
  536. )
  537. logger.debug(
  538. f'Paths in latest archive but not source paths: {", ".join(set(archive_paths) - rootless_source_paths) or "none"}'
  539. )
  540. raise ValueError(
  541. f'Spot check failed: {count_delta_percentage:.2f}% file count delta between source paths and latest archive (tolerance is {spot_check_config["count_tolerance_percentage"]}%)'
  542. )
  543. failing_paths = compare_spot_check_hashes(
  544. repository,
  545. archive,
  546. config,
  547. local_borg_version,
  548. global_arguments,
  549. local_path,
  550. remote_path,
  551. source_paths,
  552. )
  553. # Error if the percentage of failing hashes exceeds the configured tolerance percentage.
  554. logger.debug(f'{len(failing_paths)} non-matching spot check hashes')
  555. data_tolerance_percentage = spot_check_config['data_tolerance_percentage']
  556. failing_percentage = (len(failing_paths) / len(source_paths)) * 100
  557. if failing_percentage > data_tolerance_percentage:
  558. logger.debug(
  559. f'Source paths with data not matching the latest archive: {", ".join(failing_paths)}'
  560. )
  561. raise ValueError(
  562. f'Spot check failed: {failing_percentage:.2f}% of source paths with data not matching the latest archive (tolerance is {data_tolerance_percentage}%)'
  563. )
  564. logger.info(
  565. f'Spot check passed with a {count_delta_percentage:.2f}% file count delta and a {failing_percentage:.2f}% file data delta'
  566. )
  567. def run_check(
  568. config_filename,
  569. repository,
  570. config,
  571. local_borg_version,
  572. check_arguments,
  573. global_arguments,
  574. local_path,
  575. remote_path,
  576. ):
  577. '''
  578. Run the "check" action for the given repository.
  579. Raise ValueError if the Borg repository ID cannot be determined.
  580. '''
  581. if check_arguments.repository and not borgmatic.config.validate.repositories_match(
  582. repository, check_arguments.repository
  583. ):
  584. return
  585. logger.info('Running consistency checks')
  586. repository_id = borgmatic.borg.check.get_repository_id(
  587. repository['path'],
  588. config,
  589. local_borg_version,
  590. global_arguments,
  591. local_path=local_path,
  592. remote_path=remote_path,
  593. )
  594. upgrade_check_times(config, repository_id)
  595. configured_checks = parse_checks(config, check_arguments.only_checks)
  596. archive_filter_flags = borgmatic.borg.check.make_archive_filter_flags(
  597. local_borg_version, config, configured_checks, check_arguments
  598. )
  599. archives_check_id = make_archives_check_id(archive_filter_flags)
  600. checks = filter_checks_on_frequency(
  601. config,
  602. repository_id,
  603. configured_checks,
  604. check_arguments.force,
  605. archives_check_id,
  606. )
  607. borg_specific_checks = set(checks).intersection({'repository', 'archives', 'data'})
  608. if borg_specific_checks:
  609. borgmatic.borg.check.check_archives(
  610. repository['path'],
  611. config,
  612. local_borg_version,
  613. check_arguments,
  614. global_arguments,
  615. borg_specific_checks,
  616. archive_filter_flags,
  617. local_path=local_path,
  618. remote_path=remote_path,
  619. )
  620. for check in borg_specific_checks:
  621. write_check_time(make_check_time_path(config, repository_id, check, archives_check_id))
  622. if 'extract' in checks:
  623. borgmatic.borg.extract.extract_last_archive_dry_run(
  624. config,
  625. local_borg_version,
  626. global_arguments,
  627. repository['path'],
  628. config.get('lock_wait'),
  629. local_path,
  630. remote_path,
  631. )
  632. write_check_time(make_check_time_path(config, repository_id, 'extract'))
  633. if 'spot' in checks:
  634. with borgmatic.config.paths.Runtime_directory(config) as borgmatic_runtime_directory:
  635. spot_check(
  636. repository,
  637. config,
  638. local_borg_version,
  639. global_arguments,
  640. local_path,
  641. remote_path,
  642. borgmatic_runtime_directory,
  643. )
  644. write_check_time(make_check_time_path(config, repository_id, 'spot'))