浏览代码

feat: allow restoring to different port/host/username

Divyansh Singh 2 年之前
父节点
当前提交
f558cb3156
共有 2 个文件被更改,包括 31 次插入4 次删除
  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
                                 Database hostname to connect to. Defaults to
                                 connecting via local Unix socket.
                                 connecting via local Unix socket.
                             example: database.example.org
                             example: database.example.org
+                        restore_hostname:
+                            type: string
+                            description: |
+                                Database hostname to restore to. Defaults to
+                                the "hostname" option.
+                            example: database.example.org
                         port:
                         port:
                             type: integer
                             type: integer
                             description: Port to connect to. Defaults to 5432.
                             description: Port to connect to. Defaults to 5432.
                             example: 5433
                             example: 5433
+                        restore_port:
+                            type: integer
+                            description: Port to restore to. Defaults to the
+                                "port" option.
+                            example: 5433
                         username:
                         username:
                             type: string
                             type: string
                             description: |
                             description: |
@@ -775,6 +786,12 @@ properties:
                                 You probably want to specify the "postgres"
                                 You probably want to specify the "postgres"
                                 superuser here when the database name is "all".
                                 superuser here when the database name is "all".
                             example: dbuser
                             example: dbuser
+                        restore_username:
+                            type: string
+                            description: |
+                                Username with which to restore to the database.
+                                Defaults to the "username" option.
+                            example: dbuser
                         password:
                         password:
                             type: string
                             type: string
                             description: |
                             description: |
@@ -784,6 +801,13 @@ properties:
                                 without a password or you create a ~/.pgpass
                                 without a password or you create a ~/.pgpass
                                 file.
                                 file.
                             example: trustsome1
                             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:
                         format:
                             type: string
                             type: string
                             enum: ['plain', 'custom', 'directory', 'tar']
                             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 = (
     analyze_command = (
         tuple(psql_command)
         tuple(psql_command)
         + ('--no-password', '--no-psqlrc', '--quiet')
         + ('--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 ())
         + (tuple(database['analyze_options'].split(' ')) if 'analyze_options' in database else ())
         + ('--command', 'ANALYZE')
         + ('--command', 'ANALYZE')
     )
     )
@@ -245,6 +245,9 @@ def restore_database_dump(database_config, log_prefix, location_config, dry_run,
 
 
     extra_environment = make_extra_environment(database)
     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}")
     logger.debug(f"{log_prefix}: Restoring PostgreSQL database {database['name']}{dry_run_label}")
     if dry_run:
     if dry_run:
         return
         return