From c303a8b452efda2cbdf0be183cc1c99ffe2c175d Mon Sep 17 00:00:00 2001 From: "Kevin Veen-Birkenbach [aka. Frantz]" Date: Thu, 19 Aug 2021 13:43:22 +0200 Subject: [PATCH] Added draft for backup by volume instead of container --- docker-volume-backup.sh | 47 +++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/docker-volume-backup.sh b/docker-volume-backup.sh index e5e3551..e2b9e3b 100644 --- a/docker-volume-backup.sh +++ b/docker-volume-backup.sh @@ -1,29 +1,34 @@ #!/bin/bash -# If rsync stucks consider: +# Just backups volumes of running containers +# If rsync stucks consider: # @see https://stackoverflow.com/questions/20773118/rsync-suddenly-hanging-indefinitely-during-transfers +# backup_time="$(date '+%Y%m%d%H%M%S')"; backups_folder="/Backups/"; -for docker_container_name in $(docker ps --format '{{.Names}}'); +for volume_name in $(docker volume ls --format '{{.Name}}'); do - echo "stop container: $docker_container_name" && docker stop "$docker_container_name" - for source_path in $(docker inspect --format '{{ range .Mounts }}{{ if eq .Type "volume" }}{{ println .Destination }}{{ end }}{{ end }}' "$docker_container_name"); + for docker_container_name in $(docker ps -a --filter volume=$volume_name --format '{{.Names}}'); do - repository_name="$(cd "$(dirname "$(readlink -f "${0}")")" && basename `git rev-parse --show-toplevel`)"; - machine_id="$(sha256sum /etc/machine-id | head -c 64)"; - backup_repository_folder="$backups_folder$machine_id/$repository_name/"; - destination_path="$backup_repository_folder""latest/$docker_container_name$source_path"; - log_path="$backup_repository_folder""log.txt"; - backup_dir_path="$backup_repository_folder""diffs/$backup_time/$docker_container_name$source_path"; - if [ -d "$destination_path" ] - then - echo "backup: $source_path"; - else - echo "first backup: $source_path" - mkdir -vp "$destination_path"; - mkdir -vp "$backup_dir_path"; - fi - docker run --rm --volumes-from "$docker_container_name" -v "$backups_folder:$backups_folder" "kevinveenbirkenbach/alpine-rsync" sh -c " - rsync -abP --delete --delete-excluded --log-file=$log_path --backup-dir=$backup_dir_path '$source_path/' $destination_path"; + echo "stop container: $docker_container_name" && docker stop "$docker_container_name" + for source_path in $(docker inspect --format '{{ range .Mounts }}{{ if eq .Type "volume" }}{{ println .Destination }}{{ end }}{{ end }}' "$docker_container_name"); + do + repository_name="$(cd "$(dirname "$(readlink -f "${0}")")" && basename `git rev-parse --show-toplevel`)"; + machine_id="$(sha256sum /etc/machine-id | head -c 64)"; + backup_repository_folder="$backups_folder$machine_id/$repository_name/"; + destination_path="$backup_repository_folder""latest/$docker_container_name$source_path"; + log_path="$backup_repository_folder""log.txt"; + backup_dir_path="$backup_repository_folder""diffs/$backup_time/$docker_container_name$source_path"; + if [ -d "$destination_path" ] + then + echo "backup: $source_path"; + else + echo "first backup: $source_path" + mkdir -vp "$destination_path"; + mkdir -vp "$backup_dir_path"; + fi + docker run --rm --volumes-from "$docker_container_name" -v "$backups_folder:$backups_folder" "kevinveenbirkenbach/alpine-rsync" sh -c " + rsync -abP --delete --delete-excluded --log-file=$log_path --backup-dir=$backup_dir_path '$source_path/' $destination_path"; + done + echo "start container: $docker_container_name" && docker start "$docker_container_name"; done - echo "start container: $docker_container_name" && docker start "$docker_container_name"; done