Files
davidandClaude Opus 5.5 da16736fb4 Phase 10, part 9: the server package
compose.server.yaml runs Supermarket behind Caddy (HTTPS from Let's
Encrypt, Caddy's own CA or the team's), with Postgres on an internal
network and a nightly pg_dump kept two weeks. install-server.sh asks for
the domain, certificate and mail server, makes the secret key and database
password, starts it all and prints the setup link. Settings → Backups lists
the dumps on a server, and how the operator restores one. Checked end to
end in Docker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 09:37:34 -07:00

27 lines
856 B
Bash
Executable File

#!/bin/sh
# Dumps Supermarket's database every night at 03:00, and keeps 14 days of
# dumps. Runs in the backup service of compose.server.yaml.
set -u
cd /backups || exit 1
umask 077
while true; do
now=$(date +%s)
next=$(date -d "$(date +%Y-%m-%d) 03:00" +%s 2>/dev/null || date -D "%Y-%m-%d %H:%M" -d "$(date +%Y-%m-%d) 03:00" +%s)
[ "$next" -le "$now" ] && next=$((next + 86400))
# The first dump right away, if there's none yet.
if ls supermarket-*.dump > /dev/null 2>&1; then
sleep $((next - now))
fi
name="supermarket-$(date +%Y-%m-%d).dump"
if pg_dump --format=custom --file="$name.part" && mv "$name.part" "$name"; then
echo "backup: wrote $name"
else
echo "backup: pg_dump failed" >&2
rm -f "$name.part"
sleep 600
continue
fi
find /backups -name 'supermarket-*.dump' -mtime +14 -delete
sleep 60
done