| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
set -euo pipefail |
| 15 |
|
| 16 |
OFFSITE_HOST="100.106.221.39" |
| 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 |
|
| 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 |
|
| 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 |
|