From 6b6618fb1bbd711a3cd203ba383ffc3706c30d9a Mon Sep 17 00:00:00 2001 From: lluuaapp <266615+lluuaapp@users.noreply.github.com> Date: Thu, 24 Nov 2022 21:11:35 +0100 Subject: [PATCH 1/5] bareos-fd-postgres: properly close database connection --- .../filed/python/postgres/BareosFdPluginPostgres.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/src/plugins/filed/python/postgres/BareosFdPluginPostgres.py b/core/src/plugins/filed/python/postgres/BareosFdPluginPostgres.py index acfb9108429..7e6e4050a14 100644 --- a/core/src/plugins/filed/python/postgres/BareosFdPluginPostgres.py +++ b/core/src/plugins/filed/python/postgres/BareosFdPluginPostgres.py @@ -452,6 +452,12 @@ def restore_object_data(self, ROP): ) return bareosfd.bRC_OK + def __dbConClose(self): + try: + self.dbCon.close() + except Exception as e: + pass + def closeDbConnection(self): # TODO Error Handling # Get Backup Start Date @@ -469,6 +475,7 @@ def closeDbConnection(self): + "CHECKPOINT LOCATION: %s, " % self.labelItems["CHECKPOINT LOCATION"] + "START WAL LOCATION: %s\n" % self.labelItems["START WAL LOCATION"], ) + self.__dbConClose() self.PostgressFullBackupRunning = False except Exception as e: bareosfd.JobMessage( @@ -523,6 +530,7 @@ def end_backup_file(self): self.files_to_backup.append("ROP") return self.checkForWalFiles() else: + self.__dbConClose() return bareosfd.bRC_OK def end_backup_job(self): @@ -534,6 +542,8 @@ def end_backup_job(self): if self.PostgressFullBackupRunning: self.closeDbConnection() self.PostgressFullBackupRunning = False + else: + self.__dbConClose() return bareosfd.bRC_OK def wait_for_wal_archiving(self, LSN): From e8a7ee885702839a047fc4b5accdd92c3ada7fd6 Mon Sep 17 00:00:00 2001 From: lluuaapp <266615+lluuaapp@users.noreply.github.com> Date: Thu, 1 Dec 2022 17:59:28 +0100 Subject: [PATCH 2/5] bareos-fd-postgres: only catching InterfaceError exception --- .../src/plugins/filed/python/postgres/BareosFdPluginPostgres.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/plugins/filed/python/postgres/BareosFdPluginPostgres.py b/core/src/plugins/filed/python/postgres/BareosFdPluginPostgres.py index 7e6e4050a14..06f188e2872 100644 --- a/core/src/plugins/filed/python/postgres/BareosFdPluginPostgres.py +++ b/core/src/plugins/filed/python/postgres/BareosFdPluginPostgres.py @@ -455,7 +455,7 @@ def restore_object_data(self, ROP): def __dbConClose(self): try: self.dbCon.close() - except Exception as e: + except pg8000.exceptions.InterfaceError as e: pass def closeDbConnection(self): From 6a9f76d4fae7a8b724a95699d541145d0076f075 Mon Sep 17 00:00:00 2001 From: lluuaapp <266615+lluuaapp@users.noreply.github.com> Date: Thu, 1 Dec 2022 18:06:21 +0100 Subject: [PATCH 3/5] bareos-fd-postgres: renamed some methods to something more reasonable --- .../python/postgres/BareosFdPluginPostgres.py | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/core/src/plugins/filed/python/postgres/BareosFdPluginPostgres.py b/core/src/plugins/filed/python/postgres/BareosFdPluginPostgres.py index 06f188e2872..c3cc744595c 100644 --- a/core/src/plugins/filed/python/postgres/BareosFdPluginPostgres.py +++ b/core/src/plugins/filed/python/postgres/BareosFdPluginPostgres.py @@ -452,13 +452,19 @@ def restore_object_data(self, ROP): ) return bareosfd.bRC_OK - def __dbConClose(self): + def close_db_connection(self): + """ + Make sure the DB connection is closed properly. Will not throw if the connection was already closed. + """ try: self.dbCon.close() except pg8000.exceptions.InterfaceError as e: pass - def closeDbConnection(self): + def complete_backup_job_and_close_db(self): + """ + Call pg_stop_backup() on PostgreSQL DB to mark the backup job as completed. + """ # TODO Error Handling # Get Backup Start Date self.parseBackupLabelFile() @@ -475,7 +481,7 @@ def closeDbConnection(self): + "CHECKPOINT LOCATION: %s, " % self.labelItems["CHECKPOINT LOCATION"] + "START WAL LOCATION: %s\n" % self.labelItems["START WAL LOCATION"], ) - self.__dbConClose() + self.close_db_connection() self.PostgressFullBackupRunning = False except Exception as e: bareosfd.JobMessage( @@ -525,12 +531,12 @@ def end_backup_file(self): return bareosfd.bRC_More else: if self.PostgressFullBackupRunning: - self.closeDbConnection() + self.completed_backup_job_and_close_db() # Now we can also create the Restore object with the right timestamp self.files_to_backup.append("ROP") return self.checkForWalFiles() else: - self.__dbConClose() + self.close_db_connection() return bareosfd.bRC_OK def end_backup_job(self): @@ -540,10 +546,10 @@ def end_backup_job(self): especially when job was cancelled """ if self.PostgressFullBackupRunning: - self.closeDbConnection() + self.completed_backup_job_and_close_db() self.PostgressFullBackupRunning = False else: - self.__dbConClose() + self.close_db_connection() return bareosfd.bRC_OK def wait_for_wal_archiving(self, LSN): From 4bcedcc0b73af0998ed93048b6e60ab781239e47 Mon Sep 17 00:00:00 2001 From: lluuaapp <266615+lluuaapp@users.noreply.github.com> Date: Fri, 2 Dec 2022 09:54:31 +0100 Subject: [PATCH 4/5] bareos-fd-postgres: fixed renamed methods --- .../plugins/filed/python/postgres/BareosFdPluginPostgres.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/plugins/filed/python/postgres/BareosFdPluginPostgres.py b/core/src/plugins/filed/python/postgres/BareosFdPluginPostgres.py index c3cc744595c..f2f66867b9f 100644 --- a/core/src/plugins/filed/python/postgres/BareosFdPluginPostgres.py +++ b/core/src/plugins/filed/python/postgres/BareosFdPluginPostgres.py @@ -531,7 +531,7 @@ def end_backup_file(self): return bareosfd.bRC_More else: if self.PostgressFullBackupRunning: - self.completed_backup_job_and_close_db() + self.complete_backup_job_and_close_db() # Now we can also create the Restore object with the right timestamp self.files_to_backup.append("ROP") return self.checkForWalFiles() @@ -546,7 +546,7 @@ def end_backup_job(self): especially when job was cancelled """ if self.PostgressFullBackupRunning: - self.completed_backup_job_and_close_db() + self.complete_backup_job_and_close_db() self.PostgressFullBackupRunning = False else: self.close_db_connection() From 5a91e68d80111534f582ff1702581556072a8a28 Mon Sep 17 00:00:00 2001 From: Andreas Rogge Date: Mon, 5 Dec 2022 12:22:23 +0100 Subject: [PATCH 5/5] Update CHANGELOG.md --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c32b88a566..a2fa7cb8055 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -164,6 +164,7 @@ and since Bareos version 20 this project adheres to [Semantic Versioning](https: - ndmp_tape.cc: do not log current rctx->rec in joblog [PR #1324] - dird: stored: set statistics collection as deprecated [PR #1320] - webui: switch from mod_php to php-fpm [PR #1287] +- bareos-fd-postgres: properly close database connection [PR #1326] ### Deprecated - make_catalog_backup.pl is now a shell wrapper script which will be removed in version 23. @@ -365,6 +366,7 @@ and since Bareos version 20 this project adheres to [Semantic Versioning](https: [PR #1270]: https://github.com/bareos/bareos/pull/1270 [PR #1271]: https://github.com/bareos/bareos/pull/1271 [PR #1272]: https://github.com/bareos/bareos/pull/1272 +[PR #1273]: https://github.com/bareos/bareos/pull/1273 [PR #1275]: https://github.com/bareos/bareos/pull/1275 [PR #1277]: https://github.com/bareos/bareos/pull/1277 [PR #1278]: https://github.com/bareos/bareos/pull/1278 @@ -374,6 +376,7 @@ and since Bareos version 20 this project adheres to [Semantic Versioning](https: [PR #1283]: https://github.com/bareos/bareos/pull/1283 [PR #1284]: https://github.com/bareos/bareos/pull/1284 [PR #1285]: https://github.com/bareos/bareos/pull/1285 +[PR #1287]: https://github.com/bareos/bareos/pull/1287 [PR #1288]: https://github.com/bareos/bareos/pull/1288 [PR #1295]: https://github.com/bareos/bareos/pull/1295 [PR #1296]: https://github.com/bareos/bareos/pull/1296 @@ -388,5 +391,7 @@ and since Bareos version 20 this project adheres to [Semantic Versioning](https: [PR #1315]: https://github.com/bareos/bareos/pull/1315 [PR #1317]: https://github.com/bareos/bareos/pull/1317 [PR #1318]: https://github.com/bareos/bareos/pull/1318 +[PR #1320]: https://github.com/bareos/bareos/pull/1320 [PR #1324]: https://github.com/bareos/bareos/pull/1324 +[PR #1326]: https://github.com/bareos/bareos/pull/1326 [unreleased]: https://github.com/bareos/bareos/tree/master