Implemented support of multiple databases per instance

This commit is contained in:
Kevin Veen-Birkenbach 2024-01-06 13:51:30 +01:00
parent 31133f251e
commit 4388e09937

View File

@ -87,32 +87,32 @@ def backup_database(container, volume_dir, db_type):
raise BackupException(f"No entry found for instance '{instance_name}'") raise BackupException(f"No entry found for instance '{instance_name}'")
# Get the first (and only) entry # Get the first (and only) entry
database_entry = database_entries.iloc[0] for database_entry in database_entries.iloc:
database_name = database_entry['database']
database_username = database_entry['username']
database_password = database_entry['password']
backup_destination_dir = os.path.join(volume_dir, "sql") backup_destination_dir = os.path.join(volume_dir, "sql")
pathlib.Path(backup_destination_dir).mkdir(parents=True, exist_ok=True) pathlib.Path(backup_destination_dir).mkdir(parents=True, exist_ok=True)
backup_destination_file = os.path.join(backup_destination_dir, f"backup.sql") backup_destination_file = os.path.join(backup_destination_dir, f"{database_name}.backup.sql")
if db_type == 'mariadb': 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}" backup_command = f"docker exec {container} /usr/bin/mariadb-dump -u {database_username} -p{database_password} {database_name} > {backup_destination_file}"
elif db_type == 'postgres': elif db_type == 'postgres':
if database_entry['password']: if database_password:
# Include PGPASSWORD in the command when a password is provided # Include PGPASSWORD in the command when a password is provided
backup_command = ( backup_command = (
f"PGPASSWORD={database_entry['password']} docker exec -i {container} " f"PGPASSWORD={database_password} docker exec -i {container} "
f"pg_dump -U {database_entry['username']} -d {database_entry['database']} " f"pg_dump -U {database_username} -d {database_name} "
f"-h localhost > {backup_destination_file}" f"-h localhost > {backup_destination_file}"
) )
else: else:
# Exclude PGPASSWORD and use --no-password when the password is empty # Exclude PGPASSWORD and use --no-password when the password is empty
backup_command = ( backup_command = (
f"docker exec -i {container} pg_dump -U {database_entry['username']} " f"docker exec -i {container} pg_dump -U {database_username} "
f"-d {database_entry['database']} -h localhost --no-password " f"-d {database_name} -h localhost --no-password "
f"> {backup_destination_file}" f"> {backup_destination_file}"
) )
execute_shell_command(backup_command) execute_shell_command(backup_command)
print(f"Database backup for {container} completed.") print(f"Database backup for database {container} completed.")
def get_last_backup_dir(volume_name, current_backup_dir): def get_last_backup_dir(volume_name, current_backup_dir):
"""Get the most recent backup directory for the specified volume.""" """Get the most recent backup directory for the specified volume."""