#!/usr/bin/env bash backup() { local volume local target local target_dir local target_name volume="$1" target="$(realpath "$2")" target_dir="$(dirname "$target")" target_name="$(basename "$target")" test -z "$(docker volume ls | grep "$volume")" && { echo "Error: No such volume, aborting" >&2; exit 1; } test -z "$target_dir" && { echo "Error: No base folder found for target=$target" >&2; exit 2; } test -z "$target_name" && { echo "Error: No target name found for target=$target" >&2; exit 3; } set -x docker run --rm --user="$(id -u):$(id -g)" --mount="type=volume,source=$volume,destination=/data,ro=true" --mount="type=bind,source=$target_dir,destination=/data2" busybox tar cvf "/data2/$target_name" "/data" set +x } restore() { local volume local target local target_dir local target_name target="$(realpath "$1")" target_dir="$(dirname "$target")" target_name="$(basename "$target")" volume="$2" test -z "$(docker volume ls | grep "$volume")" && { echo "Error: No such volume, aborting" >&2; exit 1; } test -z "$target_dir" && { echo "Error: No base folder found for target=$target" >&2; exit 2; } test -z "$target_name" && { echo "Error: No target name found for target=$target" >&2; exit 3; } set -x docker run --rm --user="$(id -u):$(id -g)" --mount="type=volume,source=$volume,destination=/data" --mount="type=bind,source=$target_dir,destination=/data2" busybox /bin/sh -c "rm -rf /data/* && cd / && tar xvf '/data2/$target_name'" set +x } main() { local volumes volumes="$(docker volume ls | tail -n+2 | egrep -v '[a-z]+\s+[0-9a-f]+$' | egrep -o '\s.+$' | sed 's/\s//g')" while read -r line; do echo "$line -> $line.tar" backup "$line" "$line.tar" done <<< "$volumes" } "$@"