Commit eeb624fd authored by Tim Bleimehl's avatar Tim Bleimehl 🤸🏼
Browse files

wip

parent d12f893d
from typing import List, Dict, Union
import logging
from Configs import getConfig
from CoDaBackup.config import DEFAULT
from CoDaBackup.executer import Executer
from CoDaBackup.backup_manager import BackupManager, RetentionType
from pathlib import PurePath, Path
from config import DEFAULT
from executer import Executer
from backup_manager import BackupManager, RetentionType
config: DEFAULT = getConfig(config_classes=[DEFAULT])
log = logging.getLogger("BaseBackupper")
......@@ -18,20 +18,36 @@ class BaseBackupper:
bin_cmd: str = None
backup_file_prefix: str = None
backup_file_suffix: str = None
dir_backups: str = config.BACKUP_DIR
ignore_databases: List[str] = []
def __init__(
self,
executer: Executer,
mode,
container_name,
host: str,
user: str,
password: str,
databases: List[str] = None,
kubernetes_namespace: str = None,
kubernetes_all_namespaces: bool = False,
backups_base_path: Path = config.BACKUP_DIR,
):
self.executer = executer
self.executer = Executer(
mode=mode,
container_name=container_name,
namespace=kubernetes_namespace,
all_namespaces=kubernetes_all_namespaces,
)
base_path = Path(
PurePath(
backups_base_path,
kubernetes_namespace if kubernetes_namespace else "",
)
)
self.manager = BackupManager(
base_path=self.dir_backups,
base_path=base_path,
backup_file_prefix=self.backup_file_prefix,
backup_file_suffix=f"{self.backup_file_suffix}.gz"
if self.compress_backup
......
......@@ -122,16 +122,16 @@ def backup_now(
for container_name in container_names:
if mode == "kubernetes":
namespace, container_name_ = container_name.split("/")
exe = Executer(mode, container_name_, namespace=namespace)
else:
exe = Executer(mode, container_name)
container_name = container_name_
BackupperClass = database_type_backupper_mapping[database_type]
bu = BackupperClass(
executer=exe,
mode=mode,
container_name=container_name,
host=database_host,
user=database_user,
password=database_password,
kubernetes_namespace=namespace,
)
bu.backup(
databases=database_names,
......@@ -158,7 +158,7 @@ def backup_now(
"-d",
type=str,
default=None,
help=f"Where to store the backups. Will default to '{ValidLabels.backup_dir.val}'",
help=f"Define a non standard target dir. Default to '{ValidLabels.backup_dir.val}' or can be specified per container via docker-label '{ValidLabels.backup_dir.key}'",
)
def backup_kubernetes(namespace, all_namespaces, target_dir):
"""Backup databases in a kubernetes environment"""
......@@ -173,10 +173,16 @@ def backup_kubernetes(namespace, all_namespaces, target_dir):
pod.backup_labels[ValidLabels.database_type].val
]
bu = BackupperClass(
executer=Executer("kubernetes", pod.name, namespace, all_namespaces),
mode="kubernetes",
container_name=pod.name,
host=pod.backup_labels[ValidLabels.database_host].val,
user=pod.backup_labels[ValidLabels.database_username].val,
password=pod.backup_labels[ValidLabels.database_password].val,
kubernetes_all_namespaces=all_namespaces,
kubernetes_namespace=pod.kubernetes_namespace,
backups_base_path=target_dir
if target_dir
else pod.backup_labels[ValidLabels.backup_dir].val,
)
bu.manager.retention_duration = {
RetentionType.DAILY: pod.backup_labels[ValidLabels.retention_daily].val,
......@@ -193,15 +199,6 @@ def backup_kubernetes(namespace, all_namespaces, target_dir):
ValidLabels.retention_manual
].val,
}
bu.manager.base_path = Path(
PurePath(
target_dir
if target_dir
else pod.backup_labels[ValidLabels.backup_dir].val,
pod.parent["metadata"]["namespace"],
pod.parent["metadata"]["name"],
)
)
if not pod.backup_labels[ValidLabels.database_names].val:
databases = None
else:
......@@ -216,7 +213,12 @@ def backup_kubernetes(namespace, all_namespaces, target_dir):
@backup_cli.command(name="docker")
@click.argument("target-dir", default="backup/")
@click.option(
"--target-dir",
"-d",
default=None,
help=f"Define a non standard target dir. Default to '{ValidLabels.backup_dir.val}' or can be specified per container via docker-label '{ValidLabels.backup_dir.key}'",
)
def backup_docker(target_dir):
"""Backup databases in a docker environment"""
......@@ -227,20 +229,17 @@ def backup_docker(target_dir):
BackupperClass = database_type_backupper_mapping[
container.backup_labels[ValidLabels.database_type].val
]
print(container.backup_labels)
bu = BackupperClass(
executer=Executer("docker", container.name),
mode="docker",
container_name=container.name,
host=container.backup_labels[ValidLabels.database_host].val,
user=container.backup_labels[ValidLabels.database_username].val,
password=container.backup_labels[ValidLabels.database_password].val,
backups_base_path=target_dir
if target_dir
else container.backup_labels[ValidLabels.backup_dir].val,
)
bu.manager.base_path = Path(target_dir)
if not container.backup_labels[ValidLabels.database_names].val:
databases = None
else:
databases = container.backup_labels[
ValidLabels.database_names
].val.split(",")
bu.manager.retention_duration = {
RetentionType.DAILY: container.backup_labels[
ValidLabels.retention_daily
......@@ -258,12 +257,12 @@ def backup_docker(target_dir):
ValidLabels.retention_manual
].val,
}
bu.manager.base_path = Path(
PurePath(
bu.manager.base_path,
container.backup_labels[ValidLabels.backup_dir].val,
)
)
if not container.backup_labels[ValidLabels.database_names].val:
databases = None
else:
databases = container.backup_labels[
ValidLabels.database_names
].val.split(",")
bu.backup(
databases=databases,
retention_type=RetentionType.DAILY,
......@@ -283,7 +282,7 @@ def restore_cli(debug):
log.setLevel("DEBUG")
@restore_cli.command()
@restore_cli.command(name="now")
@click.option(
"--mode",
default="Docker",
......@@ -425,20 +424,16 @@ def restore_kubernetes_list(
pod.backup_labels[ValidLabels.database_type].val
]
bu = BackupperClass(
executer=Executer("kubernetes", pod.name, namespace, all_namespaces),
mode="kubernetes",
container_name=pod.name,
host=pod.backup_labels[ValidLabels.database_host].val,
user=pod.backup_labels[ValidLabels.database_username].val,
password=pod.backup_labels[ValidLabels.database_password].val,
)
bu.manager.base_path = Path(
PurePath(
source_dir
if source_dir
else pod.backup_labels[ValidLabels.backup_dir].val,
pod.parent["metadata"]["namespace"],
pod.parent["metadata"]["name"],
)
kubernetes_all_namespaces=all_namespaces,
kubernetes_namespace=pod.kubernetes_namespace,
backups_base_path=source_dir
if source_dir
else pod.backup_labels[ValidLabels.backup_dir].val,
)
if databases:
databases = databases.split(",")
......@@ -455,5 +450,13 @@ def restore_kubernetes_list(
click.echo(backup_list)
def restore_docker():
pass
def restore_docker_list():
pass
if __name__ == "__main__":
cli_root()
......@@ -23,6 +23,7 @@ class Container:
other_labels: Dict[Label, Label]
desc: Dict = None
parent: Dict = None
kubernetes_namespace: str = None
@classmethod
def from_docker_inspect_dict(cls, container_inspect_result: dict):
......@@ -54,6 +55,7 @@ class Container:
kubectl_get_item_result["metadata"]["labels"]
),
desc=kubectl_get_item_result,
kubernetes_namespace=kubectl_get_item_result["metadata"]["namespace"],
)
......@@ -114,10 +116,10 @@ class ContainerHelper:
@classmethod
def kubernetes_get_pods_to_be_backed_up(
cls, label: str = None, namespace: str = None, all_namespaces: bool = False
cls, namespace: str = None, all_namespaces: bool = False
) -> List[Container]:
workloads = cls.kubernetes_get_workloads_to_be_backed_up(
label, namespace, all_namespaces
namespace, all_namespaces
)
pods: List[Container] = []
for workload in workloads:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment