summaryrefslogtreecommitdiff
path: root/forwarddown.in
diff options
context:
space:
mode:
authorErich Eckner <git@eckner.net>2019-10-26 16:45:27 +0200
committerErich Eckner <git@eckner.net>2019-10-26 16:45:27 +0200
commitad5f03c11a6d52aba33b9787ed1dd23296b3c756 (patch)
treedee27fdb9e842812ef69b8d8edc73dc7ee48276a /forwarddown.in
parent78edd03c36070f2a6b31a78847fffca9d4f35d8d (diff)
downloadhardlinkedBackups-ad5f03c11a6d52aba33b9787ed1dd23296b3c756.tar.xz
forwarddown neu
Diffstat (limited to 'forwarddown.in')
-rw-r--r--forwarddown.in111
1 files changed, 111 insertions, 0 deletions
diff --git a/forwarddown.in b/forwarddown.in
new file mode 100644
index 0000000..31b4487
--- /dev/null
+++ b/forwarddown.in
@@ -0,0 +1,111 @@
+#!/bin/bash
+
+[ -r "#ETCDIR#/backup.conf" ] && \
+ . "#ETCDIR#/backup.conf"
+
+usage()
+{
+ >&2 echo \
+'Usage: forwarddown key-id date backup-id1 backup-id2 ...
+Write encrypted, compressed tar from backed up files not newer than date to stdout.
+
+Options:
+ key-id key id of key to encrypt to
+ date backup should not be newer than the given date
+ backup-id[n] backups to restore
+#HELPTEXT# #'
+ [ -z "$1" ] && exit 1
+ exit $1
+}
+
+if [ $# -eq 1 ]; then
+ if [ "$1" == "--help" ]; then
+ usage 0
+ elif [ "$1" == "--version" ]; then
+ echo '#VERSION#'
+ exit
+ fi
+fi
+
+if [ $# -lt 3 ]; then
+ usage
+fi
+
+key_id=$(
+ printf '%s\n' "$1" \
+ | tr '[:lower:]' '[:upper:]'
+)
+shift
+
+if ! gpg --list-keys --with-colon "${key_id}" \
+| grep -qxF 'fpr:::::::::'"${key_id}"':'; then
+ >&2 printf 'Cannot find gpg key with fingerprint "%s"\n' "${key_id}"
+ usage
+fi
+
+if ! date=$(date -u '+%Y_%m_%d' -d"${1}"); then
+ >&2 printf 'Cannot parse date "%s"\n' "${1}"
+ usage
+fi
+shift
+
+forwarddown_dirs=()
+
+for backup_id in "$@"; do
+ if [ -z "${backups[${backup_id}]}" ]; then
+ >&2 printf 'backup id "%s" is unknown\n' "${backup_id}"
+ usage
+ fi
+ forwarddown_dirs+=("${backups[${backup_id}]## *}")
+done
+
+date=$(
+ {
+ find "${forwarddown_dirs[@]%% *}" -mindepth 1 -maxdepth 1 -type d -printf '0 %f\n'
+ printf '1 %s\n' "${date}"
+ } \
+ | sort -k2,2 -k1,1 \
+ | sed '
+ s/^0 //
+ t
+ /^1 / q
+ ' \
+ | uniq -c \
+ | sed '
+ s/^\s*'"${#forwarddown_dirs[@]}"'\s\+//
+ t
+ d
+ ' \
+ | tail -n1
+)
+
+transforms=(
+ $(
+ for backup_id in "$@"; do
+ printf -- '--transform=s@^%s/%s/@%s/%s/@\n' \
+ "${backups[${backup_id}]%% *}" \
+ "${date}" \
+ "${date}" \
+ "${backup_id}"
+ done
+ )
+)
+
+directories=(
+ $(
+ for backup_id in "$@"; do
+ printf '%s/%s/\n' \
+ "${backups[${backup_id}]%% *}" \
+ "${date}"
+ done
+ )
+)
+
+tar \
+ -c \
+ -f - \
+ --absolute-names \
+ "${transforms[@]}" \
+ "${directories[@]}" \
+| pigz --best -c - \
+| gpg -e -r "${key_id}" -o - -