summaryrefslogtreecommitdiff
path: root/scripts/savepads.sh
blob: f63cc661881d3fce6e4af29d35d14d97e09d66cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 * * * /path/to/savepads.sh /path/to/download/dir >>/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 -q "$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