summaryrefslogtreecommitdiff
path: root/forwarddown.in
diff options
context:
space:
mode:
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 - -