42 lines
1.6 KiB
Bash
Executable file
42 lines
1.6 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# Pull the SkateSpots Community Cordn state off the relay host, keep 30 daily
|
|
# snapshots locally, refresh the 0600 working copy, and optionally push the
|
|
# snapshot directory to restic when the normal relay backup is configured.
|
|
#
|
|
# scripts/backup-community-state.sh
|
|
# SKATESPOTS_COMMUNITY_HOST=skatespots-cvm scripts/backup-community-state.sh
|
|
set -euo pipefail
|
|
|
|
HOST="${SKATESPOTS_COMMUNITY_HOST:-skatespots-cvm}"
|
|
REMOTE_STATE="${SKATESPOTS_COMMUNITY_STATE:-/var/lib/skatespots-community/state.json}"
|
|
DEST="${SKATESPOTS_COMMUNITY_BACKUP_DIR:-$HOME/.backups/community}"
|
|
CONFIG_DIR="$HOME/.config/skatespots"
|
|
|
|
mkdir -p "$DEST" "$CONFIG_DIR"
|
|
chmod 700 "$DEST" "$CONFIG_DIR"
|
|
|
|
tmp="$(mktemp)"
|
|
trap 'rm -f "$tmp"' EXIT
|
|
|
|
scp -q "$HOST:$REMOTE_STATE" "$tmp"
|
|
# Refuse to rotate a valid backup out for an empty/corrupt download.
|
|
node -e 'const fs=require("fs");const s=JSON.parse(fs.readFileSync(process.argv[1],"utf8"));if(!s||typeof s!=="object")process.exit(1)' "$tmp"
|
|
|
|
stamp="$(date +%F)"
|
|
install -m 600 "$tmp" "$DEST/state-$stamp.json"
|
|
install -m 600 "$tmp" "$CONFIG_DIR/community-state.json"
|
|
echo "saved $DEST/state-$stamp.json"
|
|
|
|
# Keep 30 daily snapshots.
|
|
ls -1t "$DEST"/state-*.json 2>/dev/null | tail -n +31 | xargs -r rm -f
|
|
|
|
if [[ -f "$CONFIG_DIR/restic.env" ]] && command -v restic >/dev/null 2>&1; then
|
|
set -a
|
|
# shellcheck disable=SC1091
|
|
. "$CONFIG_DIR/restic.env"
|
|
set +a
|
|
restic backup "$DEST" --tag community-state --host "$(hostname)"
|
|
restic forget --tag community-state --keep-daily 30 --prune
|
|
else
|
|
echo "restic not configured; local snapshots only"
|
|
fi
|