Made more restrictiv

This commit is contained in:
Kevin Veen-Birkenbach 2021-08-24 19:58:28 +02:00
parent 2d53923538
commit febaa5ac3b

View File

@ -3,45 +3,47 @@
# If rsync stucks consider: # If rsync stucks consider:
# @see https://stackoverflow.com/questions/20773118/rsync-suddenly-hanging-indefinitely-during-transfers # @see https://stackoverflow.com/questions/20773118/rsync-suddenly-hanging-indefinitely-during-transfers
# #
backup_time="$(date '+%Y%m%d%H%M%S')"; echo "start volume backups..."
backups_folder="/Backups/"; backup_time="$(date '+%Y%m%d%H%M%S')" &&
repository_name="$(cd "$(dirname "$(readlink -f "${0}")")" && basename `git rev-parse --show-toplevel`)"; backups_folder="/Backups/" &&
machine_id="$(sha256sum /etc/machine-id | head -c 64)"; repository_name="$(cd "$(dirname "$(readlink -f "${0}")")" && basename `git rev-parse --show-toplevel`)" &&
backup_repository_folder="$backups_folder$machine_id/$repository_name/"; machine_id="$(sha256sum /etc/machine-id | head -c 64)" &&
for volume_name in $(docker volume ls --format '{{.Name}}'); backup_repository_folder="$backups_folder$machine_id/$repository_name/" || exit 1
for volume_name in $(docker volume ls --format '{{.Name}}')
do do
echo "start backup routine: $volume_name"; echo "start backup routine for volume: $volume_name" &&
containers="$(docker ps --filter volume="$volume_name" --format '{{.Names}}')"; containers="$(docker ps --filter volume="$volume_name" --format '{{.Names}}')" &&
containers_array=($containers) containers_array=($containers) &&
container=${containers_array[0]} container=${containers_array[0]} || exit 1
if [ -z "$containers" ] if [ -z "$containers" ]
then then
echo "skipped due to no running containers using this volume." echo "skipped due to no running containers using this volume." || exit 1
else else
echo "stop containers:" && docker stop $containers echo "stop containers:" && docker stop $containers || exit 1
for source_path in $(docker inspect --format "{{ range .Mounts }}{{ if eq .Type \"volume\"}}{{ if eq .Name \"$volume_name\"}}{{ println .Destination }}{{ end }}{{ end }}{{ end }}" "$container"); 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 do
destination_path="$backup_repository_folder""latest/$volume_name"; destination_path="$backup_repository_folder""latest/$volume_name" &&
raw_destination_path="$destination_path/raw" raw_destination_path="$destination_path/raw" &&
prepared_destination_path="$destination_path/prepared" prepared_destination_path="$destination_path/prepared" &&
log_path="$backup_repository_folder""log.txt"; log_path="$backup_repository_folder""log.txt" &&
backup_dir_path="$backup_repository_folder""diffs/$backup_time/$volume_name"; backup_dir_path="$backup_repository_folder""diffs/$backup_time/$volume_name" &&
raw_backup_dir_path="$backup_dir_path/raw"; raw_backup_dir_path="$backup_dir_path/raw" &&
prepared_backup_dir_path="$backup_dir_path/prepared"; prepared_backup_dir_path="$backup_dir_path/prepared" || exit 1
if [ -d "$destination_path" ] if [ -d "$destination_path" ]
then then
echo "backup volume: $volume_name"; echo "backup volume: $volume_name" || exit 1
else else
echo "first backup volume: $volume_name" echo "first backup volume: $volume_name" &&
mkdir -vp "$raw_destination_path"; mkdir -vp "$raw_destination_path" &&
mkdir -vp "$raw_backup_dir_path"; mkdir -vp "$raw_backup_dir_path" &&
mkdir -vp "$prepared_destination_path"; mkdir -vp "$prepared_destination_path" &&
mkdir -vp "$prepared_backup_dir_path"; mkdir -vp "$prepared_backup_dir_path" || exit 1
fi fi
docker run --rm --volumes-from "$container" -v "$backups_folder:$backups_folder" "kevinveenbirkenbach/alpine-rsync" sh -c " 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"; rsync -abP --delete --delete-excluded --log-file=$log_path --backup-dir=$raw_backup_dir_path '$source_path/' $raw_destination_path" &&
echo "start containers:" && docker start $containers; echo "start containers:" && docker start $containers || exit 1
done done
fi fi
echo "end backup routine: $volume_name"; echo "end backup routine for volume: $volume_name" || exit 1
done done
echo "finished backup routine." || exit 1