#!/usr/bin/env bash # RasterizedDB: copy a live PostgreSQL database (schema, rows, then keys and # constraints) into a new RasterizedDB database, then exit. Run it while no # RasterizedDB server is using the data directory. # # The source is read with the usual PostgreSQL variables (PGHOST, PGPORT, # PGUSER, PGDATABASE, PGPASSWORD), for example: # PGHOST=pg.internal PGUSER=app PGDATABASE=shop PGPASSWORD='…' RDB_IMPORT_INTO=shop ./setup.sh set -euo pipefail RDB_BIN="${RDB_BIN:-rasterizeddb}" RDB_DATA="${RDB_DATA:-./data}" # the RasterizedDB data directory RDB_IMPORT_INTO="${RDB_IMPORT_INTO:-}" # required: name of the new database RDB_IMPORT_SSLMODE="${RDB_IMPORT_SSLMODE:-prefer}" # disable, prefer, require or verify-full RDB_IMPORT_SCHEMA="${RDB_IMPORT_SCHEMA:-public}" RDB_IMPORT_ONLY="${RDB_IMPORT_ONLY:-}" # tables to import, comma-separated (default: all) RDB_IMPORT_EXCLUDE="${RDB_IMPORT_EXCLUDE:-}" # tables to leave out, comma-separated RDB_IMPORT_JSON="${RDB_IMPORT_JSON:-auto}" # auto, text or native RDB_IMPORT_WIDE_NUMERIC="${RDB_IMPORT_WIDE_NUMERIC:-fail}" # fail or text RDB_IMPORT_BATCH_ROWS="${RDB_IMPORT_BATCH_ROWS:-5000}" RDB_IMPORT_WITHOUT_CONSTRAINTS="${RDB_IMPORT_WITHOUT_CONSTRAINTS:-0}" # 1 skips keys and foreign keys RDB_IMPORT_DRY_RUN="${RDB_IMPORT_DRY_RUN:-0}" # 1 reports what would come across, creates nothing 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_IMPORT_INTO" ] || die "set RDB_IMPORT_INTO to the name of the new database" [ -n "${PGDATABASE:-}" ] || die "set PGDATABASE (and PGHOST, PGPORT, PGUSER, PGPASSWORD) to the source database" [ -n "${PGPASSWORD:-}" ] || echo "warning: PGPASSWORD is not set; only a source that needs no password will work" >&2 # The new database starts with the roles of the directory's default database, # so the directory must have been started once (with its first role) already. [ -d "$RDB_DATA" ] && [ -n "$(ls -A "$RDB_DATA")" ] \ || die "$RDB_DATA is empty: start a server on it once first, so the role you will log in with exists" RDB_DATA="$(cd "$RDB_DATA" && pwd)" if [ -f "$RDB_DATA/rasterizeddb.lock" ] && command -v flock >/dev/null 2>&1 \ && ! flock -n "$RDB_DATA/rasterizeddb.lock" true; then die "a server is running on $RDB_DATA; stop it first (sudo systemctl stop rasterizeddb)" fi args=(--location "$RDB_DATA" --pg-import-into "$RDB_IMPORT_INTO" --pg-import-host "${PGHOST:-localhost}" --pg-import-port "${PGPORT:-5432}" --pg-import-user "${PGUSER:-postgres}" --pg-import-database "$PGDATABASE" --pg-import-sslmode "$RDB_IMPORT_SSLMODE" --pg-import-schema "$RDB_IMPORT_SCHEMA" --pg-import-json "$RDB_IMPORT_JSON" --pg-import-wide-numeric "$RDB_IMPORT_WIDE_NUMERIC" --pg-import-batch-rows "$RDB_IMPORT_BATCH_ROWS") [ -z "$RDB_IMPORT_ONLY" ] || args+=(--pg-import-only-tables "$RDB_IMPORT_ONLY") [ -z "$RDB_IMPORT_EXCLUDE" ] || args+=(--pg-import-exclude-tables "$RDB_IMPORT_EXCLUDE") [ "$RDB_IMPORT_WITHOUT_CONSTRAINTS" != 1 ] || args+=(--pg-import-without-constraints) [ "$RDB_IMPORT_DRY_RUN" != 1 ] || args+=(--pg-import-dry-run) echo "Importing $PGDATABASE from ${PGHOST:-localhost}:${PGPORT:-5432} (schema $RDB_IMPORT_SCHEMA) into $RDB_IMPORT_INTO at $RDB_DATA" "$RDB_BIN" "${args[@]}" if [ "$RDB_IMPORT_DRY_RUN" = 1 ]; then echo "Dry run: nothing was created." else echo echo "Done. Start the server on $RDB_DATA as usual, then connect to the new database:" echo " psql \"postgresql://rasterized@127.0.0.1:5432/$RDB_IMPORT_INTO?sslmode=require\"" fi