#!/usr/bin/env bash # RasterizedDB: keep chosen tables in an S3 bucket instead of local files. # Defines a key/value store on a running server, then places new tables on it # or moves existing ones there. The store is defined with IF NOT EXISTS, so # running this again with the same settings does not change it. # # Every setting can be overridden from the environment, for example: # RDB_STORE_BUCKET=my-tables RDB_PLACE_TABLES="events audit" ./setup.sh # RDB_STORE_ENDPOINT=http://minio.internal:9000 RDB_STORE_ADDRESSING=path RDB_MOVE_TABLES=logs ./setup.sh set -euo pipefail RDB_PG_URL="${RDB_PG_URL:-postgresql://rasterized@127.0.0.1:5432/postgres?sslmode=require}" RDB_STORE_NAME="${RDB_STORE_NAME:-archive}" RDB_STORE_ENDPOINT="${RDB_STORE_ENDPOINT:-https://s3.us-east-1.amazonaws.com}" RDB_STORE_REGION="${RDB_STORE_REGION:-us-east-1}" RDB_STORE_BUCKET="${RDB_STORE_BUCKET:-}" # required RDB_STORE_PREFIX="${RDB_STORE_PREFIX:-rdb/archive}" # one server per prefix RDB_STORE_ADDRESSING="${RDB_STORE_ADDRESSING:-virtual}" # path for MinIO and non-DNS-safe bucket names RDB_STORE_CACHE="${RDB_STORE_CACHE:-256MiB}" # page cache for this store's tables RDB_STORE_OWN_KEYS="${RDB_STORE_OWN_KEYS:-0}" # 1: the store keeps AWS_ACCESS_KEY_ID/SECRET from this shell RDB_PLACE_TABLES="${RDB_PLACE_TABLES:-}" # tables not created yet, space-separated RDB_MOVE_TABLES="${RDB_MOVE_TABLES:-}" # existing tables to move onto the store die() { echo "error: $*" >&2; exit 1; } command -v psql >/dev/null 2>&1 || die "psql is needed" [ -n "$RDB_STORE_BUCKET" ] || die "set RDB_STORE_BUCKET to the bucket that will hold the tables" [ -n "${PGPASSWORD:-}" ] || die "set PGPASSWORD to the password of the role in RDB_PG_URL (a superuser)" for v in "$RDB_STORE_NAME" "$RDB_STORE_ENDPOINT" "$RDB_STORE_REGION" "$RDB_STORE_BUCKET" "$RDB_STORE_PREFIX" "$RDB_STORE_ADDRESSING" "$RDB_STORE_CACHE"; do case "$v" in *"'"*) die "settings may not contain a single quote: $v" ;; esac done credentials="" if [ "$RDB_STORE_OWN_KEYS" = 1 ]; then : "${AWS_ACCESS_KEY_ID:?set AWS_ACCESS_KEY_ID for the store}" : "${AWS_SECRET_ACCESS_KEY:?set AWS_SECRET_ACCESS_KEY for the store}" case "$AWS_ACCESS_KEY_ID$AWS_SECRET_ACCESS_KEY" in *"'"*) die "the keys may not contain a single quote" ;; esac credentials=", ACCESS_KEY_ID = '$AWS_ACCESS_KEY_ID', SECRET_ACCESS_KEY = '$AWS_SECRET_ACCESS_KEY'" fi # The statements go to psql on standard input, so no key appears in the process list. sql() { psql "$RDB_PG_URL" -v ON_ERROR_STOP=1 -X -q "$@"; } sql <&2 fi done for t in $RDB_MOVE_TABLES; do if sql -c "STORAGE MOVE TABLE $t TO 'kv:$RDB_STORE_NAME'"; then echo "moved: $t" else echo "warning: $t was not moved (the error is above)" >&2 fi done sql -c "SHOW STORAGE STORES" echo "Tables on the store show backend s3 in SHOW TABLES."