Compare commits

...

2 Commits

Author SHA1 Message Date
e9588b0e31 Added exception catching 2024-01-14 01:46:47 +01:00
42566815c4 solved start bug 2024-01-14 01:25:54 +01:00

View File

@ -150,17 +150,23 @@ def getFileRsyncDestinationPath(volume_dir):
return f"{path}/" return f"{path}/"
def backup_volume(volume_name, volume_dir): def backup_volume(volume_name, volume_dir):
"""Backup files of a volume with incremental backups.""" try:
print(f"Starting backup routine for volume: {volume_name}") """Backup files of a volume with incremental backups."""
files_rsync_destination_path = getFileRsyncDestinationPath(volume_dir) print(f"Starting backup routine for volume: {volume_name}")
pathlib.Path(files_rsync_destination_path).mkdir(parents=True, exist_ok=True) files_rsync_destination_path = getFileRsyncDestinationPath(volume_dir)
pathlib.Path(files_rsync_destination_path).mkdir(parents=True, exist_ok=True)
last_backup_dir = get_last_backup_dir(volume_name, files_rsync_destination_path) last_backup_dir = get_last_backup_dir(volume_name, files_rsync_destination_path)
link_dest_option = f"--link-dest='{last_backup_dir}'" if last_backup_dir else "" link_dest_option = f"--link-dest='{last_backup_dir}'" if last_backup_dir else ""
source_dir = getStoragePath(volume_name) source_dir = getStoragePath(volume_name)
rsync_command = f"rsync -abP --delete --delete-excluded {link_dest_option} {source_dir} {files_rsync_destination_path}" rsync_command = f"rsync -abP --delete --delete-excluded {link_dest_option} {source_dir} {files_rsync_destination_path}"
execute_shell_command(rsync_command) execute_shell_command(rsync_command)
except BackupException as e:
if "file has vanished" in e.args[0]:
print("Warning: Some files vanished before transfer. Continuing.")
else:
raise
print(f"Backup routine for volume: {volume_name} completed.") print(f"Backup routine for volume: {volume_name} completed.")
def get_image_info(container): def get_image_info(container):
@ -170,18 +176,15 @@ def has_image(container,image):
"""Check if the container is using the image""" """Check if the container is using the image"""
image_info = get_image_info(container) image_info = get_image_info(container)
return image in image_info[0] return image in image_info[0]
def stop_containers(containers): def change_containers_status(containers,status):
"""Stop a list of containers.""" """Stop a list of containers."""
container_list = ' '.join(containers) if containers:
print(f"Stopping containers {container_list}...") container_list = ' '.join(containers)
execute_shell_command(f"docker stop {container_list}") print(f"{status} containers {container_list}...")
execute_shell_command(f"docker {status} {container_list}")
def start_containers(containers): else:
"""Start a list of containers.""" print(f"No containers to {status}.")
container_list = ' '.join(containers)
print(f"Start containers {container_list}...")
execute_shell_command(f"docker start {container_list}")
def get_container_with_image(containers,image): def get_container_with_image(containers,image):
for container in containers: for container in containers:
@ -217,15 +220,12 @@ def is_image_ignored(container):
return False return False
def backup_with_containers_paused(volume_name, volume_dir, containers, shutdown): def backup_with_containers_paused(volume_name, volume_dir, containers, shutdown):
if containers: change_containers_status(containers,'stop')
stop_containers(containers)
else:
print(f"{volume_name} has no containers to stop. Skipped.")
backup_volume(volume_name, volume_dir) backup_volume(volume_name, volume_dir)
# Just restart containers if shutdown is false # Just restart containers if shutdown is false
if not shutdown: if not shutdown:
start_containers(containers) change_containers_status(containers,'start')
def backup_mariadb_or_postgres(container, volume_dir): def backup_mariadb_or_postgres(container, volume_dir):
'''Performs database image specific backup procedures''' '''Performs database image specific backup procedures'''