mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2025-04-21 01:04:56 +02:00
Implemented fallback_pg_dumpall
This commit is contained in:
parent
5005d577cc
commit
2e2c8131c4
@ -124,22 +124,32 @@ def backup_database(container, volume_dir, db_type):
|
|||||||
backup_destination_file = os.path.join(backup_destination_dir, f"{database_name}.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_username} -p{database_password} {database_name} > {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':
|
execute_shell_command(backup_command)
|
||||||
|
if db_type == 'postgres':
|
||||||
|
cluster_file = os.path.join(backup_destination_dir, f"{instance_name}.cluster.backup.sql")
|
||||||
|
|
||||||
|
if not database_name:
|
||||||
|
fallback_pg_dumpall(container, database_username, database_password, cluster_file)
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
if database_password:
|
if database_password:
|
||||||
# Include PGPASSWORD in the command when a password is provided
|
|
||||||
backup_command = (
|
backup_command = (
|
||||||
f"PGPASSWORD={database_password} docker exec -i {container} "
|
f"PGPASSWORD={database_password} docker exec -i {container} "
|
||||||
f"pg_dump -U {database_username} -d {database_name} "
|
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
|
|
||||||
backup_command = (
|
backup_command = (
|
||||||
f"docker exec -i {container} pg_dump -U {database_username} "
|
f"docker exec -i {container} pg_dump -U {database_username} "
|
||||||
f"-d {database_name} -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)
|
||||||
|
except BackupException as e:
|
||||||
|
print(f"pg_dump failed: {e}")
|
||||||
|
print(f"Falling back to pg_dumpall for instance '{instance_name}'")
|
||||||
|
fallback_pg_dumpall(container, database_username, database_password, cluster_file)
|
||||||
print(f"Database backup for database {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):
|
||||||
@ -162,6 +172,15 @@ def getFileRsyncDestinationPath(volume_dir):
|
|||||||
path = os.path.join(volume_dir, "files")
|
path = os.path.join(volume_dir, "files")
|
||||||
return f"{path}/"
|
return f"{path}/"
|
||||||
|
|
||||||
|
def fallback_pg_dumpall(container, username, password, backup_destination_file):
|
||||||
|
"""Fallback function to run pg_dumpall if pg_dump fails or no DB is defined."""
|
||||||
|
print(f"Running pg_dumpall for container '{container}'...")
|
||||||
|
command = (
|
||||||
|
f"PGPASSWORD={password} docker exec -i {container} "
|
||||||
|
f"pg_dumpall -U {username} -h localhost > {backup_destination_file}"
|
||||||
|
)
|
||||||
|
execute_shell_command(command)
|
||||||
|
|
||||||
def backup_volume(volume_name, volume_dir):
|
def backup_volume(volume_name, volume_dir):
|
||||||
try:
|
try:
|
||||||
"""Backup files of a volume with incremental backups."""
|
"""Backup files of a volume with incremental backups."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user