From c60b2cc38e69319bef45ca90da99b0328535fe93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kr=C3=B3l?= Date: Mon, 24 Jan 2022 15:30:45 +0100 Subject: [PATCH 01/11] Update dbbackup.py --- dbbackup/management/commands/dbbackup.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dbbackup/management/commands/dbbackup.py b/dbbackup/management/commands/dbbackup.py index 541e8d89..0042285d 100644 --- a/dbbackup/management/commands/dbbackup.py +++ b/dbbackup/management/commands/dbbackup.py @@ -34,7 +34,9 @@ class Command(BaseDbBackupCommand): make_option("-O", "--output-path", default=None, help="Specify where to store on local filesystem"), make_option("-x", "--exclude-tables", default=None, - help="Exclude tables from backup") + help="Exclude tables from backup"), + make_option("-n", "--schema", default=None, + help="Specify schema to limit backup to given schema only") ) @utils.email_uncaught_exception @@ -52,6 +54,7 @@ def handle(self, **options): self.filename = options.get('output_filename') self.path = options.get('output_path') self.exclude_tables = options.get("exclude_tables") + self.schema = options.get("schema") self.storage = get_storage() self.database = options.get('database') or '' From b9464d2817ebe01ef8588600f28389c3828bcc61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kr=C3=B3l?= Date: Mon, 24 Jan 2022 15:34:07 +0100 Subject: [PATCH 02/11] Update postgresql.py --- dbbackup/db/postgresql.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/dbbackup/db/postgresql.py b/dbbackup/db/postgresql.py index 89707ca0..f23b7d0c 100644 --- a/dbbackup/db/postgresql.py +++ b/dbbackup/db/postgresql.py @@ -45,6 +45,8 @@ def _create_dump(self): cmd += ' --exclude-table-data={}'.format(table) if self.drop: cmd += ' --clean' + if self.schema: + cmd += ' -n {}'.format(self.schema) cmd = '{} {} {}'.format(self.dump_prefix, cmd, self.dump_suffix) stdout, stderr = self.run_command(cmd, env=self.dump_env) @@ -56,6 +58,8 @@ def _restore_dump(self, dump): # without this, psql terminates with an exit value of 0 regardless of errors cmd += ' --set ON_ERROR_STOP=on' + if self.schema: + cmd += ' -n {}'.format(self.schema) if self.single_transaction: cmd += ' --single-transaction' cmd += ' {}'.format(self.settings['NAME']) @@ -102,6 +106,8 @@ class PgDumpBinaryConnector(PgDumpConnector): def _create_dump(self): cmd = '{} '.format(self.dump_cmd) cmd = cmd + create_postgres_uri(self) + if self.schema: + cmd += ' -n {}'.format(self.schema) cmd += ' --format=custom' for table in self.exclude: @@ -114,6 +120,8 @@ def _restore_dump(self, dump): dbname = create_postgres_uri(self) cmd = '{} {}'.format(self.restore_cmd, dbname) + if self.schema: + cmd += ' -n {}'.format(self.schema) if self.single_transaction: cmd += ' --single-transaction' if self.drop: From 1765a089d50b487fedb79869f9268159f96047aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kr=C3=B3l?= Date: Mon, 24 Jan 2022 17:05:07 +0100 Subject: [PATCH 03/11] schema --- dbbackup/db/postgresql.py | 4 ++-- dbbackup/management/commands/dbbackup.py | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/dbbackup/db/postgresql.py b/dbbackup/db/postgresql.py index f23b7d0c..5de708da 100644 --- a/dbbackup/db/postgresql.py +++ b/dbbackup/db/postgresql.py @@ -36,6 +36,7 @@ class PgDumpConnector(BaseCommandDBConnector): restore_cmd = 'psql' single_transaction = True drop = True + schema = None def _create_dump(self): cmd = '{} '.format(self.dump_cmd) @@ -47,7 +48,6 @@ def _create_dump(self): cmd += ' --clean' if self.schema: cmd += ' -n {}'.format(self.schema) - cmd = '{} {} {}'.format(self.dump_prefix, cmd, self.dump_suffix) stdout, stderr = self.run_command(cmd, env=self.dump_env) return stdout @@ -106,9 +106,9 @@ class PgDumpBinaryConnector(PgDumpConnector): def _create_dump(self): cmd = '{} '.format(self.dump_cmd) cmd = cmd + create_postgres_uri(self) + if self.schema: cmd += ' -n {}'.format(self.schema) - cmd += ' --format=custom' for table in self.exclude: cmd += ' --exclude-table-data={}'.format(table) diff --git a/dbbackup/management/commands/dbbackup.py b/dbbackup/management/commands/dbbackup.py index 0042285d..7ee4addf 100644 --- a/dbbackup/management/commands/dbbackup.py +++ b/dbbackup/management/commands/dbbackup.py @@ -3,6 +3,7 @@ """ from __future__ import (absolute_import, division, print_function, unicode_literals) +from tkinter.messagebox import NO from django.core.management.base import CommandError @@ -79,6 +80,8 @@ def _save_new_backup(self, database): self.logger.info("Backing Up Database: %s", database['NAME']) # Get backup and name filename = self.connector.generate_filename(self.servername) + if self.schema: + self.connector.schema = self.schema or None outputfile = self.connector.create_dump() # Apply trans if self.compress: From ff730a886e78db64eb8413f6009bd62b547ce8c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kr=C3=B3l?= Date: Mon, 24 Jan 2022 17:08:58 +0100 Subject: [PATCH 04/11] fix --- dbbackup/management/commands/dbbackup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dbbackup/management/commands/dbbackup.py b/dbbackup/management/commands/dbbackup.py index 7ee4addf..ccb0adb4 100644 --- a/dbbackup/management/commands/dbbackup.py +++ b/dbbackup/management/commands/dbbackup.py @@ -3,7 +3,6 @@ """ from __future__ import (absolute_import, division, print_function, unicode_literals) -from tkinter.messagebox import NO from django.core.management.base import CommandError From e4727c6445abd30ab4b0ac96a9c0e6ed11cc586f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kr=C3=B3l?= Date: Mon, 24 Jan 2022 17:18:32 +0100 Subject: [PATCH 05/11] restore --- dbbackup/management/commands/dbrestore.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/dbbackup/management/commands/dbrestore.py b/dbbackup/management/commands/dbrestore.py index 4cacfab3..749bfe8a 100644 --- a/dbbackup/management/commands/dbrestore.py +++ b/dbbackup/management/commands/dbrestore.py @@ -30,7 +30,9 @@ class Command(BaseDbBackupCommand): help="Decrypt data before restoring"), make_option("-p", "--passphrase", help="Passphrase for decrypt file", default=None), make_option("-z", "--uncompress", action='store_true', default=False, - help="Uncompress gzip data before restoring") + help="Uncompress gzip data before restoring"), + make_option("-n", "--schema", default=None, + help="Restore backup to given schema only") ) def handle(self, *args, **options): @@ -48,6 +50,7 @@ def handle(self, *args, **options): self.uncompress = options.get('uncompress') self.passphrase = options.get('passphrase') self.interactive = options.get('interactive') + self.schema = options.get("schema") self.database_name, self.database = self._get_database(options) self.storage = get_storage() self._restore_backup() @@ -73,6 +76,8 @@ def _restore_backup(self): servername=self.servername) self.logger.info("Restoring backup for database '%s' and server '%s'", self.database_name, self.servername) + if self.schema: + self.logger.info("Restoring to schema: %s" % self.schema) self.logger.info("Restoring: %s" % input_filename) if self.decrypt: @@ -91,4 +96,6 @@ def _restore_backup(self): input_file.seek(0) self.connector = get_connector(self.database_name) + if self.schema: + self.connector.schema = self.schema self.connector.restore_dump(input_file) From 99ed451b6fb1a15285ae24753b9a4d11cd374d8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kr=C3=B3l?= Date: Mon, 24 Jan 2022 17:40:08 +0100 Subject: [PATCH 06/11] test --- dbbackup/management/commands/dbrestore.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbbackup/management/commands/dbrestore.py b/dbbackup/management/commands/dbrestore.py index 749bfe8a..e248f5c8 100644 --- a/dbbackup/management/commands/dbrestore.py +++ b/dbbackup/management/commands/dbrestore.py @@ -30,7 +30,7 @@ class Command(BaseDbBackupCommand): help="Decrypt data before restoring"), make_option("-p", "--passphrase", help="Passphrase for decrypt file", default=None), make_option("-z", "--uncompress", action='store_true', default=False, - help="Uncompress gzip data before restoring"), + help="Uncompress gzip data before restoringX"), make_option("-n", "--schema", default=None, help="Restore backup to given schema only") ) From 35d738343b9981cc8765ab4f0d05528cc5ea2057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kr=C3=B3l?= Date: Wed, 26 Jan 2022 17:29:31 +0100 Subject: [PATCH 07/11] typo --- dbbackup/management/commands/dbrestore.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbbackup/management/commands/dbrestore.py b/dbbackup/management/commands/dbrestore.py index e248f5c8..749bfe8a 100644 --- a/dbbackup/management/commands/dbrestore.py +++ b/dbbackup/management/commands/dbrestore.py @@ -30,7 +30,7 @@ class Command(BaseDbBackupCommand): help="Decrypt data before restoring"), make_option("-p", "--passphrase", help="Passphrase for decrypt file", default=None), make_option("-z", "--uncompress", action='store_true', default=False, - help="Uncompress gzip data before restoringX"), + help="Uncompress gzip data before restoring"), make_option("-n", "--schema", default=None, help="Restore backup to given schema only") ) From 9ddaff421d5115c88f8b56b0574cc71a6152053a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kr=C3=B3l?= Date: Mon, 16 May 2022 23:29:57 +0200 Subject: [PATCH 08/11] Added --no-owner restore option --- .vscode/settings.json | 4 ++++ dbbackup/db/postgresql.py | 2 ++ dbbackup/management/commands/dbrestore.py | 7 ++++++- 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..33fe63f7 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "python.linting.flake8Enabled": true, + "python.linting.enabled": true +} \ No newline at end of file diff --git a/dbbackup/db/postgresql.py b/dbbackup/db/postgresql.py index 5de708da..91aee5f9 100644 --- a/dbbackup/db/postgresql.py +++ b/dbbackup/db/postgresql.py @@ -62,6 +62,8 @@ def _restore_dump(self, dump): cmd += ' -n {}'.format(self.schema) if self.single_transaction: cmd += ' --single-transaction' + if self.no_owner: + cmd += ' --no-owner' cmd += ' {}'.format(self.settings['NAME']) cmd = '{} {} {}'.format(self.restore_prefix, cmd, self.restore_suffix) stdout, stderr = self.run_command(cmd, stdin=dump, env=self.restore_env) diff --git a/dbbackup/management/commands/dbrestore.py b/dbbackup/management/commands/dbrestore.py index 749bfe8a..6e6dce73 100644 --- a/dbbackup/management/commands/dbrestore.py +++ b/dbbackup/management/commands/dbrestore.py @@ -32,7 +32,9 @@ class Command(BaseDbBackupCommand): make_option("-z", "--uncompress", action='store_true', default=False, help="Uncompress gzip data before restoring"), make_option("-n", "--schema", default=None, - help="Restore backup to given schema only") + help="Restore backup to given schema only"), + make_option("-o", "--no-owner", default=None, + help="Don't try to set the ownership of the objects to the original owner") ) def handle(self, *args, **options): @@ -51,6 +53,7 @@ def handle(self, *args, **options): self.passphrase = options.get('passphrase') self.interactive = options.get('interactive') self.schema = options.get("schema") + self.no_owner = options.get("no-owner") self.database_name, self.database = self._get_database(options) self.storage = get_storage() self._restore_backup() @@ -98,4 +101,6 @@ def _restore_backup(self): self.connector = get_connector(self.database_name) if self.schema: self.connector.schema = self.schema + if self.no_owner: + self.connector.no_owner = self.no_owner self.connector.restore_dump(input_file) From 5f8e18b094ec56011851d3f53963c063b2e0bfab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kr=C3=B3l?= Date: Tue, 17 May 2022 00:26:48 +0200 Subject: [PATCH 09/11] --no-owner option fix --- dbbackup/management/commands/dbrestore.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dbbackup/management/commands/dbrestore.py b/dbbackup/management/commands/dbrestore.py index 6e6dce73..81574f19 100644 --- a/dbbackup/management/commands/dbrestore.py +++ b/dbbackup/management/commands/dbrestore.py @@ -33,7 +33,7 @@ class Command(BaseDbBackupCommand): help="Uncompress gzip data before restoring"), make_option("-n", "--schema", default=None, help="Restore backup to given schema only"), - make_option("-o", "--no-owner", default=None, + make_option("--no-owner", action='store_true', default=False, help="Don't try to set the ownership of the objects to the original owner") ) @@ -102,5 +102,5 @@ def _restore_backup(self): if self.schema: self.connector.schema = self.schema if self.no_owner: - self.connector.no_owner = self.no_owner + self.connector.no_owner = True self.connector.restore_dump(input_file) From 1bf4f7239b93dde1ec299a6f312ff6acc69af160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kr=C3=B3l?= Date: Tue, 17 May 2022 01:22:40 +0200 Subject: [PATCH 10/11] --no-owner option fix 2 --- dbbackup/db/postgresql.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dbbackup/db/postgresql.py b/dbbackup/db/postgresql.py index 91aee5f9..8d65fbea 100644 --- a/dbbackup/db/postgresql.py +++ b/dbbackup/db/postgresql.py @@ -37,6 +37,7 @@ class PgDumpConnector(BaseCommandDBConnector): single_transaction = True drop = True schema = None + no_owner = False def _create_dump(self): cmd = '{} '.format(self.dump_cmd) @@ -128,6 +129,8 @@ def _restore_dump(self, dump): cmd += ' --single-transaction' if self.drop: cmd += ' --clean' + if self.no_owner: + cmd += ' --no-owner' cmd = '{} {} {}'.format(self.restore_prefix, cmd, self.restore_suffix) stdout, stderr = self.run_command(cmd, stdin=dump, env=self.restore_env) return stdout, stderr From 11d1e9791c77aca0bfe4462aa7beaf5d9133d879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kr=C3=B3l?= Date: Tue, 17 May 2022 19:29:50 +0200 Subject: [PATCH 11/11] no-owner option updated --- dbbackup/management/commands/dbrestore.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbbackup/management/commands/dbrestore.py b/dbbackup/management/commands/dbrestore.py index 81574f19..5f4c6a3b 100644 --- a/dbbackup/management/commands/dbrestore.py +++ b/dbbackup/management/commands/dbrestore.py @@ -33,7 +33,7 @@ class Command(BaseDbBackupCommand): help="Uncompress gzip data before restoring"), make_option("-n", "--schema", default=None, help="Restore backup to given schema only"), - make_option("--no-owner", action='store_true', default=False, + make_option("--no-owner", action='store_true', dest="no-owner", help="Don't try to set the ownership of the objects to the original owner") )