complete psql multi schema backup

This commit is contained in:
Divyansh Singh 2023-04-11 23:19:49 +05:30
parent 9bc2322f9a
commit 264cebd2b1
3 changed files with 8 additions and 10 deletions

View File

@ -68,15 +68,12 @@ def restore_single_database(
archive_name, archive_name,
hook_name, hook_name,
database, database,
schemas
): # pragma: no cover ): # pragma: no cover
''' '''
Given (among other things) an archive name, a database hook name, and a configured database Given (among other things) an archive name, a database hook name, and a configured database
configuration dict, restore that database from the archive. configuration dict, restore that database from the archive.
''' '''
logger.info(f'{repository}: Restoring database {database["name"]}') logger.info(f'{repository}: Restoring database {database["name"]}')
if schemas:
database['schemas'] = schemas
dump_pattern = borgmatic.hooks.dispatch.call_hooks( dump_pattern = borgmatic.hooks.dispatch.call_hooks(
'make_database_dump_pattern', 'make_database_dump_pattern',
@ -316,8 +313,7 @@ def run_restore(
remote_path, remote_path,
archive_name, archive_name,
found_hook_name or hook_name, found_hook_name or hook_name,
found_database, dict(found_database, **{'schemas': restore_arguments.schemas}),
schemas = restore_arguments.schemas,
) )
# For any database that weren't found via exact matches in the hooks configuration, try to # For any database that weren't found via exact matches in the hooks configuration, try to
@ -346,8 +342,7 @@ def run_restore(
remote_path, remote_path,
archive_name, archive_name,
found_hook_name or hook_name, found_hook_name or hook_name,
database, dict(database, **{'schemas': restore_arguments.schemas}),
schemas = restore_arguments.schemas,
) )
borgmatic.hooks.dispatch.call_hooks_even_if_unconfigured( borgmatic.hooks.dispatch.call_hooks_even_if_unconfigured(

View File

@ -634,7 +634,7 @@ def make_parsers():
metavar='NAME', metavar='NAME',
nargs='+', nargs='+',
dest='schemas', dest='schemas',
help="Names of schemas to restore from the database, defaults to all schemas." help="Names of schemas to restore from the database, defaults to all schemas. Schemas are only supported for PostgreSQL and MongoDB databases",
) )
restore_group.add_argument( restore_group.add_argument(
'-h', '--help', action='help', help='Show this help message and exit' '-h', '--help', action='help', help='Show this help message and exit'

View File

@ -213,7 +213,6 @@ def restore_database_dump(database_config, log_prefix, location_config, dry_run,
+ ('--command', 'ANALYZE') + ('--command', 'ANALYZE')
) )
pg_restore_command = database.get('pg_restore_command') or 'pg_restore' pg_restore_command = database.get('pg_restore_command') or 'pg_restore'
backup_schemas = ', '.join(database['schemas']) if 'schemas' in database else None
restore_command = ( restore_command = (
(psql_command if all_databases else pg_restore_command, '--no-password') (psql_command if all_databases else pg_restore_command, '--no-password')
+ ( + (
@ -224,10 +223,14 @@ def restore_database_dump(database_config, log_prefix, location_config, dry_run,
+ (('--host', database['hostname']) if 'hostname' in database else ()) + (('--host', database['hostname']) if 'hostname' in database else ())
+ (('--port', str(database['port'])) if 'port' in database else ()) + (('--port', str(database['port'])) if 'port' in database else ())
+ (('--username', database['username']) if 'username' 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 ()) + (tuple(database['restore_options'].split(' ')) if 'restore_options' in database else ())
+ (() if extract_process else (dump_filename,)) + (() if extract_process else (dump_filename,))
) )
if database['schemas']:
for schema in database['schemas']:
restore_command += ('--schema', schema)
extra_environment = make_extra_environment(database) extra_environment = make_extra_environment(database)
logger.debug(f"{log_prefix}: Restoring PostgreSQL database {database['name']}{dry_run_label}") logger.debug(f"{log_prefix}: Restoring PostgreSQL database {database['name']}{dry_run_label}")