restore.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. import collections
  2. import logging
  3. import os
  4. import pathlib
  5. import shutil
  6. import tempfile
  7. import borgmatic.borg.extract
  8. import borgmatic.borg.list
  9. import borgmatic.borg.mount
  10. import borgmatic.borg.repo_list
  11. import borgmatic.config.paths
  12. import borgmatic.config.validate
  13. import borgmatic.hooks.data_source.dump
  14. import borgmatic.hooks.dispatch
  15. logger = logging.getLogger(__name__)
  16. UNSPECIFIED = object()
  17. Dump = collections.namedtuple(
  18. 'Dump',
  19. ('hook_name', 'data_source_name', 'hostname', 'port', 'label'),
  20. defaults=('localhost', None, None),
  21. )
  22. def dumps_match(first, second, default_port=None):
  23. '''
  24. Compare two Dump instances for equality while supporting a field value of UNSPECIFIED, which
  25. indicates that the field should match any value. If a default port is given, then consider any
  26. dump having that port to match with a dump having a None port.
  27. '''
  28. # label kinda counts as id, if they match ignore hostname & port
  29. if first.label not in {None, UNSPECIFIED} and first.label == second.label:
  30. return first[:2] == second[:2]
  31. for field_name in first._fields:
  32. first_value = getattr(first, field_name)
  33. second_value = getattr(second, field_name)
  34. if default_port is not None and field_name == 'port':
  35. if first_value == default_port and second_value is None:
  36. continue
  37. if second_value == default_port and first_value is None:
  38. continue
  39. if first_value == UNSPECIFIED or second_value == UNSPECIFIED: # noqa: PLR1714
  40. continue
  41. if first_value != second_value:
  42. return False
  43. return True
  44. def render_dump_metadata(dump):
  45. '''
  46. Given a Dump instance, make a display string describing it for use in log messages.
  47. '''
  48. label = dump.label or UNSPECIFIED
  49. name = 'unspecified' if dump.data_source_name is UNSPECIFIED else dump.data_source_name
  50. hostname = dump.hostname or UNSPECIFIED
  51. port = None if dump.port is UNSPECIFIED else dump.port
  52. if label is not UNSPECIFIED:
  53. metadata = f'{name}@{label}'
  54. elif port:
  55. metadata = f'{name}@:{port}' if hostname is UNSPECIFIED else f'{name}@{hostname}:{port}'
  56. else:
  57. metadata = f'{name}' if hostname is UNSPECIFIED else f'{name}@{hostname}'
  58. if dump.hook_name not in {None, UNSPECIFIED}:
  59. return f'{metadata} ({dump.hook_name})'
  60. return metadata
  61. def get_configured_data_source(config, restore_dump):
  62. '''
  63. Search in the given configuration dict for dumps corresponding to the given dump to restore. If
  64. there are multiple matches, error.
  65. Return the found data source as a data source configuration dict or None if not found.
  66. '''
  67. try:
  68. hooks_to_search = {restore_dump.hook_name: config[restore_dump.hook_name]}
  69. except KeyError:
  70. return None
  71. matching_dumps = tuple(
  72. hook_data_source
  73. for (hook_name, hook_config) in hooks_to_search.items()
  74. for hook_data_source in hook_config
  75. for default_port in (
  76. borgmatic.hooks.dispatch.call_hook(
  77. function_name='get_default_port',
  78. config=config,
  79. hook_name=hook_name,
  80. ),
  81. )
  82. if dumps_match(
  83. Dump(
  84. hook_name,
  85. hook_data_source.get('name'),
  86. hook_data_source.get('hostname', 'localhost'),
  87. hook_data_source.get('port'),
  88. hook_data_source.get('label') or hook_data_source.get('container') or UNSPECIFIED,
  89. ),
  90. restore_dump,
  91. default_port,
  92. )
  93. )
  94. if not matching_dumps:
  95. return None
  96. if len(matching_dumps) > 1:
  97. raise ValueError(
  98. f'Cannot restore data source {render_dump_metadata(restore_dump)} because there are multiple matching data sources configured',
  99. )
  100. return matching_dumps[0]
  101. def strip_path_prefix_from_extracted_dump_destination(
  102. destination_path,
  103. borgmatic_runtime_directory,
  104. ):
  105. '''
  106. Directory-format dump files get extracted into a temporary directory containing a path prefix
  107. that depends how the files were stored in the archive. So, given the destination path where the
  108. dump was extracted and the borgmatic runtime directory, move the dump files such that the
  109. restore doesn't have to deal with that varying path prefix.
  110. For instance, if the dump was extracted to:
  111. /run/user/0/borgmatic/tmp1234/borgmatic/postgresql_databases/test/...
  112. or:
  113. /run/user/0/borgmatic/tmp1234/root/.borgmatic/postgresql_databases/test/...
  114. then this function moves it to:
  115. /run/user/0/borgmatic/postgresql_databases/test/...
  116. '''
  117. for subdirectory_path, _, _ in os.walk(destination_path):
  118. databases_directory = os.path.basename(subdirectory_path)
  119. if not databases_directory.endswith('_databases'):
  120. continue
  121. shutil.move(
  122. subdirectory_path,
  123. os.path.join(borgmatic_runtime_directory, databases_directory),
  124. )
  125. break
  126. def restore_single_dump(
  127. repository,
  128. config,
  129. local_borg_version,
  130. global_arguments,
  131. local_path,
  132. remote_path,
  133. archive_name,
  134. hook_name,
  135. data_source,
  136. connection_params,
  137. borgmatic_runtime_directory,
  138. ):
  139. '''
  140. Given (among other things) an archive name, a data source hook name, the hostname, port,
  141. username/password as connection params, and a configured data source configuration dict, restore
  142. that data source from the archive.
  143. '''
  144. dump_metadata = render_dump_metadata(
  145. Dump(
  146. hook_name,
  147. data_source['name'],
  148. data_source.get('hostname'),
  149. data_source.get('port'),
  150. data_source.get('label') or data_source.get('container') or UNSPECIFIED,
  151. ),
  152. )
  153. logger.info(f'Restoring data source {dump_metadata}')
  154. dump_patterns = borgmatic.hooks.dispatch.call_hooks(
  155. 'make_data_source_dump_patterns',
  156. config,
  157. borgmatic.hooks.dispatch.Hook_type.DATA_SOURCE,
  158. borgmatic_runtime_directory,
  159. data_source['name'],
  160. )[hook_name.split('_databases', 1)[0]]
  161. destination_path = (
  162. tempfile.mkdtemp(dir=borgmatic_runtime_directory)
  163. if data_source.get('format') == 'directory'
  164. else None
  165. )
  166. try:
  167. # Kick off a single data source extract. If using a directory format, extract to a temporary
  168. # directory. Otherwise extract the single dump file to stdout.
  169. extract_process = borgmatic.borg.extract.extract_archive(
  170. dry_run=global_arguments.dry_run,
  171. repository=repository['path'],
  172. archive=archive_name,
  173. paths=[
  174. borgmatic.hooks.data_source.dump.convert_glob_patterns_to_borg_pattern(
  175. dump_patterns,
  176. ),
  177. ],
  178. config=config,
  179. local_borg_version=local_borg_version,
  180. global_arguments=global_arguments,
  181. local_path=local_path,
  182. remote_path=remote_path,
  183. destination_path=destination_path,
  184. # A directory format dump isn't a single file, and therefore can't extract
  185. # to stdout. In this case, the extract_process return value is None.
  186. extract_to_stdout=bool(data_source.get('format') != 'directory'),
  187. )
  188. if destination_path and not global_arguments.dry_run:
  189. strip_path_prefix_from_extracted_dump_destination(
  190. destination_path,
  191. borgmatic_runtime_directory,
  192. )
  193. finally:
  194. if destination_path and not global_arguments.dry_run:
  195. shutil.rmtree(destination_path, ignore_errors=True)
  196. # Run a single data source restore, consuming the extract stdout (if any).
  197. borgmatic.hooks.dispatch.call_hook(
  198. function_name='restore_data_source_dump',
  199. config=config,
  200. hook_name=hook_name,
  201. data_source=data_source,
  202. dry_run=global_arguments.dry_run,
  203. extract_process=extract_process,
  204. connection_params=connection_params,
  205. borgmatic_runtime_directory=borgmatic_runtime_directory,
  206. )
  207. def collect_dumps_from_archive(
  208. repository,
  209. archive,
  210. config,
  211. local_borg_version,
  212. global_arguments,
  213. local_path,
  214. remote_path,
  215. borgmatic_runtime_directory,
  216. ):
  217. '''
  218. Given a local or remote repository path, a resolved archive name, a configuration dict, the
  219. local Borg version, global arguments an argparse.Namespace, local and remote Borg paths, and the
  220. borgmatic runtime directory, query the archive for the names of data sources dumps it contains
  221. and return them as a set of Dump instances.
  222. '''
  223. dumps_from_archive = set()
  224. # There is (at most) one dump metadata file per data source hook. Load each.
  225. for dumps_metadata_path in borgmatic.borg.list.capture_archive_listing(
  226. repository,
  227. archive,
  228. config,
  229. local_borg_version,
  230. global_arguments,
  231. list_paths=[
  232. 'sh:'
  233. + borgmatic.hooks.data_source.dump.make_data_source_dump_path(
  234. base_directory,
  235. '*_databases/dumps.json',
  236. )
  237. # Probe for dump metadata files in multiple locations, as the default location is
  238. # "/borgmatic/*_databases/dumps.json" with Borg 1.4+, but instead begins with the
  239. # borgmatic runtime directory for older versions of Borg.
  240. for base_directory in (
  241. 'borgmatic',
  242. borgmatic.config.paths.make_runtime_directory_glob(borgmatic_runtime_directory),
  243. )
  244. ],
  245. local_path=local_path,
  246. remote_path=remote_path,
  247. ):
  248. if not dumps_metadata_path:
  249. continue
  250. dumps_from_archive.update(
  251. set(
  252. borgmatic.hooks.data_source.dump.parse_data_source_dumps_metadata(
  253. borgmatic.borg.extract.extract_archive(
  254. global_arguments.dry_run,
  255. repository,
  256. archive,
  257. [dumps_metadata_path],
  258. config,
  259. local_borg_version,
  260. global_arguments,
  261. local_path=local_path,
  262. remote_path=remote_path,
  263. extract_to_stdout=True,
  264. )
  265. .stdout.read()
  266. .decode(),
  267. dumps_metadata_path,
  268. )
  269. )
  270. )
  271. # If we've successfully loaded any dumps metadata, we're done.
  272. if dumps_from_archive:
  273. logger.debug('Collecting database dumps from archive data source dumps metadata files')
  274. return dumps_from_archive
  275. # No dumps metadata files were found, so for backwards compatibility, fall back to parsing the
  276. # paths of dumps found in the archive to get their respective dump metadata.
  277. logger.debug('Collecting database dumps from archive data source dump paths (fallback)')
  278. borgmatic_source_directory = str(
  279. pathlib.Path(borgmatic.config.paths.get_borgmatic_source_directory(config)),
  280. )
  281. # Probe for the data source dumps in multiple locations, as the default location has moved to
  282. # the borgmatic runtime directory (which gets stored as just "/borgmatic" with Borg 1.4+). But
  283. # we still want to support reading dumps from previously created archives as well.
  284. dump_paths = borgmatic.borg.list.capture_archive_listing(
  285. repository,
  286. archive,
  287. config,
  288. local_borg_version,
  289. global_arguments,
  290. list_paths=[
  291. 'sh:'
  292. + borgmatic.hooks.data_source.dump.make_data_source_dump_path(
  293. base_directory,
  294. '*_databases/*/*',
  295. )
  296. for base_directory in (
  297. 'borgmatic',
  298. borgmatic.config.paths.make_runtime_directory_glob(borgmatic_runtime_directory),
  299. borgmatic_source_directory.lstrip('/'),
  300. )
  301. ],
  302. local_path=local_path,
  303. remote_path=remote_path,
  304. )
  305. for dump_path in dump_paths:
  306. if not dump_path:
  307. continue
  308. # Probe to find the base directory that's at the start of the dump path.
  309. for base_directory in (
  310. 'borgmatic',
  311. borgmatic_runtime_directory,
  312. borgmatic_source_directory,
  313. ):
  314. try:
  315. (hook_name, host_and_port, data_source_name) = dump_path.split(
  316. base_directory + os.path.sep,
  317. 1,
  318. )[1].split(os.path.sep)[0:3]
  319. except (ValueError, IndexError):
  320. continue
  321. parts = host_and_port.split(':', 1)
  322. if len(parts) == 1:
  323. parts += (None,)
  324. (hostname, port) = parts
  325. try:
  326. port = int(port)
  327. except (ValueError, TypeError):
  328. port = None
  329. dumps_from_archive.add(Dump(hook_name, data_source_name, hostname, port, host_and_port))
  330. # We've successfully parsed the dump path, so need to probe any further.
  331. break
  332. else:
  333. logger.warning(
  334. f'Ignoring invalid data source dump path "{dump_path}" in archive {archive}',
  335. )
  336. return dumps_from_archive
  337. def get_dumps_to_restore(restore_arguments, dumps_from_archive):
  338. '''
  339. Given restore arguments as an argparse.Namespace instance indicating which dumps to restore and
  340. a set of Dump instances representing the dumps found in an archive, return a set of specific
  341. Dump instances from the archive to restore. As part of this, replace any Dump having a data
  342. source name of "all" with multiple named Dump instances as appropriate.
  343. Raise ValueError if any of the requested data source names cannot be found in the archive or if
  344. there are multiple archive dump matches for a given requested dump.
  345. '''
  346. requested_dumps = (
  347. {
  348. Dump(
  349. hook_name=(
  350. (
  351. restore_arguments.hook
  352. if restore_arguments.hook.endswith('_databases')
  353. else f'{restore_arguments.hook}_databases'
  354. )
  355. if restore_arguments.hook
  356. else UNSPECIFIED
  357. ),
  358. data_source_name=name,
  359. hostname=restore_arguments.original_hostname or UNSPECIFIED,
  360. port=restore_arguments.original_port,
  361. label=restore_arguments.original_label,
  362. )
  363. for name in restore_arguments.data_sources or (UNSPECIFIED,)
  364. }
  365. if restore_arguments.hook
  366. or restore_arguments.data_sources
  367. or restore_arguments.original_hostname
  368. or restore_arguments.original_port
  369. or restore_arguments.original_label
  370. else {
  371. Dump(
  372. hook_name=UNSPECIFIED,
  373. data_source_name='all',
  374. hostname=UNSPECIFIED,
  375. port=UNSPECIFIED,
  376. label=UNSPECIFIED,
  377. ),
  378. }
  379. )
  380. missing_dumps = set()
  381. dumps_to_restore = set()
  382. # If there's a requested "all" dump, add every dump from the archive to the dumps to restore.
  383. if any(dump for dump in requested_dumps if dump.data_source_name == 'all'):
  384. dumps_to_restore.update(dumps_from_archive)
  385. # If any archive dump matches a requested dump, add the archive dump to the dumps to restore.
  386. for requested_dump in requested_dumps:
  387. if requested_dump.data_source_name == 'all':
  388. continue
  389. matching_dumps = tuple(
  390. archive_dump
  391. for archive_dump in dumps_from_archive
  392. if dumps_match(requested_dump, archive_dump)
  393. )
  394. if len(matching_dumps) == 0:
  395. missing_dumps.add(requested_dump)
  396. elif len(matching_dumps) == 1:
  397. dumps_to_restore.add(matching_dumps[0])
  398. else:
  399. raise ValueError(
  400. f'Cannot restore data source {render_dump_metadata(requested_dump)} because there are multiple matching dumps in the archive. Try adding flags to disambiguate.',
  401. )
  402. if missing_dumps:
  403. rendered_dumps = ', '.join(
  404. f'{render_dump_metadata(dump)}' for dump in sorted(missing_dumps)
  405. )
  406. raise ValueError(
  407. f"Cannot restore data source dump{'s' if len(missing_dumps) > 1 else ''} {rendered_dumps} missing from archive",
  408. )
  409. return dumps_to_restore
  410. def ensure_requested_dumps_restored(dumps_to_restore, dumps_actually_restored):
  411. '''
  412. Given a set of requested dumps to restore and a set of dumps actually restored, raise ValueError
  413. if any requested dumps to restore weren't restored, indicating that they were missing from the
  414. configuration.
  415. '''
  416. if not dumps_actually_restored:
  417. raise ValueError('No data source dumps were found to restore')
  418. missing_dumps = sorted(
  419. dumps_to_restore - dumps_actually_restored,
  420. key=lambda dump: dump.data_source_name,
  421. )
  422. if missing_dumps:
  423. rendered_dumps = ', '.join(f'{render_dump_metadata(dump)}' for dump in missing_dumps)
  424. raise ValueError(
  425. f"Cannot restore data source{'s' if len(missing_dumps) > 1 else ''} {rendered_dumps} missing from borgmatic's configuration",
  426. )
  427. def run_restore(
  428. repository,
  429. config,
  430. local_borg_version,
  431. restore_arguments,
  432. global_arguments,
  433. local_path,
  434. remote_path,
  435. ):
  436. '''
  437. Run the "restore" action for the given repository, but only if the repository matches the
  438. requested repository in restore arguments.
  439. Raise ValueError if a configured data source could not be found to restore or there's no
  440. matching dump in the archive.
  441. '''
  442. if restore_arguments.repository and not borgmatic.config.validate.repositories_match(
  443. repository,
  444. restore_arguments.repository,
  445. ):
  446. return
  447. logger.info(f'Restoring data sources from archive {restore_arguments.archive}')
  448. with borgmatic.config.paths.Runtime_directory(config) as borgmatic_runtime_directory:
  449. borgmatic.hooks.dispatch.call_hooks_even_if_unconfigured(
  450. 'remove_data_source_dumps',
  451. config,
  452. borgmatic.hooks.dispatch.Hook_type.DATA_SOURCE,
  453. borgmatic_runtime_directory,
  454. global_arguments.dry_run,
  455. )
  456. archive_name = borgmatic.borg.repo_list.resolve_archive_name(
  457. repository['path'],
  458. restore_arguments.archive,
  459. config,
  460. local_borg_version,
  461. global_arguments,
  462. local_path,
  463. remote_path,
  464. )
  465. dumps_from_archive = collect_dumps_from_archive(
  466. repository['path'],
  467. archive_name,
  468. config,
  469. local_borg_version,
  470. global_arguments,
  471. local_path,
  472. remote_path,
  473. borgmatic_runtime_directory,
  474. )
  475. dumps_to_restore = get_dumps_to_restore(restore_arguments, dumps_from_archive)
  476. dumps_actually_restored = set()
  477. connection_params = {
  478. 'container': restore_arguments.container,
  479. 'hostname': restore_arguments.hostname,
  480. 'port': restore_arguments.port,
  481. 'username': restore_arguments.username,
  482. 'password': restore_arguments.password,
  483. 'restore_path': restore_arguments.restore_path,
  484. }
  485. # Restore each dump.
  486. for restore_dump in dumps_to_restore:
  487. found_data_source = get_configured_data_source(
  488. config,
  489. restore_dump,
  490. )
  491. # For a dump that wasn't found via an exact match in the configuration, try to fallback
  492. # to an "all" data source.
  493. if not found_data_source:
  494. found_data_source = get_configured_data_source(
  495. config,
  496. Dump(
  497. restore_dump.hook_name,
  498. 'all',
  499. restore_dump.hostname,
  500. restore_dump.port,
  501. restore_dump.label,
  502. ),
  503. )
  504. if not found_data_source:
  505. continue
  506. found_data_source = dict(found_data_source)
  507. found_data_source['name'] = restore_dump.data_source_name
  508. dumps_actually_restored.add(restore_dump)
  509. restore_single_dump(
  510. repository,
  511. config,
  512. local_borg_version,
  513. global_arguments,
  514. local_path,
  515. remote_path,
  516. archive_name,
  517. restore_dump.hook_name,
  518. dict(found_data_source, schemas=restore_arguments.schemas),
  519. connection_params,
  520. borgmatic_runtime_directory,
  521. )
  522. borgmatic.hooks.dispatch.call_hooks_even_if_unconfigured(
  523. 'remove_data_source_dumps',
  524. config,
  525. borgmatic.hooks.dispatch.Hook_type.DATA_SOURCE,
  526. borgmatic_runtime_directory,
  527. global_arguments.dry_run,
  528. )
  529. ensure_requested_dumps_restored(dumps_to_restore, dumps_actually_restored)