#!/usr/bin/env bash # RasterizedDB: two servers that both take writes and replicate every commit to # each other, over TLS, each node pinning the other's certificate. # # Run it on each node. The first run makes this node's replication identity, # prints its fingerprint and stops; give each node the other's fingerprint in # RDB_PEER_FINGERPRINT and run it again to start the server (foreground). # # Node 1 (10.0.0.1): RDB_NODE_ID=1 RDB_NODE_ADDR=10.0.0.1 RDB_PEER_ADDR=10.0.0.2 ./setup.sh # Node 2 (10.0.0.2): RDB_NODE_ID=2 RDB_NODE_ADDR=10.0.0.2 RDB_PEER_ADDR=10.0.0.1 ./setup.sh set -euo pipefail RDB_BIN="${RDB_BIN:-rasterizeddb}" RDB_DATA="${RDB_DATA:-./data}" RDB_IDENTITY_DIR="${RDB_IDENTITY_DIR:-./replication-identity}" # this node's certificate and key RDB_NODE_ID="${RDB_NODE_ID:-}" # required: 1 on one node, 2 on the other RDB_NODE_ADDR="${RDB_NODE_ADDR:-}" # required: this node's address, which the peer connects to RDB_PEER_ADDR="${RDB_PEER_ADDR:-}" # required: the other node's address RDB_PEER_NAME="${RDB_PEER_NAME:-peer}" # a label for the other node in SHOW REPLICATION RDB_PEER_FINGERPRINT="${RDB_PEER_FINGERPRINT:-}" RDB_REPL_PORT="${RDB_REPL_PORT:-61171}" # this node's replication port RDB_PEER_PORT="${RDB_PEER_PORT:-61171}" # the other node's replication port RDB_REPL_MODE="${RDB_REPL_MODE:-async}" # async, sync, quorum:N or all RDB_REPL_CONFLICT="${RDB_REPL_CONFLICT:-abort}" # abort, skip or last-writer-wins RDB_CLUSTER="${RDB_CLUSTER:-rasterizeddb}" # both nodes must use the same name RDB_PG_LISTEN="${RDB_PG_LISTEN:-0.0.0.0}" # where applications reach PostgreSQL RDB_TLS_CERT="${RDB_TLS_CERT:-}" # optional certificate for the SQL listeners RDB_TLS_KEY="${RDB_TLS_KEY:-}" die() { echo "error: $*" >&2; exit 1; } command -v "$RDB_BIN" >/dev/null 2>&1 || die "'$RDB_BIN' not found; set RDB_BIN to the server binary" [ -n "$RDB_NODE_ID" ] && [ "$RDB_NODE_ID" != 0 ] || die "set RDB_NODE_ID (1 on one node, 2 on the other; never 0)" [ -n "$RDB_NODE_ADDR" ] || die "set RDB_NODE_ADDR to this node's address" [ -n "$RDB_PEER_ADDR" ] || die "set RDB_PEER_ADDR to the other node's address" # This node's identity: made once, kept. mkdir -p "$RDB_IDENTITY_DIR" RDB_IDENTITY_DIR="$(cd "$RDB_IDENTITY_DIR" && pwd)" cert="$RDB_IDENTITY_DIR/replication-cert.pem" key="$RDB_IDENTITY_DIR/replication-key.pem" if [ ! -f "$cert" ] || [ ! -f "$key" ]; then out="$("$RDB_BIN" --replication-generate-identity "$RDB_IDENTITY_DIR" --replication-addr "$RDB_NODE_ADDR")" echo "$out" echo "$out" | sed -n 's/^fingerprint: *//p' > "$RDB_IDENTITY_DIR/fingerprint" fi mine="$(cat "$RDB_IDENTITY_DIR/fingerprint" 2>/dev/null || true)" [ -n "$mine" ] || die "no fingerprint saved in $RDB_IDENTITY_DIR/fingerprint; delete the identity files there to make a new one" if [ -z "$RDB_PEER_FINGERPRINT" ]; then echo echo "This node's fingerprint: $mine" echo "Run this script on the other node too, then here again with" echo " RDB_PEER_FINGERPRINT=" exit 0 fi : "${RASTERIZEDDB_PG_PASSWORD:?set RASTERIZEDDB_PG_PASSWORD (the same on both nodes) for the role rasterized}" export RASTERIZEDDB_PG_PASSWORD mkdir -p "$RDB_DATA" RDB_DATA="$(cd "$RDB_DATA" && pwd)" args=(--location "$RDB_DATA" --protocol both --pg-addr "$RDB_PG_LISTEN" --replication --node-id "$RDB_NODE_ID" --replication-cluster "$RDB_CLUSTER" --replication-addr "$RDB_NODE_ADDR" --replication-port "$RDB_REPL_PORT" --replication-auth pinned --replication-cert "$cert" --replication-key "$key" --replication-peer-fingerprint "$RDB_PEER_FINGERPRINT" --peer "$RDB_PEER_NAME@$RDB_PEER_ADDR:$RDB_PEER_PORT" --replication-mode "$RDB_REPL_MODE" --replication-conflict "$RDB_REPL_CONFLICT") if [ -n "$RDB_TLS_CERT" ]; then args+=(--pg-cert "$RDB_TLS_CERT" --pg-key "$RDB_TLS_KEY" --rdb-cert "$RDB_TLS_CERT" --rdb-key "$RDB_TLS_KEY") fi echo "Node $RDB_NODE_ID: replication on $RDB_NODE_ADDR:$RDB_REPL_PORT, peer $RDB_PEER_NAME at $RDB_PEER_ADDR:$RDB_PEER_PORT." echo "Open port $RDB_REPL_PORT to the other node only." echo "PostgreSQL: postgresql://rasterized@$RDB_NODE_ADDR:5432/postgres?sslmode=require" echo "Check: psql … -c 'SHOW REPLICATION'" echo exec "$RDB_BIN" "${args[@]}"