2020-08-16 16:29:59 +02:00
|
|
|
#!/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
|
2020-08-16 16:39:29 +02:00
|
|
|
docker run --rm --mount="type=volume,source=$volume,destination=/data,ro=true" --mount="type=bind,source=$target_dir,destination=/data2" busybox /bin/sh -c "tar cvf '/data2/$target_name' /data && chown $(id -u):$(id -g) /data2/$target_name"
|
2020-08-16 16:29:59 +02:00
|
|
|
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
|
2020-08-16 16:39:29 +02:00
|
|
|
docker run --rm --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'"
|
2020-08-16 16:29:59 +02:00
|
|
|
set +x
|
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
|
|
|
local volumes
|
2020-08-16 16:34:02 +02:00
|
|
|
volumes="$(docker volume ls | tail -n+2 | egrep -v '[a-z]+\s+[0-9a-f]+$' | egrep -o '\s.+$' | sed 's/\s//g')"
|
2020-08-16 16:29:59 +02:00
|
|
|
while read -r line; do
|
|
|
|
echo "$line -> $line.tar"
|
|
|
|
backup "$line" "$line.tar"
|
|
|
|
done <<< "$volumes"
|
|
|
|
}
|
|
|
|
|
|
|
|
"$@"
|