From 46d56223ae575430e936e47150b280f8a7e81e40 Mon Sep 17 00:00:00 2001 From: redxef Date: Sun, 16 Aug 2020 16:29:59 +0200 Subject: [PATCH] Init commit. --- dvbackup.sh | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 dvbackup.sh diff --git a/dvbackup.sh b/dvbackup.sh new file mode 100755 index 0000000..58252a9 --- /dev/null +++ b/dvbackup.sh @@ -0,0 +1,50 @@ +#!/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" +} + +"$@"