Menu
AkurAI-Build
publicLatest change b2fb6810b8844f39a9752f65118c05ffecf28160 - Fold AkurAI EC2 operations into Build CLI by Ólafur Búi Ólafsson
set -euo pipefail
source_path="$1"
name="$2"
retention="$3"
case "$source_path" in /tmp/omp-workbench-backup-*.gpg) ;; *) exit 1 ;; esac
[[ "$name" =~ ^workbench-[0-9]{8}T[0-9]{6}-[0-9]+\.sqlite\.gpg$ ]]
[[ "$retention" =~ ^[0-9]+$ ]] && [ "$retention" -ge 2 ] && [ "$retention" -le 365 ]
directory=/var/backups/omp-workbench
sudo install -d -o root -g root -m 0700 "$directory"
scratch="$(sudo mktemp -d "$directory/.verify-XXXXXXXX")"
incoming="$directory/.incoming-$name"
cleanup() {
rm -f "$source_path"
sudo rm -rf "$scratch" "$incoming"
}
trap cleanup EXIT
sudo gpg --batch --homedir /etc/omp-workbench-backup/gnupg \
--output "$scratch/restored.sqlite" --decrypt "$source_path" >/dev/null 2>&1
[ "$(sudo sqlite3 "$scratch/restored.sqlite" 'PRAGMA quick_check;')" = ok ]
sudo install -o root -g root -m 0600 "$source_path" "$incoming"
sudo mv -f "$incoming" "$directory/$name"
mapfile -t backups < <(sudo find "$directory" -maxdepth 1 -type f -name 'workbench-*.sqlite.gpg' -printf '%f\n' | LC_ALL=C sort)
excess=$((${#backups[@]} - retention))
if [ "$excess" -gt 0 ]; then
for ((index = 0; index < excess; index++)); do
sudo rm -- "$directory/${backups[$index]}"
done
fi