#!/usr/bin/env bash # RasterizedDB across several disks: new tables are spread over extra primary # directories by a placement policy, and an optional backup directory gets a # mirrored copy of every write. Runs in the foreground. # # Every setting can be overridden from the environment, for example: # RDB_PRIMARIES="/mnt/nvme1/rdb /mnt/nvme2/rdb" RDB_MIRROR=/mnt/backup/rdb ./setup.sh set -euo pipefail RDB_BIN="${RDB_BIN:-rasterizeddb}" RDB_DATA="${RDB_DATA:-./data}" # the database's own directory: catalogue, logs, the pool file RDB_PRIMARIES="${RDB_PRIMARIES:-}" # extra directories that hold tables, space-separated; DIR:WEIGHT for a weight RDB_MIRROR="${RDB_MIRROR:-}" # a directory that receives a copy of every write RDB_POLICY="${RDB_POLICY:-free-space}" # primary, round-robin or free-space RDB_MIN_FREE="${RDB_MIN_FREE:-2GiB}" # a directory with less free space takes no new tables RDB_LISTEN="${RDB_LISTEN:-127.0.0.1}" 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" : "${RASTERIZEDDB_PG_PASSWORD:?set RASTERIZEDDB_PG_PASSWORD to the password for the role rasterized}" export RASTERIZEDDB_PG_PASSWORD [ -n "$RDB_PRIMARIES$RDB_MIRROR" ] || die "set RDB_PRIMARIES and/or RDB_MIRROR to the extra directories" mkdir -p "$RDB_DATA" RDB_DATA="$(cd "$RDB_DATA" && pwd)" # Locations must be absolute paths, outside the data directory and each other. abs() { mkdir -p "$1" && (cd "$1" && pwd); } args=(--location "$RDB_DATA" --protocol both --pg-addr "$RDB_LISTEN" --rdb-addr "$RDB_LISTEN" --storage-policy "$RDB_POLICY" --storage-min-free "$RDB_MIN_FREE") for p in $RDB_PRIMARIES; do dir="$p" weight="" if [[ "$p" =~ ^(.+):([0-9]+)$ ]]; then dir="${BASH_REMATCH[1]}" weight="${BASH_REMATCH[2]}" fi dir="$(abs "$dir")" args+=(--storage-location "$dir:primary${weight:+:$weight}") echo "primary: $dir${weight:+ (weight $weight)}" done if [ -n "$RDB_MIRROR" ]; then dir="$(abs "$RDB_MIRROR")" args+=(--storage-location "$dir:backup") echo "backup: $dir" fi echo "Policy $RDB_POLICY, minimum free $RDB_MIN_FREE. Locations are added to the pool once and remembered." echo "PostgreSQL: postgresql://rasterized@$RDB_LISTEN:5432/postgres?sslmode=require" echo "Check: psql … -c 'SHOW STORAGE'" echo exec "$RDB_BIN" "${args[@]}"