From 32a3218ac99faf7c646d7671812b5aa255d09e1e Mon Sep 17 00:00:00 2001 From: Einar Forselv Date: Tue, 3 Dec 2019 01:29:41 +0100 Subject: [PATCH] Separate database logic in subclasses --- restic_volume_backup/containers.py | 37 ++++++++++----- restic_volume_backup/containers_db.py | 65 +++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 restic_volume_backup/containers_db.py diff --git a/restic_volume_backup/containers.py b/restic_volume_backup/containers.py index 3e04215..a7191bd 100644 --- a/restic_volume_backup/containers.py +++ b/restic_volume_backup/containers.py @@ -1,5 +1,6 @@ import os from pathlib import Path +from typing import List from restic_volume_backup import utils @@ -9,6 +10,7 @@ VOLUME_TYPE_VOLUME = "volume" class Container: """Represents a docker container""" + container_type = None def __init__(self, data: dict): self._data = data @@ -74,15 +76,22 @@ class Container: """Is backup enabled for this container?""" return any([ self.volume_backup_enabled, - self.mysql_backup_enabled, - self.mariadb_backup_enabled, - self.postgresql_backup_enabled, + self.database_backup_enabled, ]) @property def volume_backup_enabled(self) -> bool: return utils.is_true(self.get_label('restic-volume-backup.volumes')) + @property + def database_backup_enabled(self) -> bool: + """bool: Is database backup enabled in any shape or form?""" + return any([ + self.mysql_backup_enabled, + self.mariadb_backup_enabled, + self.postgresql_backup_enabled, + ]) + @property def mysql_backup_enabled(self) -> bool: return utils.is_true(self.get_label('restic-volume-backup.mysql')) @@ -170,15 +179,20 @@ class Container: return volumes - def get_mysql_credentials(self): - return { - 'host': self.hostname, - 'username': self.get_config_env('MYSQL_USER'), - 'password': self.get_config_env('MYSQL_PASSWORD'), - 'port': "3306", - } + def get_credentials(self) -> dict: + """dict: get credentials for the service""" + raise NotImplementedError("Base container class don't implement this") - def _parse_pattern(self, value): + def ping(self) -> bool: + """Check the availability of the service""" + raise NotImplementedError("Base container class don't implement this") + + def dump_command(self) -> list: + """list: create a dump command restic and use to send data through stdin""" + raise NotImplementedError("Base container class don't implement this") + + def _parse_pattern(self, value: str) -> List[str]: + """list: Safely parse include/exclude pattern from user""" if not value: return None @@ -192,6 +206,7 @@ class Container: return value.split(',') def __eq__(self, other): + """Compare container by id""" if other is None: return False diff --git a/restic_volume_backup/containers_db.py b/restic_volume_backup/containers_db.py new file mode 100644 index 0000000..b0dcba6 --- /dev/null +++ b/restic_volume_backup/containers_db.py @@ -0,0 +1,65 @@ +from restic_volume_backup.containers import Container +from restic_volume_backup.restic + + +class MariadbContainer(Container): + container_type = 'mariadb' + + def get_credentials(self) -> dict: + """dict: get credentials for the service""" + return { + 'host': self.hostname, + 'username': self.get_config_env('MYSQL_USER'), + 'password': self.get_config_env('MYSQL_PASSWORD'), + 'port': "3306", + } + + def ping(self) -> bool: + """Check the availability of the service""" + raise NotImplementedError("Base container class don't implement this") + + def dump_command(self) -> list: + """list: create a dump command restic and use to send data through stdin""" + raise NotImplementedError("Base container class don't implement this") + + +class MysqlContainer(Container): + container_type = 'mysql' + + def get_credentials(self) -> dict: + """dict: get credentials for the service""" + return { + 'host': self.hostname, + 'username': self.get_config_env('MYSQL_USER'), + 'password': self.get_config_env('MYSQL_PASSWORD'), + 'port': "3306", + } + + def ping(self) -> bool: + """Check the availability of the service""" + raise NotImplementedError("Base container class don't implement this") + + def dump_command(self) -> list: + """list: create a dump command restic and use to send data through stdin""" + raise NotImplementedError("Base container class don't implement this") + + +class PostgresContainer(Container): + container_type = 'postgres' + + def get_credentials(self) -> dict: + """dict: get credentials for the service""" + return { + 'host': self.hostname, + 'username': self.get_config_env('POSTGRES_USER'), + 'password': self.get_config_env('POSTGRES_PASSWORD'), + 'port': "5432", + } + + def ping(self) -> bool: + """Check the availability of the service""" + raise NotImplementedError("Base container class don't implement this") + + def dump_command(self) -> list: + """list: create a dump command restic and use to send data through stdin""" + raise NotImplementedError("Base container class don't implement this")