#!/usr/bin/env bash # RasterizedDB: restore a database from its S3 backups into a directory, as of # the newest archived moment, a point in time, a commit sequence number, or # one snapshot. Then exits; start a server on the directory afterwards. # # Every setting can be overridden from the environment, for example: # RDB_S3_BUCKET=my-backups RDB_S3_PREFIX=prod/main RDB_RESTORE_DIR=/tmp/verify ./setup.sh # RDB_S3_BUCKET=my-backups RDB_S3_PREFIX=prod/main RDB_RESTORE_DIR=/var/lib/rasterizeddb \ # RDB_RESTORE_REPLACE=1 RDB_RESTORE_AT_TIME=2026-11-27T09:55:00Z ./setup.sh set -euo pipefail RDB_BIN="${RDB_BIN:-rasterizeddb}" RDB_RESTORE_DIR="${RDB_RESTORE_DIR:-./restored}" # must not exist or be empty, unless RDB_RESTORE_REPLACE=1 RDB_RESTORE_REPLACE="${RDB_RESTORE_REPLACE:-0}" # 1 renames a non-empty RDB_RESTORE_DIR aside first RDB_RESTORE_AT_TIME="${RDB_RESTORE_AT_TIME:-}" # e.g. 2026-11-27T09:55:00Z RDB_RESTORE_AT_LSN="${RDB_RESTORE_AT_LSN:-}" # an exact commit sequence number RDB_RESTORE_SNAPSHOT="${RDB_RESTORE_SNAPSHOT:-}" # a snapshot id from SHOW BACKUPS RDB_S3_BUCKET="${RDB_S3_BUCKET:-}" # required: where the backups are 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_S3_BUCKET" ] || die "set RDB_S3_BUCKET (and RDB_S3_PREFIX) to where the backups are" : "${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 targets=0 [ -z "$RDB_RESTORE_AT_TIME" ] || targets=$((targets + 1)) [ -z "$RDB_RESTORE_AT_LSN" ] || targets=$((targets + 1)) [ -z "$RDB_RESTORE_SNAPSHOT" ] || targets=$((targets + 1)) [ "$targets" -le 1 ] || die "give at most one of RDB_RESTORE_AT_TIME, RDB_RESTORE_AT_LSN and RDB_RESTORE_SNAPSHOT" # The directory: absent or empty, or moved aside (never deleted). parent="$(dirname "$RDB_RESTORE_DIR")" mkdir -p "$parent" RDB_RESTORE_DIR="$(cd "$parent" && pwd)/$(basename "$RDB_RESTORE_DIR")" if [ -d "$RDB_RESTORE_DIR" ] && [ -n "$(ls -A "$RDB_RESTORE_DIR")" ]; then [ "$RDB_RESTORE_REPLACE" = 1 ] || die "$RDB_RESTORE_DIR is not empty; choose another RDB_RESTORE_DIR, or set RDB_RESTORE_REPLACE=1 to move it aside" # A running server holds an exclusive lock on rasterizeddb.lock in its directory. if [ -f "$RDB_RESTORE_DIR/rasterizeddb.lock" ] && command -v flock >/dev/null 2>&1 \ && ! flock -n "$RDB_RESTORE_DIR/rasterizeddb.lock" true; then die "a server is running on $RDB_RESTORE_DIR; stop it first" fi aside="$RDB_RESTORE_DIR.pre-restore-$(date -u +%Y%m%dT%H%M%SZ)" mv "$RDB_RESTORE_DIR" "$aside" echo "Moved the existing directory to $aside (delete it once the restore is verified)" fi args=(--restore-to "$RDB_RESTORE_DIR" --s3-bucket "$RDB_S3_BUCKET" --s3-prefix "$RDB_S3_PREFIX" --s3-region "$RDB_S3_REGION") [ -z "$RDB_S3_ENDPOINT" ] || args+=(--s3-endpoint "$RDB_S3_ENDPOINT") [ "$RDB_S3_PATH_STYLE" != 1 ] || args+=(--s3-path-style) [ -z "$RDB_RESTORE_AT_TIME" ] || args+=(--restore-at-time "$RDB_RESTORE_AT_TIME") [ -z "$RDB_RESTORE_AT_LSN" ] || args+=(--restore-at-lsn "$RDB_RESTORE_AT_LSN") [ -z "$RDB_RESTORE_SNAPSHOT" ] || args+=(--restore-snapshot "$RDB_RESTORE_SNAPSHOT") "$RDB_BIN" "${args[@]}" echo echo "Restored into $RDB_RESTORE_DIR. Open it, for example on spare ports:" echo " $RDB_BIN --location $RDB_RESTORE_DIR --protocol both --pg-port 55432 --rdb-port 61190" echo " psql \"postgresql://rasterized@127.0.0.1:55432/postgres?sslmode=require\"" echo "The roles and passwords are the backed-up database's own." echo "To back the restored database up, give it a new --s3-prefix: it is a new database to the backups," echo "and the prefix it came from refuses it."