Added instance attribut

This commit is contained in:
Kevin Veen-Birkenbach 2023-12-26 00:27:27 +01:00
parent de59646fc0
commit 5e91e298c4
2 changed files with 27 additions and 13 deletions

View File

@ -35,20 +35,33 @@ def create_backup_directories(base_dir, machine_id, repository_name, backup_time
pathlib.Path(version_dir).mkdir(parents=True, exist_ok=True)
return version_dir
# Muss modifiziert werden.
def get_database_name(container):
database_name = re.split("(_|-)(database|db)", container)[0]
print(f"Extracted database name: {database_name}")
return database_name
def get_instance(container):
instance_name = re.split("(_|-)(database|db|postgres)", container)[0]
print(f"Extracted instance name: {instance_name}")
return instance_name
def backup_database(container, databases, version_dir, db_type):
"""Backup database (MariaDB or PostgreSQL) if applicable."""
print(f"Starting database backup for {container} using {db_type}...")
database_name = get_database_name(container)
database_entry = databases.loc[databases['database'] == database_name].iloc[0]
instance_name = get_instance(container)
# Filter the DataFrame for the given instance_name
database_entries = databases.loc[databases['instance'] == instance_name]
# Check if there are more than one entries
if len(database_entries) > 1:
raise BackupException(f"More than one entry found for instance '{instance_name}'")
# Check if there is no entry
if database_entries.empty:
raise BackupException(f"No entry found for instance '{instance_name}'")
# Get the first (and only) entry
database_entry = database_entries.iloc[0]
backup_destination_dir = os.path.join(version_dir, "sql")
pathlib.Path(backup_destination_dir).mkdir(parents=True, exist_ok=True)
backup_destination_file = os.path.join(backup_destination_dir, f"{database_name}_backup.sql")
backup_destination_file = os.path.join(backup_destination_dir, f"backup.sql")
if db_type == 'mariadb':
backup_command = f"docker exec {container} /usr/bin/mariadb-dump -u {database_entry['username']} -p{database_entry['password']} {database_entry['database']} > {backup_destination_file}"

View File

@ -2,17 +2,17 @@ import pandas as pd
import argparse
import os
def check_and_add_entry(file_path, host, database, username, password):
def check_and_add_entry(file_path, instance, host, database, username, password):
# Check if the file exists and is not empty
if os.path.exists(file_path) and os.path.getsize(file_path) > 0:
# Read the existing CSV file with header
df = pd.read_csv(file_path, sep=';')
else:
# Create a new DataFrame with columns if file does not exist
df = pd.DataFrame(columns=['host', 'database', 'username', 'password'])
df = pd.DataFrame(columns=['instance','host', 'database', 'username', 'password'])
# Check if the entry exists and remove it
mask = (df['host'] == host) & (df['database'] == database) & (df['username'] == username)
mask = (df['instance'] == instance) & (df['host'] == host) & (df['database'] == database) & (df['username'] == username)
if not df[mask].empty:
print("Replacing existing entry.")
df = df[~mask]
@ -20,7 +20,7 @@ def check_and_add_entry(file_path, host, database, username, password):
print("Adding new entry.")
# Create a new DataFrame for the new entry
new_entry = pd.DataFrame([{'host': host, 'database': database, 'username': username, 'password': password}])
new_entry = pd.DataFrame([{'instance': instance, 'host': host, 'database': database, 'username': username, 'password': password}])
# Add (or replace) the entry using concat
df = pd.concat([df, new_entry], ignore_index=True)
@ -31,6 +31,7 @@ def check_and_add_entry(file_path, host, database, username, password):
def main():
parser = argparse.ArgumentParser(description="Check and replace (or add) a database entry in a CSV file.")
parser.add_argument("file_path", help="Path to the CSV file")
parser.add_argument("instance", help="Database instance")
parser.add_argument("host", help="Database host")
parser.add_argument("database", help="Database name")
parser.add_argument("username", help="Username")
@ -38,7 +39,7 @@ def main():
args = parser.parse_args()
check_and_add_entry(args.file_path, args.host, args.database, args.username, args.password)
check_and_add_entry(args.file_path, args.instance, args.host, args.database, args.username, args.password)
if __name__ == "__main__":
main()