witten review

This commit is contained in:
Divyansh Singh 2023-06-15 01:37:18 +05:30
parent 230cf6adc4
commit 67f4d43aec
3 changed files with 18 additions and 23 deletions

View file

@ -79,10 +79,6 @@ def restore_single_database(
f'{repository.get("label", repository["path"])}: Restoring database {database["name"]}' f'{repository.get("label", repository["path"])}: Restoring database {database["name"]}'
) )
logger.info(
f'hostname port username password for database {database["name"]}'
)
dump_pattern = borgmatic.hooks.dispatch.call_hooks( dump_pattern = borgmatic.hooks.dispatch.call_hooks(
'make_database_dump_pattern', 'make_database_dump_pattern',
hooks, hooks,

View file

@ -816,11 +816,7 @@ properties:
OWNER or SET SESSION AUTHORIZATION statements OWNER or SET SESSION AUTHORIZATION statements
to set ownership of created schema elements. to set ownership of created schema elements.
These statements will fail unless the initial These statements will fail unless the initial
connection to the database is made by a superuser connection to the database is made by a superuser.
(in which case they will execute as though wrapped
in SECURITY DEFINER functions). When --no-owner
is used, neither the ALTER OWNER nor SET SESSION
AUTHORIZATION statements will be emitted.
format: format:
type: string type: string
enum: ['plain', 'custom', 'directory', 'tar'] enum: ['plain', 'custom', 'directory', 'tar']

View file

@ -22,17 +22,20 @@ def make_dump_path(location_config): # pragma: no cover
location_config.get('borgmatic_source_directory'), 'postgresql_databases' location_config.get('borgmatic_source_directory'), 'postgresql_databases'
) )
def make_extra_environment(database, restore=False, connection_params=None): def make_extra_environment(database, restore_connection_params=None):
''' '''
Make the extra_environment dict from the given database configuration. Make the extra_environment dict from the given database configuration.
If restore connection params are given, this is for a restore operation.
''' '''
extra = dict() extra = dict()
if 'password' in database: if 'password' in database:
extra['PGPASSWORD'] = database['password'] extra['PGPASSWORD'] = database['password']
if restore and 'restore_password' in database:
extra['PGPASSWORD'] = database['restore_password'] try:
if connection_params is not None and connection_params.get('password'): extra['PGPASSWORD'] = restore_connection_params.get('password') or database['restore_password']
extra['PGPASSWORD'] = connection_params['password'] except (AttributeError, KeyError):
pass
extra['PGSSLMODE'] = database.get('ssl_mode', 'disable') extra['PGSSLMODE'] = database.get('ssl_mode', 'disable')
if 'ssl_cert' in database: if 'ssl_cert' in database:
extra['PGSSLCERT'] = database['ssl_cert'] extra['PGSSLCERT'] = database['ssl_cert']
@ -138,7 +141,7 @@ def dump_databases(databases, 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 ())
+ (('--no-owner',) if database['no_owner'] else ()) + (('--no-owner',) if database.get('no_owner', False) else ())
+ (('--format', dump_format) if dump_format else ()) + (('--format', dump_format) if dump_format else ())
+ (('--file', dump_filename) if dump_format == 'directory' else ()) + (('--file', dump_filename) if dump_format == 'directory' else ())
+ (tuple(database['options'].split(' ')) if 'options' in database else ()) + (tuple(database['options'].split(' ')) if 'options' in database else ())
@ -230,9 +233,9 @@ def restore_database_dump(database_config, log_prefix, location_config, dry_run,
analyze_command = ( analyze_command = (
tuple(psql_command) tuple(psql_command)
+ ('--no-password', '--no-psqlrc', '--quiet') + ('--no-password', '--no-psqlrc', '--quiet')
+ (('--host', hostname) if 'hostname' in database or 'restore_hostname' in database or 'hostname' in connection_params else ()) + (('--host', hostname) if hostname else ())
+ (('--port', port) if 'port' in database or 'restore_port' in database or 'port' in connection_params else ()) + (('--port', port) if port else ())
+ (('--username', username) if 'username' in database or 'restore_username' in database or 'username' in connection_params else ()) + (('--username', username) if username else ())
+ (('--dbname', database['name']) if not all_databases else ()) + (('--dbname', database['name']) if not all_databases else ())
+ (tuple(database['analyze_options'].split(' ')) if 'analyze_options' in database else ()) + (tuple(database['analyze_options'].split(' ')) if 'analyze_options' in database else ())
+ ('--command', 'ANALYZE') + ('--command', 'ANALYZE')
@ -244,10 +247,10 @@ def restore_database_dump(database_config, log_prefix, location_config, dry_run,
+ ('--no-password',) + ('--no-password',)
+ (('--no-psqlrc',) if use_psql_command else ('--if-exists', '--exit-on-error', '--clean')) + (('--no-psqlrc',) if use_psql_command else ('--if-exists', '--exit-on-error', '--clean'))
+ (('--dbname', database['name']) if not all_databases else ()) + (('--dbname', database['name']) if not all_databases else ())
+ (('--host', hostname) if 'hostname' in database or 'restore_hostname' in database or 'hostname' in connection_params else ()) + (('--host', hostname) if hostname else ())
+ (('--port', port) if 'port' in database or 'restore_port' in database or 'port' in connection_params else ()) + (('--port', port) if port else ())
+ (('--username', username) if 'username' in database or 'restore_username' in database or 'username' in connection_params else ()) + (('--username', username) if username else ())
+ (('--no-owner',) if database['no_owner'] else ()) + (('--no-owner',) if database.get('no_owner', False) 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,))
+ tuple( + tuple(
@ -257,7 +260,7 @@ def restore_database_dump(database_config, log_prefix, location_config, dry_run,
) )
) )
extra_environment = make_extra_environment(database, restore=True, connection_params=connection_params) extra_environment = make_extra_environment(database, restore_connection_params=connection_params)
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}")
if dry_run: if dry_run: