Skip to main content

max / makenotwork

2.1 KB · 63 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
23 wam_alert() {
24 local title="$1"
25 local body="${2:-}"
26 curl -sf -X POST "$WAM_URL/tickets" \
27 -H "Content-Type: application/json" \
28 -d "{\"title\": \"$title\", \"body\": \"$body\", \"priority\": \"high\", \"source\": \"wal-offsite\"}" \
29 >/dev/null 2>&1 || true
30 }
31
32 if [ ! -d "$WAL_DIR" ]; then
33 echo "[$(date -Iseconds)] WAL-OFFSITE: Archive directory $WAL_DIR does not exist"
34 exit 0
35 fi
36
37 # Count files to sync
38 WAL_COUNT=$(find "$WAL_DIR" -maxdepth 1 -name '0*' -type f 2>/dev/null | wc -l)
39 if [ "$WAL_COUNT" -eq 0 ]; then
40 exit 0
41 fi
42
43 echo "[$(date -Iseconds)] WAL-OFFSITE: Syncing $WAL_COUNT WAL segment(s) to ${OFFSITE_HOST}:${OFFSITE_DIR}"
44
45 if rsync -e "ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=accept-new" \
46 --timeout=120 \
47 "$WAL_DIR"/ \
48 "${OFFSITE_USER}@${OFFSITE_HOST}:${OFFSITE_DIR}/"; then
49 echo "[$(date -Iseconds)] WAL-OFFSITE: Transfer complete"
50 else
51 echo "[$(date -Iseconds)] WAL-OFFSITE: Transfer FAILED"
52 wam_alert "WAL offsite sync failed" "rsync to ${OFFSITE_HOST}:${OFFSITE_DIR} failed. Check Tailscale connectivity and SSH auth."
53 exit 0
54 fi
55
56 # Prune old offsite WAL segments
57 DELETED=$(ssh -o ConnectTimeout=10 "${OFFSITE_USER}@${OFFSITE_HOST}" \
58 "find ${OFFSITE_DIR} -name '0*' -mtime +${OFFSITE_RETENTION_DAYS} -delete -print 2>/dev/null | wc -l" \
59 2>/dev/null || echo "0")
60 if [ "$DELETED" -gt 0 ]; then
61 echo "[$(date -Iseconds)] WAL-OFFSITE: Pruned ${DELETED} segment(s) older than ${OFFSITE_RETENTION_DAYS} days"
62 fi
63