sqlite.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import logging
  2. import os
  3. import shlex
  4. from borgmatic.execute import execute_command, execute_command_with_processes
  5. from borgmatic.hooks import dump
  6. logger = logging.getLogger(__name__)
  7. def make_dump_path(config): # pragma: no cover
  8. '''
  9. Make the dump path from the given configuration dict and the name of this hook.
  10. '''
  11. return dump.make_data_source_dump_path(
  12. config.get('borgmatic_source_directory'), 'sqlite_databases'
  13. )
  14. def use_streaming(databases, config, log_prefix):
  15. '''
  16. Given a sequence of SQLite database configuration dicts, a configuration dict (ignored), and a
  17. log prefix (ignored), return whether streaming will be using during dumps.
  18. '''
  19. return any(databases)
  20. def dump_data_sources(databases, config, log_prefix, dry_run):
  21. '''
  22. Dump the given SQLite databases to a named pipe. The databases are supplied as a sequence of
  23. configuration dicts, as per the configuration schema. Use the given configuration dict to
  24. construct the destination path and the given log prefix in any log entries.
  25. Return a sequence of subprocess.Popen instances for the dump processes ready to spew to a named
  26. pipe. But if this is a dry run, then don't actually dump anything and return an empty sequence.
  27. '''
  28. dry_run_label = ' (dry run; not actually dumping anything)' if dry_run else ''
  29. processes = []
  30. logger.info(f'{log_prefix}: Dumping SQLite databases{dry_run_label}')
  31. for database in databases:
  32. database_path = database['path']
  33. if database['name'] == 'all':
  34. logger.warning('The "all" database name has no meaning for SQLite databases')
  35. if not os.path.exists(database_path):
  36. logger.warning(
  37. f'{log_prefix}: No SQLite database at {database_path}; an empty database will be created and dumped'
  38. )
  39. dump_path = make_dump_path(config)
  40. dump_filename = dump.make_data_source_dump_filename(dump_path, database['name'])
  41. if os.path.exists(dump_filename):
  42. logger.warning(
  43. f'{log_prefix}: Skipping duplicate dump of SQLite database at {database_path} to {dump_filename}'
  44. )
  45. continue
  46. command = (
  47. 'sqlite3',
  48. shlex.quote(database_path),
  49. '.dump',
  50. '>',
  51. shlex.quote(dump_filename),
  52. )
  53. logger.debug(
  54. f'{log_prefix}: Dumping SQLite database at {database_path} to {dump_filename}{dry_run_label}'
  55. )
  56. if dry_run:
  57. continue
  58. dump.create_named_pipe_for_dump(dump_filename)
  59. processes.append(execute_command(command, shell=True, run_to_completion=False))
  60. return processes
  61. def remove_data_source_dumps(databases, config, log_prefix, dry_run): # pragma: no cover
  62. '''
  63. Remove the given SQLite database dumps from the filesystem. The databases are supplied as a
  64. sequence of configuration dicts, as per the configuration schema. Use the given configuration
  65. dict to construct the destination path and the given log prefix in any log entries. If this is a
  66. dry run, then don't actually remove anything.
  67. '''
  68. dump.remove_data_source_dumps(make_dump_path(config), 'SQLite', log_prefix, dry_run)
  69. def make_data_source_dump_pattern(databases, config, log_prefix, name=None): # pragma: no cover
  70. '''
  71. Make a pattern that matches the given SQLite databases. The databases are supplied as a sequence
  72. of configuration dicts, as per the configuration schema.
  73. '''
  74. return dump.make_data_source_dump_filename(make_dump_path(config), name)
  75. def restore_data_source_dump(
  76. hook_config, config, log_prefix, data_source, dry_run, extract_process, connection_params
  77. ):
  78. '''
  79. Restore a database from the given extract stream. The database is supplied as a data source
  80. configuration dict, but the given hook configuration is ignored. The given configuration dict is
  81. used to construct the destination path, and the given log prefix is used for any log entries. If
  82. this is a dry run, then don't actually restore anything. Trigger the given active extract
  83. process (an instance of subprocess.Popen) to produce output to consume.
  84. '''
  85. dry_run_label = ' (dry run; not actually restoring anything)' if dry_run else ''
  86. database_path = connection_params['restore_path'] or data_source.get(
  87. 'restore_path', data_source.get('path')
  88. )
  89. logger.debug(f'{log_prefix}: Restoring SQLite database at {database_path}{dry_run_label}')
  90. if dry_run:
  91. return
  92. try:
  93. os.remove(database_path)
  94. logger.warning(f'{log_prefix}: Removed existing SQLite database at {database_path}')
  95. except FileNotFoundError: # pragma: no cover
  96. pass
  97. restore_command = (
  98. 'sqlite3',
  99. database_path,
  100. )
  101. # Don't give Borg local path so as to error on warnings, as "borg extract" only gives a warning
  102. # if the restore paths don't exist in the archive.
  103. execute_command_with_processes(
  104. restore_command,
  105. [extract_process],
  106. output_log_level=logging.DEBUG,
  107. input_file=extract_process.stdout,
  108. )