2022-08-30 22:26:52 +02:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
if [ -z "$DOCKER" ]; then
|
|
|
|
DOCKER=docker
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
echo_and_run() {
|
|
|
|
echo "$@"
|
|
|
|
"$@"
|
|
|
|
return $?
|
|
|
|
}
|
2020-08-16 16:29:59 +02:00
|
|
|
|
|
|
|
backup() {
|
|
|
|
volume="$1"
|
|
|
|
target="$(realpath "$2")"
|
|
|
|
target_dir="$(dirname "$target")"
|
|
|
|
target_name="$(basename "$target")"
|
|
|
|
|
2022-08-30 22:26:52 +02:00
|
|
|
test -z "$("$DOCKER" volume ls | grep "$volume")" && { echo "Error: No such volume, aborting" >&2; exit 1; }
|
2020-08-16 16:29:59 +02:00
|
|
|
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; }
|
|
|
|
|
2022-08-30 22:26:52 +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 \
|
|
|
|
"cd /data/ && tar cf '/data2/$target_name' ./* && chown $(id -u):$(id -g) /data2/$target_name"
|
2020-08-16 16:29:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
restore() {
|
|
|
|
target="$(realpath "$1")"
|
|
|
|
target_dir="$(dirname "$target")"
|
|
|
|
target_name="$(basename "$target")"
|
|
|
|
volume="$2"
|
|
|
|
|
2022-08-30 22:26:52 +02:00
|
|
|
test -z "$("$DOCKER" volume ls | grep "$volume")" && { echo "Error: No such volume, aborting" >&2; exit 1; }
|
2020-08-16 16:29:59 +02:00
|
|
|
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
|
2022-08-30 22:26:52 +02:00
|
|
|
"$DOCKER" run --rm \
|
|
|
|
--mount="type=volume,source=$volume,destination=/data" \
|
|
|
|
--mount="type=bind,source=$target_dir,destination=/data2" \
|
|
|
|
busybox /bin/sh -c \
|
|
|
|
"cd /data/ && rm -rf ./* && tar xf '/data2/$target_name'"
|
2020-08-16 16:29:59 +02:00
|
|
|
set +x
|
|
|
|
}
|
|
|
|
|
2022-08-30 22:26:52 +02:00
|
|
|
backup_all() {
|
|
|
|
"$DOCKER" volume ls \
|
|
|
|
| awk '!/^[a-z]+ +[0-9a-f]+$/ && (NR>1) {print $2}' \
|
|
|
|
| while read -r volume_name; do
|
|
|
|
echo "$volume_name -> $volume_name.tar"
|
|
|
|
backup "$volume_name" "$volume_name.tar"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
restore_all() {
|
|
|
|
for tarball in "$@"; do
|
|
|
|
volume_name="${tarball%.tar}"
|
|
|
|
echo "$volume_name -> $volume"
|
|
|
|
if ! "$DOCKER" volume inspect "$volume_name" 1>&2 2>/dev/null; then
|
|
|
|
echo "Error: no such volume $volume_name" >&2
|
|
|
|
exit 4
|
|
|
|
fi
|
|
|
|
restore "$tarball" "$volume_name"
|
|
|
|
done
|
2020-08-16 16:29:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
"$@"
|