81 lines
2.6 KiB
Bash
Executable file
81 lines
2.6 KiB
Bash
Executable file
#!/usr/bin/env sh
|
|
|
|
if [ -z "$DOCKER" ]; then
|
|
DOCKER=docker
|
|
fi
|
|
|
|
|
|
echo_and_run() {
|
|
echo "$@"
|
|
"$@"
|
|
return $?
|
|
}
|
|
|
|
backup() {
|
|
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; }
|
|
|
|
echo_and_run "$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"
|
|
}
|
|
|
|
restore() {
|
|
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; }
|
|
|
|
echo_and_run "$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'"
|
|
}
|
|
|
|
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() {
|
|
if [ -z "$DVB_I_KNOW_WHAT_I_DO" ]; then
|
|
printf "The following operation will delete all data in the volumes to be restored, are you sure [y/N]? "
|
|
read -r DVB_I_KNOW_WHAT_I_DO
|
|
fi
|
|
if echo "$DVB_I_KNOW_WHAT_I_DO" | grep -Eviq 't|true|1|y|yes'; then
|
|
echo aborting
|
|
exit 1
|
|
fi
|
|
for tarball in "$@"; do
|
|
volume_name="${tarball%.tar}"
|
|
echo "$volume_name -> $volume"
|
|
if ! "$DOCKER" volume inspect "$volume_name" 1>&2 2>/dev/null; then
|
|
if echo "$DVB_CREATE_VOLUME" | grep -Eiq 't|true|1|y|yes'; then
|
|
"$DOCKER" volume create "$volume_name"
|
|
else
|
|
echo "Error: no such volume $volume_name" >&2
|
|
exit 4
|
|
fi
|
|
fi
|
|
restore "$tarball" "$volume_name"
|
|
done
|
|
}
|
|
|
|
"$@"
|