Browse Source

feat: allow restoring to different port/host/username

Divyansh Singh 2 years ago
parent
commit
f558cb3156
2 changed files with 31 additions and 4 deletions
  1. 24 0
      borgmatic/config/schema.yaml
  2. 7 4
      borgmatic/hooks/postgresql.py

+ 24 - 0
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']

+ 7 - 4
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