#!/usr/bin/env bash # RasterizedDB: turn a running single server into a replicated pair by adding # a new node, seeded from the existing server's S3 backups. # # Run this on the NEW node; run prepare-existing-node.sh on the existing one. # The order is in the README. The first run restores the copy, removes what # belongs to the existing node (its replication files, its key/value store # definitions), makes this node's replication identity and prints its # fingerprint. Run it again with RDB_PEER_FINGERPRINT to start (foreground). set -euo pipefail RDB_BIN="${RDB_BIN:-rasterizeddb}" RDB_DATA="${RDB_DATA:-./data}" # the new node's database directory RDB_IDENTITY_DIR="${RDB_IDENTITY_DIR:-./replication-identity}" RDB_NODE_ID="${RDB_NODE_ID:-2}" # must differ from the existing node's (1 by default) RDB_NODE_ADDR="${RDB_NODE_ADDR:-}" # required: this node's address RDB_PEER_ADDR="${RDB_PEER_ADDR:-}" # required: the existing node's address RDB_PEER_NAME="${RDB_PEER_NAME:-one}" RDB_PEER_FINGERPRINT="${RDB_PEER_FINGERPRINT:-}" # the existing node's fingerprint, to start RDB_REPL_PORT="${RDB_REPL_PORT:-61171}" RDB_PEER_PORT="${RDB_PEER_PORT:-61171}" RDB_REPL_MODE="${RDB_REPL_MODE:-async}" RDB_CLUSTER="${RDB_CLUSTER:-rasterizeddb}" RDB_PG_LISTEN="${RDB_PG_LISTEN:-0.0.0.0}" # Where the existing node's backups are (the s3-backup-pitr recipe): RDB_S3_BUCKET="${RDB_S3_BUCKET:-}" RDB_S3_PREFIX="${RDB_S3_PREFIX:-rasterizeddb/main}" RDB_S3_REGION="${RDB_S3_REGION:-${AWS_REGION:-us-east-1}}" RDB_S3_ENDPOINT="${RDB_S3_ENDPOINT:-}" RDB_S3_PATH_STYLE="${RDB_S3_PATH_STYLE:-0}" 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_ADDR" ] || die "set RDB_NODE_ADDR to this node's address" [ -n "$RDB_PEER_ADDR" ] || die "set RDB_PEER_ADDR to the existing node's address" [ -n "$RDB_NODE_ID" ] && [ "$RDB_NODE_ID" != 0 ] || die "RDB_NODE_ID must be set, differ from the existing node's, and not be 0" parent="$(dirname "$RDB_DATA")" mkdir -p "$parent" RDB_DATA="$(cd "$parent" && pwd)/$(basename "$RDB_DATA")" seeded="$RDB_DATA.seeded" # 1. The copy: the existing node's latest backup, restored here once. if [ ! -f "$seeded" ]; then if [ -d "$RDB_DATA" ] && [ -n "$(ls -A "$RDB_DATA")" ]; then die "$RDB_DATA already holds files but was not seeded by this script; give an empty RDB_DATA" fi [ -n "$RDB_S3_BUCKET" ] || die "set RDB_S3_BUCKET and RDB_S3_PREFIX to the existing node's backups" : "${AWS_ACCESS_KEY_ID:?set AWS_ACCESS_KEY_ID for the bucket}" : "${AWS_SECRET_ACCESS_KEY:?set AWS_SECRET_ACCESS_KEY for the bucket}" export AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY restore=(--restore-to "$RDB_DATA" --s3-bucket "$RDB_S3_BUCKET" --s3-prefix "$RDB_S3_PREFIX" --s3-region "$RDB_S3_REGION") [ -z "$RDB_S3_ENDPOINT" ] || restore+=(--s3-endpoint "$RDB_S3_ENDPOINT") [ "$RDB_S3_PATH_STYLE" != 1 ] || restore+=(--s3-path-style) "$RDB_BIN" "${restore[@]}" # What describes the existing node rather than this one: its position in its # own replication stream (left behind, this node would silently skip its # first writes), and its key/value store definitions and their credentials. find "$RDB_DATA" -type f \( -name 'db_replication.*' -o -name 'db_kv.cfg' -o -name 'db_kv_secrets.cfg' \ -o -name 'backup_instance.id' \) -print -delete | sed 's/^/removed from the copy: /' date -u +%Y-%m-%dT%H:%M:%SZ > "$seeded" echo "Seeded $RDB_DATA" fi # 2. This node's identity. 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 "Give it to the existing node (prepare-existing-node.sh prints the flags), then run this" echo "again with RDB_PEER_FINGERPRINT= to start." exit 0 fi # 3. Start, replicating with the existing node. The roles came with the copy. 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") 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 "PostgreSQL: postgresql://rasterized@$RDB_NODE_ADDR:5432/postgres?sslmode=require (the existing node's roles)" echo exec "$RDB_BIN" "${args[@]}"