From f558cb31563dcf303ca5eebc469a8e462f2ccbe6 Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Mon, 12 Jun 2023 21:54:39 +0530 Subject: [PATCH] feat: allow restoring to different port/host/username --- borgmatic/config/schema.yaml | 24 ++++++++++++++++++++++++ borgmatic/hooks/postgresql.py | 11 +++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/borgmatic/config/schema.yaml b/borgmatic/config/schema.yaml index 903c0432..50abc3fa 100644 --- a/borgmatic/config/schema.yaml +++ b/borgmatic/config/schema.yaml @@ -763,10 +763,21 @@ properties: Database hostname to connect to. Defaults to connecting via local Unix socket. example: database.example.org + restore_hostname: + type: string + description: | + Database hostname to restore to. Defaults to + the "hostname" option. + example: database.example.org port: type: integer description: Port to connect to. Defaults to 5432. example: 5433 + restore_port: + type: integer + description: Port to restore to. Defaults to the + "port" option. + example: 5433 username: type: string description: | @@ -775,6 +786,12 @@ properties: You probably want to specify the "postgres" superuser here when the database name is "all". example: dbuser + restore_username: + type: string + description: | + Username with which to restore to the database. + Defaults to the "username" option. + example: dbuser password: type: string description: | @@ -784,6 +801,13 @@ properties: without a password or you create a ~/.pgpass file. example: trustsome1 + restore_password: + type: string + description: | + Password with which to connect to the database that + is being restored to. Defaults to the "password" + option. + example: trustsome1 format: type: string enum: ['plain', 'custom', 'directory', 'tar'] diff --git a/borgmatic/hooks/postgresql.py b/borgmatic/hooks/postgresql.py index 3325391f..dab1e83a 100644 --- a/borgmatic/hooks/postgresql.py +++ b/borgmatic/hooks/postgresql.py @@ -217,10 +217,10 @@ def restore_database_dump(database_config, log_prefix, location_config, dry_run, analyze_command = ( tuple(psql_command) + ('--no-password', '--no-psqlrc', '--quiet') - + (('--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 ()) - + (('--dbname', database['name']) if not all_databases else ()) + + (('--host', database.get('restore_hostname', database.get('hostname', ())))) + + (('--port', str(database.get('restore_port', database.get('port', ())))) + + (('--username', database.get('restore_username', database.get('username', ())))) + + (('--dbname', database['name']) if not all_databases else ())) + (tuple(database['analyze_options'].split(' ')) if 'analyze_options' in database else ()) + ('--command', 'ANALYZE') ) @@ -245,6 +245,9 @@ def restore_database_dump(database_config, log_prefix, location_config, dry_run, extra_environment = make_extra_environment(database) + if 'restore_password' in database: + extra_environment['PGPASSWORD'] = database['restore_password'] + logger.debug(f"{log_prefix}: Restoring PostgreSQL database {database['name']}{dry_run_label}") if dry_run: return