Separate database logic in subclasses
This commit is contained in:
parent
a6f088b648
commit
32a3218ac9
|
@ -1,5 +1,6 @@
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import List
|
||||||
|
|
||||||
from restic_volume_backup import utils
|
from restic_volume_backup import utils
|
||||||
|
|
||||||
|
@ -9,6 +10,7 @@ VOLUME_TYPE_VOLUME = "volume"
|
||||||
|
|
||||||
class Container:
|
class Container:
|
||||||
"""Represents a docker container"""
|
"""Represents a docker container"""
|
||||||
|
container_type = None
|
||||||
|
|
||||||
def __init__(self, data: dict):
|
def __init__(self, data: dict):
|
||||||
self._data = data
|
self._data = data
|
||||||
|
@ -74,15 +76,22 @@ class Container:
|
||||||
"""Is backup enabled for this container?"""
|
"""Is backup enabled for this container?"""
|
||||||
return any([
|
return any([
|
||||||
self.volume_backup_enabled,
|
self.volume_backup_enabled,
|
||||||
self.mysql_backup_enabled,
|
self.database_backup_enabled,
|
||||||
self.mariadb_backup_enabled,
|
|
||||||
self.postgresql_backup_enabled,
|
|
||||||
])
|
])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def volume_backup_enabled(self) -> bool:
|
def volume_backup_enabled(self) -> bool:
|
||||||
return utils.is_true(self.get_label('restic-volume-backup.volumes'))
|
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
|
@property
|
||||||
def mysql_backup_enabled(self) -> bool:
|
def mysql_backup_enabled(self) -> bool:
|
||||||
return utils.is_true(self.get_label('restic-volume-backup.mysql'))
|
return utils.is_true(self.get_label('restic-volume-backup.mysql'))
|
||||||
|
@ -170,15 +179,20 @@ class Container:
|
||||||
|
|
||||||
return volumes
|
return volumes
|
||||||
|
|
||||||
def get_mysql_credentials(self):
|
def get_credentials(self) -> dict:
|
||||||
return {
|
"""dict: get credentials for the service"""
|
||||||
'host': self.hostname,
|
raise NotImplementedError("Base container class don't implement this")
|
||||||
'username': self.get_config_env('MYSQL_USER'),
|
|
||||||
'password': self.get_config_env('MYSQL_PASSWORD'),
|
|
||||||
'port': "3306",
|
|
||||||
}
|
|
||||||
|
|
||||||
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:
|
if not value:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -192,6 +206,7 @@ class Container:
|
||||||
return value.split(',')
|
return value.split(',')
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
|
"""Compare container by id"""
|
||||||
if other is None:
|
if other is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
|
@ -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")
|
Loading…
Reference in New Issue