From 9bc2322f9a38c6ab704be284563b1928c7673172 Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Thu, 6 Apr 2023 02:10:36 +0530 Subject: [PATCH] feat: restore specific schemas --- borgmatic/actions/restore.py | 5 +++++ borgmatic/commands/arguments.py | 7 +++++++ borgmatic/hooks/postgresql.py | 2 ++ 3 files changed, 14 insertions(+) diff --git a/borgmatic/actions/restore.py b/borgmatic/actions/restore.py index bbbb4c7..fa7b04b 100644 --- a/borgmatic/actions/restore.py +++ b/borgmatic/actions/restore.py @@ -68,12 +68,15 @@ def restore_single_database( archive_name, hook_name, database, + schemas ): # pragma: no cover ''' Given (among other things) an archive name, a database hook name, and a configured database configuration dict, restore that database from the archive. ''' logger.info(f'{repository}: Restoring database {database["name"]}') + if schemas: + database['schemas'] = schemas dump_pattern = borgmatic.hooks.dispatch.call_hooks( 'make_database_dump_pattern', @@ -314,6 +317,7 @@ def run_restore( archive_name, found_hook_name or hook_name, found_database, + schemas = restore_arguments.schemas, ) # For any database that weren't found via exact matches in the hooks configuration, try to @@ -343,6 +347,7 @@ def run_restore( archive_name, found_hook_name or hook_name, database, + schemas = restore_arguments.schemas, ) borgmatic.hooks.dispatch.call_hooks_even_if_unconfigured( diff --git a/borgmatic/commands/arguments.py b/borgmatic/commands/arguments.py index 13ee315..7f93910 100644 --- a/borgmatic/commands/arguments.py +++ b/borgmatic/commands/arguments.py @@ -629,6 +629,13 @@ def make_parsers(): dest='databases', help="Names of databases to restore from archive, defaults to all databases. Note that any databases to restore must be defined in borgmatic's configuration", ) + restore_group.add_argument( + '--schema', + metavar='NAME', + nargs='+', + dest='schemas', + help="Names of schemas to restore from the database, defaults to all schemas." + ) restore_group.add_argument( '-h', '--help', action='help', help='Show this help message and exit' ) diff --git a/borgmatic/hooks/postgresql.py b/borgmatic/hooks/postgresql.py index d4799f5..5d951f6 100644 --- a/borgmatic/hooks/postgresql.py +++ b/borgmatic/hooks/postgresql.py @@ -213,6 +213,7 @@ def restore_database_dump(database_config, log_prefix, location_config, dry_run, + ('--command', 'ANALYZE') ) pg_restore_command = database.get('pg_restore_command') or 'pg_restore' + backup_schemas = ', '.join(database['schemas']) if 'schemas' in database else None restore_command = ( (psql_command if all_databases else pg_restore_command, '--no-password') + ( @@ -223,6 +224,7 @@ def restore_database_dump(database_config, log_prefix, location_config, dry_run, + (('--host', database['hostname']) if 'hostname' in database else ()) + (('--port', str(database['port'])) if 'port' in database else ()) + (('--username', database['username']) if 'username' in database else ()) + + (('--schema', backup_schemas) if backup_schemas else ()) + (tuple(database['restore_options'].split(' ')) if 'restore_options' in database else ()) + (() if extract_process else (dump_filename,)) )