dvbackup/dvbackup.sh

71 lines
2.2 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; }
"$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; }
set -x
"$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'"
set +x
}
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
}
"$@"