From 88e691cb29c6c1e3618c5ee7faf645b349517672 Mon Sep 17 00:00:00 2001
From: Linus Nordberg <linus@nordberg.se>
Date: Thu, 31 Mar 2022 08:26:12 +0200
Subject: make executable

---
 scripts/savepads.sh | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
 create mode 100644 scripts/savepads.sh

(limited to 'scripts')

diff --git a/scripts/savepads.sh b/scripts/savepads.sh
new file mode 100644
index 0000000..18a55bc
--- /dev/null
+++ b/scripts/savepads.sh
@@ -0,0 +1,45 @@
+#! /usr/bin/env bash
+
+#
+# A simple script that downloads sigsum's ms and db pads periodcally.
+# Downloaded pads that are older than $age_days are silently removed.
+#
+# It is assumed that both pads contain the pattern $match.  You may want
+# to keep an eye on the resulting log file.  It should always be empty.
+#
+# Crontab at the 16th minute every three hours:
+#
+#     16 */3 * * * /some/path/sigsum-savepads >>/some/log/file 2>&1 
+#
+
+set -eu
+trap EXIT
+
+prefix="https://pad.sigsum.org/p"
+suffix="export/txt"
+age_days=30
+
+dir="$1"; shift
+pads="sigsum-ms sigsum-db"
+match="This pad describes"
+
+function main() {
+	for pad in $pads; do
+		src=$prefix/$pad/$suffix
+		dst=$dir/$(date "+%y%m%d-%H%M%S")_$pad
+		curl "$src" > "$dst" 2>/dev/null ||
+			die_with_error "must fetch $pad"
+
+		grep "$match" "$dst" ||
+			die_with_error "expected \"$match\" to be in $src"
+
+		find "$dir" -type f -mtime +$age_days -regextype posix-extended -regex "^.*[0-9]{6}-[0-9]{6}_$pad$" -delete
+	done
+}
+
+function die_with_error(){
+	echo "$(date \"+%y-%m-%d %H:%M:%S %Z\") [FATA]" "$@" >&2
+	exit 1
+}
+
+main
-- 
cgit v1.2.3