Skip to main content

max / makenotwork

2.3 KB · 67 lines History Blame Raw
1 #!/bin/bash
2 # Sync WAL archive to offsite host (astra) via Tailscale.
3 # Runs every 10 minutes via cron.
4 #
5 # Setup on astra:
6 # mkdir -p /opt/backups/mnw/wal
7 #
8 # Setup on Hetzner (as makenotwork user):
9 # Ensure SSH key-based auth to astra is configured (same as sync-backup-offsite.sh).
10 #
11 # Cron (as makenotwork user):
12 # */10 * * * * /opt/makenotwork/sync-wal-offsite.sh >> /opt/makenotwork/wal-archive/sync.log 2>&1
13
14 set -euo pipefail
15
16 OFFSITE_HOST="100.106.221.39" # astra (Tailscale IP)
17 OFFSITE_USER="max"
18 OFFSITE_DIR="/opt/backups/mnw/wal"
19 WAL_DIR="/opt/makenotwork/wal-archive"
20 OFFSITE_RETENTION_DAYS=7
21 WAM_URL="${WAM_URL:-http://127.0.0.1:7890}"
22 WAM_TOKEN="${WAM_TOKEN:-}"
23
24 wam_alert() {
25 local title="$1"
26 local body="${2:-}"
27 local auth=()
28 [ -n "$WAM_TOKEN" ] && auth=(-H "Authorization: Bearer $WAM_TOKEN")
29 curl -sf -X POST "$WAM_URL/tickets" \
30 -H "Content-Type: application/json" \
31 "${auth[@]}" \
32 -d "{\"title\": \"$title\", \"body\": \"$body\", \"priority\": \"high\", \"source\": \"wal-offsite\"}" \
33 >/dev/null 2>&1 || true
34 }
35
36 if [ ! -d "$WAL_DIR" ]; then
37 echo "[$(date -Iseconds)] WAL-OFFSITE: Archive directory $WAL_DIR does not exist"
38 exit 0
39 fi
40
41 # Count files to sync
42 WAL_COUNT=$(find "$WAL_DIR" -maxdepth 1 -name '0*' -type f 2>/dev/null | wc -l)
43 if [ "$WAL_COUNT" -eq 0 ]; then
44 exit 0
45 fi
46
47 echo "[$(date -Iseconds)] WAL-OFFSITE: Syncing $WAL_COUNT WAL segment(s) to ${OFFSITE_HOST}:${OFFSITE_DIR}"
48
49 if rsync -e "ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=accept-new" \
50 --timeout=120 \
51 "$WAL_DIR"/ \
52 "${OFFSITE_USER}@${OFFSITE_HOST}:${OFFSITE_DIR}/"; then
53 echo "[$(date -Iseconds)] WAL-OFFSITE: Transfer complete"
54 else
55 echo "[$(date -Iseconds)] WAL-OFFSITE: Transfer FAILED"
56 wam_alert "WAL offsite sync failed" "rsync to ${OFFSITE_HOST}:${OFFSITE_DIR} failed. Check Tailscale connectivity and SSH auth."
57 exit 0
58 fi
59
60 # Prune old offsite WAL segments
61 DELETED=$(ssh -o ConnectTimeout=10 "${OFFSITE_USER}@${OFFSITE_HOST}" \
62 "find ${OFFSITE_DIR} -name '0*' -mtime +${OFFSITE_RETENTION_DAYS} -delete -print 2>/dev/null | wc -l" \
63 2>/dev/null || echo "0")
64 if [ "$DELETED" -gt 0 ]; then
65 echo "[$(date -Iseconds)] WAL-OFFSITE: Pruned ${DELETED} segment(s) older than ${OFFSITE_RETENTION_DAYS} days"
66 fi
67