2
0
Эх сурвалжийг харах

add mongodb support, and sqlite restore path (config option only)

Divyansh Singh 2 жил өмнө
parent
commit
a9386b7a87

+ 52 - 0
borgmatic/config/schema.yaml

@@ -952,16 +952,33 @@ 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 3306.
                             description: Port to connect to. Defaults to 3306.
                             example: 3307
                             example: 3307
+                        restore_port:
+                            type: integer
+                            description: Port to restore to. Defaults to the
+                                "port" option.
+                            example: 5433
                         username:
                         username:
                             type: string
                             type: string
                             description: |
                             description: |
                                 Username with which to connect to the database.
                                 Username with which to connect to the database.
                                 Defaults to the username of the current user.
                                 Defaults to the username of the current user.
                             example: dbuser
                             example: dbuser
+                        restore_username:
+                            type: string
+                            description: |
+                                Username with which to restore the database.
+                                Defaults to the "username" option.
+                            example: dbuser                        
                         password:
                         password:
                             type: string
                             type: string
                             description: |
                             description: |
@@ -970,6 +987,12 @@ properties:
                                 configured to trust the configured username
                                 configured to trust the configured username
                                 without a password.
                                 without a password.
                             example: trustsome1
                             example: trustsome1
+                        restore_password:
+                            type: string
+                            description: |
+                                Password with which to connect to the restore
+                                database. Defaults to the "password" option.
+                            example: trustsome1                        
                         format:
                         format:
                             type: string
                             type: string
                             enum: ['sql']
                             enum: ['sql']
@@ -1047,6 +1070,12 @@ properties:
                                 read_special and one_file_system (see above) to
                                 read_special and one_file_system (see above) to
                                 support dump and restore streaming.
                                 support dump and restore streaming.
                             example: /var/lib/sqlite/users.db
                             example: /var/lib/sqlite/users.db
+                        restore_path:
+                            type: string
+                            description: |
+                                Path to the SQLite database file to restore to.
+                                Defaults to the "path" option.
+                            example: /var/lib/sqlite/users.db
             mongodb_databases:
             mongodb_databases:
                 type: array
                 type: array
                 items:
                 items:
@@ -1069,22 +1098,45 @@ properties:
                                 Database hostname to connect to. Defaults to
                                 Database hostname to connect to. Defaults to
                                 connecting to localhost.
                                 connecting to localhost.
                             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 27017.
                             description: Port to connect to. Defaults to 27017.
                             example: 27018
                             example: 27018
+                        restore_port:
+                            type: integer
+                            description: Port to restore to. Defaults to the
+                                "port" option.
+                            example: 5433                        
                         username:
                         username:
                             type: string
                             type: string
                             description: |
                             description: |
                                 Username with which to connect to the database.
                                 Username with which to connect to the database.
                                 Skip it if no authentication is needed.
                                 Skip it if no authentication is needed.
                             example: dbuser
                             example: dbuser
+                        restore_username:
+                            type: string
+                            description: |
+                                Username with which to restore the database.
+                                Defaults to the "username" option.
+                            example: dbuser                        
                         password:
                         password:
                             type: string
                             type: string
                             description: |
                             description: |
                                 Password with which to connect to the database.
                                 Password with which to connect to the database.
                                 Skip it if no authentication is needed.
                                 Skip it if no authentication is needed.
                             example: trustsome1
                             example: trustsome1
+                        restore_password:
+                            type: string
+                            description: |
+                                Password with which to connect to the restore
+                                database. Defaults to the "password" option.
+                            example: trustsome1
                         authentication_database:
                         authentication_database:
                             type: string
                             type: string
                             description: |
                             description: |

+ 16 - 11
borgmatic/hooks/mongodb.py

@@ -102,7 +102,7 @@ def make_database_dump_pattern(
     return dump.make_database_dump_filename(make_dump_path(location_config), name, hostname='*')
     return dump.make_database_dump_filename(make_dump_path(location_config), name, hostname='*')
 
 
 
 
-def restore_database_dump(database_config, log_prefix, location_config, dry_run, extract_process):
+def restore_database_dump(database_config, log_prefix, location_config, dry_run, extract_process, connection_params):
     '''
     '''
     Restore the given MongoDB database from an extract stream. The database is supplied as a
     Restore the given MongoDB database from an extract stream. The database is supplied as a
     one-element sequence containing a dict describing the database, as per the configuration schema.
     one-element sequence containing a dict describing the database, as per the configuration schema.
@@ -122,7 +122,7 @@ def restore_database_dump(database_config, log_prefix, location_config, dry_run,
     dump_filename = dump.make_database_dump_filename(
     dump_filename = dump.make_database_dump_filename(
         make_dump_path(location_config), database['name'], database.get('hostname')
         make_dump_path(location_config), database['name'], database.get('hostname')
     )
     )
-    restore_command = build_restore_command(extract_process, database, dump_filename)
+    restore_command = build_restore_command(extract_process, database, dump_filename, connection_params)
 
 
     logger.debug(f"{log_prefix}: Restoring MongoDB database {database['name']}{dry_run_label}")
     logger.debug(f"{log_prefix}: Restoring MongoDB database {database['name']}{dry_run_label}")
     if dry_run:
     if dry_run:
@@ -138,10 +138,15 @@ def restore_database_dump(database_config, log_prefix, location_config, dry_run,
     )
     )
 
 
 
 
-def build_restore_command(extract_process, database, dump_filename):
+def build_restore_command(extract_process, database, dump_filename, connection_params):
     '''
     '''
     Return the mongorestore command from a single database configuration.
     Return the mongorestore command from a single database configuration.
     '''
     '''
+    hostname = connection_params['hostname'] or database.get('restore_hostname', database.get('hostname'))
+    port = str(connection_params['port'] or database.get('restore_port', database.get('port')))
+    username = connection_params['username'] or database.get('restore_username', database.get('username'))
+    password = connection_params['password'] or database.get('restore_password', database.get('password'))
+
     command = ['mongorestore']
     command = ['mongorestore']
     if extract_process:
     if extract_process:
         command.append('--archive')
         command.append('--archive')
@@ -149,14 +154,14 @@ def build_restore_command(extract_process, database, dump_filename):
         command.extend(('--dir', dump_filename))
         command.extend(('--dir', dump_filename))
     if database['name'] != 'all':
     if database['name'] != 'all':
         command.extend(('--drop', '--db', database['name']))
         command.extend(('--drop', '--db', database['name']))
-    if 'hostname' in database:
-        command.extend(('--host', database['hostname']))
-    if 'port' in database:
-        command.extend(('--port', str(database['port'])))
-    if 'username' in database:
-        command.extend(('--username', database['username']))
-    if 'password' in database:
-        command.extend(('--password', database['password']))
+    if hostname:
+        command.extend(('--host', hostname))
+    if port:
+        command.extend(('--port', str(port)))
+    if username:
+        command.extend(('--username', username))
+    if password:
+        command.extend(('--password', password))
     if 'authentication_database' in database:
     if 'authentication_database' in database:
         command.extend(('--authenticationDatabase', database['authentication_database']))
         command.extend(('--authenticationDatabase', database['authentication_database']))
     if 'restore_options' in database:
     if 'restore_options' in database: