restore.py 21 KB

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