From 99f7b387c2a8cecaa2deb2cc4fda392fccd5c1e2 Mon Sep 17 00:00:00 2001 From: "Kevin Veen-Birkenbach [aka. Frantz]" Date: Thu, 19 Aug 2021 22:00:17 +0200 Subject: [PATCH] Highered performance by removing container loop --- docker-volume-backup.sh | 50 ++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/docker-volume-backup.sh b/docker-volume-backup.sh index 4b8e3c6..748b957 100644 --- a/docker-volume-backup.sh +++ b/docker-volume-backup.sh @@ -11,32 +11,32 @@ backup_repository_folder="$backups_folder$machine_id/$repository_name/"; for volume_name in $(docker volume ls --format '{{.Name}}'); do echo "start backup routine: $volume_name"; - for container_name in $(docker ps --filter volume="$volume_name" --format '{{.Names}}'); + containers="$(docker ps --filter volume="$volume_name" --format '{{.Names}}')"; + containers_array=($containers) + container=${containers_array[0]} + echo "stop containers: $containers" && docker stop "$containers" + for source_path in $(docker inspect --format "{{ range .Mounts }}{{ if eq .Type \"volume\"}}{{ if eq .Name \"$volume_name\"}}{{ println .Destination }}{{ end }}{{ end }}{{ end }}" "$container"); do - echo "stop container: $container_name" && docker stop "$container_name" - for source_path in $(docker inspect --format "{{ range .Mounts }}{{ if eq .Type \"volume\"}}{{ if eq .Name \"$volume_name\"}}{{ println .Destination }}{{ end }}{{ end }}{{ end }}" "$container_name"); - do - destination_path="$backup_repository_folder""latest/$volume_name"; - raw_destination_path="$destination_path/raw" - prepared_destination_path="$destination_path/prepared" - log_path="$backup_repository_folder""log.txt"; - backup_dir_path="$backup_repository_folder""diffs/$backup_time/$volume_name"; - raw_backup_dir_path="$backup_dir_path/raw"; - prepared_backup_dir_path="$backup_dir_path/prepared"; - if [ -d "$destination_path" ] - then - echo "backup volume: $volume_name"; - else - echo "first backup volume: $volume_name" - mkdir -vp "$raw_destination_path"; - mkdir -vp "$raw_backup_dir_path"; - mkdir -vp "$prepared_destination_path"; - mkdir -vp "$prepared_backup_dir_path"; - fi - docker run --rm --volumes-from "$container_name" -v "$backups_folder:$backups_folder" "kevinveenbirkenbach/alpine-rsync" sh -c " - rsync -abP --delete --delete-excluded --log-file=$log_path --backup-dir=$raw_backup_dir_path '$source_path/' $raw_destination_path"; - done - echo "start container: $container_name" && docker start "$container_name"; + destination_path="$backup_repository_folder""latest/$volume_name"; + raw_destination_path="$destination_path/raw" + prepared_destination_path="$destination_path/prepared" + log_path="$backup_repository_folder""log.txt"; + backup_dir_path="$backup_repository_folder""diffs/$backup_time/$volume_name"; + raw_backup_dir_path="$backup_dir_path/raw"; + prepared_backup_dir_path="$backup_dir_path/prepared"; + if [ -d "$destination_path" ] + then + echo "backup volume: $volume_name"; + else + echo "first backup volume: $volume_name" + mkdir -vp "$raw_destination_path"; + mkdir -vp "$raw_backup_dir_path"; + mkdir -vp "$prepared_destination_path"; + mkdir -vp "$prepared_backup_dir_path"; + fi + docker run --rm --volumes-from "$container" -v "$backups_folder:$backups_folder" "kevinveenbirkenbach/alpine-rsync" sh -c " + rsync -abP --delete --delete-excluded --log-file=$log_path --backup-dir=$raw_backup_dir_path '$source_path/' $raw_destination_path"; + echo "start containers: $containers" && docker start "$containers"; done echo "end backup routine: $volume_name"; done