mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2024-10-31 23:33:11 +01:00
Implemented support of multiple databases per instance
This commit is contained in:
parent
31133f251e
commit
4388e09937
@ -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']
|
||||||
backup_destination_dir = os.path.join(volume_dir, "sql")
|
database_username = database_entry['username']
|
||||||
pathlib.Path(backup_destination_dir).mkdir(parents=True, exist_ok=True)
|
database_password = database_entry['password']
|
||||||
backup_destination_file = os.path.join(backup_destination_dir, f"backup.sql")
|
backup_destination_dir = os.path.join(volume_dir, "sql")
|
||||||
|
pathlib.Path(backup_destination_dir).mkdir(parents=True, exist_ok=True)
|
||||||
if db_type == 'mariadb':
|
backup_destination_file = os.path.join(backup_destination_dir, f"{database_name}.backup.sql")
|
||||||
backup_command = f"docker exec {container} /usr/bin/mariadb-dump -u {database_entry['username']} -p{database_entry['password']} {database_entry['database']} > {backup_destination_file}"
|
if db_type == 'mariadb':
|
||||||
elif db_type == 'postgres':
|
backup_command = f"docker exec {container} /usr/bin/mariadb-dump -u {database_username} -p{database_password} {database_name} > {backup_destination_file}"
|
||||||
if database_entry['password']:
|
elif db_type == 'postgres':
|
||||||
# Include PGPASSWORD in the command when a password is provided
|
if database_password:
|
||||||
backup_command = (
|
# Include PGPASSWORD in the command when a password is provided
|
||||||
f"PGPASSWORD={database_entry['password']} docker exec -i {container} "
|
backup_command = (
|
||||||
f"pg_dump -U {database_entry['username']} -d {database_entry['database']} "
|
f"PGPASSWORD={database_password} docker exec -i {container} "
|
||||||
f"-h localhost > {backup_destination_file}"
|
f"pg_dump -U {database_username} -d {database_name} "
|
||||||
)
|
f"-h localhost > {backup_destination_file}"
|
||||||
else:
|
)
|
||||||
# Exclude PGPASSWORD and use --no-password when the password is empty
|
else:
|
||||||
backup_command = (
|
# Exclude PGPASSWORD and use --no-password when the password is empty
|
||||||
f"docker exec -i {container} pg_dump -U {database_entry['username']} "
|
backup_command = (
|
||||||
f"-d {database_entry['database']} -h localhost --no-password "
|
f"docker exec -i {container} pg_dump -U {database_username} "
|
||||||
f"> {backup_destination_file}"
|
f"-d {database_name} -h localhost --no-password "
|
||||||
)
|
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."""
|
||||||
|
Loading…
Reference in New Issue
Block a user