NEW RasterizedDB 1.1.0 is out: sharding and the gateway, the message broker, roles and row-level security, compression, .NET on NuGet, and the enterprise edition. What's new ›
DOCS / SETUP SCRIPTS / RESTORE FROM S3

Restore from S3

Rebuilds a database from the backups the s3-backup-pitr recipe makes: the newest snapshot plus the archived log, replayed up to the newest archived moment, a point in time, an exact commit sequence number, or one snapshot exactly. The restore goes into a directory and the program exits; you then start a server on that directory. Use it to check that your backups restore, to clone a database, to rebuild a lost machine, or to rewind past a mistake.

Before you start

  • The server binary (see the install recipe). It does not have to be the machine that made the backups.
  • The bucket and prefix the backups are in, and credentials that may read them in AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY.
  • To restore over a database that is in use, stop its server first. The script refuses a directory a running server holds.
  • Free disk space for the whole database. The restore is built in a directory next to the target and moved into place when it is complete, so a failed restore leaves nothing behind and can be run again.

Settings

bash variablePowerShell parameterDefaultWhat it does
RDB_BIN-RdbBinrasterizeddb / rasterizeddb.exeThe server binary.
RDB_RESTORE_DIR-RestoreDir./restoredWhere the database is restored. It must not exist, or be empty.
RDB_RESTORE_REPLACE=1-ReplaceoffRename a non-empty directory to <dir>.pre-restore-<time> first. Nothing is deleted.
RDB_RESTORE_AT_TIME-AtTimenewestRestore as of this moment, for example 2026-11-27T09:55:00Z.
RDB_RESTORE_AT_LSN-AtLsnnewestRestore exactly up to this commit sequence number.
RDB_RESTORE_SNAPSHOT-SnapshotnewestRestore this snapshot (an id from SHOW BACKUPS), with only enough log to make it consistent.
RDB_S3_BUCKET-BucketrequiredThe bucket.
RDB_S3_PREFIX-Prefixrasterizeddb/mainThe prefix the backups were written under.
RDB_S3_REGION-Region$AWS_REGION, else us-east-1Signing region.
RDB_S3_ENDPOINT-EndpointAWSThe store's URL for MinIO, R2, B2, Wasabi.
RDB_S3_PATH_STYLE=1-PathStyleoffPath-style addressing (MinIO).

Give at most one of the time, sequence number and snapshot. A restore to a time stops at the last log upload at or before it, so it can be up to --s3-wal-flush-secs earlier than the time you name. A restore to a sequence number is exact: each transaction is either wholly in or wholly out.

Run it

Check that the backups restore, into a scratch directory:

sh
export AWS_ACCESS_KEY_ID='…' AWS_SECRET_ACCESS_KEY='…'
RDB_S3_BUCKET=my-backups RDB_S3_PREFIX=prod/main RDB_RESTORE_DIR=/tmp/verify ./setup.sh

Rewind the live database to just before a bad migration (stop the server first; the current directory is kept aside):

sh
sudo systemctl stop rasterizeddb
sudo --preserve-env=AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY \
  RDB_BIN=/usr/local/bin/rasterizeddb 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
sudo chown -R rasterizeddb:rasterizeddb /var/lib/rasterizeddb
powershell
$env:AWS_ACCESS_KEY_ID = '…'; $env:AWS_SECRET_ACCESS_KEY = '…'
.\setup.ps1 -Bucket my-backups -Prefix prod/main -RestoreDir C:\rdb-verify
.\setup.ps1 -Bucket my-backups -Prefix prod/main -RestoreDir C:\ProgramData\RasterizedDB\data -Replace -AtTime 2026-11-27T09:55:00Z

The restore prints the snapshot it used, how many log bundles it replayed and the commit sequence it recovered to.

Check that it works

Open the restored directory on spare ports and look at the data. The roles and passwords are those of the database that was backed up:

sh
rasterizeddb --location /tmp/verify --protocol both --pg-port 55432 --rdb-port 61190 &
PGPASSWORD='…' psql "postgresql://rasterized@127.0.0.1:55432/postgres?sslmode=require" -c "SELECT count(*) FROM orders"

Start serving it, and back it up again

Start the server on the restored directory as you normally do. A restored database is a new database as far as backups go: the prefix it was restored from belongs to the original and refuses it at start (already holds backups for instance …). Give it a new --s3-prefix, for example prod/main-2. A new prefix gets its first snapshot at once.

Stop or undo

The restore itself changes nothing in the bucket. With RDB_RESTORE_REPLACE=1 the previous directory is still there as <dir>.pre-restore-<time>. To go back to it, stop the server, move the restored directory away and rename the old one back. Delete it once you are satisfied.

Limits

  • Tables kept on a key/value store come back as ordinary tables in the restored directory, as of the snapshot that copied them, since they have no log to replay.
  • Before using a restored copy as a replica of the original, remove its replication files; see the add-replica-node recipe.
  • A long restore from a remote store can outlive temporary credentials. Use credentials that stay valid for the whole restore.

The files

setup.sh · Linux and macOS (Bash)DOWNLOAD ↓
#!/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."
setup.ps1 · Windows (PowerShell)DOWNLOAD ↓
#Requires -Version 5.1
<#
.SYNOPSIS
  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.
.EXAMPLE
  $env:AWS_ACCESS_KEY_ID = '…'; $env:AWS_SECRET_ACCESS_KEY = '…'
  .\setup.ps1 -Bucket my-backups -Prefix prod/main -RestoreDir C:\rdb-verify
.EXAMPLE
  .\setup.ps1 -Bucket my-backups -Prefix prod/main -RestoreDir C:\ProgramData\RasterizedDB\data -Replace -AtTime 2026-11-27T09:55:00Z
#>
[CmdletBinding()]
param(
    [string]$RdbBin = $(if ($env:RDB_BIN) { $env:RDB_BIN } else { 'rasterizeddb.exe' }),
    # Must not exist or be empty, unless -Replace.
    [string]$RestoreDir = $(if ($env:RDB_RESTORE_DIR) { $env:RDB_RESTORE_DIR } else { '.\restored' }),
    # Rename a non-empty RestoreDir aside first (it is never deleted).
    [switch]$Replace = ($env:RDB_RESTORE_REPLACE -eq '1'),
    # For example 2026-11-27T09:55:00Z.
    [string]$AtTime = $env:RDB_RESTORE_AT_TIME,
    # An exact commit sequence number.
    [string]$AtLsn = $env:RDB_RESTORE_AT_LSN,
    # A snapshot id from SHOW BACKUPS.
    [string]$Snapshot = $env:RDB_RESTORE_SNAPSHOT,
    [string]$Bucket = $env:RDB_S3_BUCKET,
    [string]$Prefix = $(if ($env:RDB_S3_PREFIX) { $env:RDB_S3_PREFIX } else { 'rasterizeddb/main' }),
    [string]$Region = $(if ($env:RDB_S3_REGION) { $env:RDB_S3_REGION } elseif ($env:AWS_REGION) { $env:AWS_REGION } else { 'us-east-1' }),
    [string]$Endpoint = $env:RDB_S3_ENDPOINT,
    [switch]$PathStyle = ($env:RDB_S3_PATH_STYLE -eq '1')
)
$ErrorActionPreference = 'Stop'

$cmd = Get-Command $RdbBin -ErrorAction SilentlyContinue
if (-not $cmd) { throw "'$RdbBin' not found; pass -RdbBin with the path of rasterizeddb.exe." }
if (-not $Bucket) { throw 'Pass -Bucket (and -Prefix) for where the backups are.' }
if (-not $env:AWS_ACCESS_KEY_ID -or -not $env:AWS_SECRET_ACCESS_KEY) { throw 'Set $env:AWS_ACCESS_KEY_ID and $env:AWS_SECRET_ACCESS_KEY for the bucket.' }
if (@($AtTime, $AtLsn, $Snapshot | Where-Object { $_ }).Count -gt 1) { throw 'Give at most one of -AtTime, -AtLsn and -Snapshot.' }

# The directory: absent or empty, or moved aside (never deleted).
$parent = Split-Path -Parent $RestoreDir
if (-not $parent) { $parent = '.' }
New-Item -ItemType Directory -Force -Path $parent | Out-Null
$RestoreDir = Join-Path (Resolve-Path -LiteralPath $parent).Path (Split-Path -Leaf $RestoreDir)
if ((Test-Path -LiteralPath $RestoreDir) -and (Get-ChildItem -LiteralPath $RestoreDir -Force | Select-Object -First 1)) {
    if (-not $Replace) { throw "$RestoreDir is not empty; choose another -RestoreDir, or pass -Replace to move it aside." }
    # A running server holds a lock on rasterizeddb.lock in its directory.
    $lock = Join-Path $RestoreDir 'rasterizeddb.lock'
    if (Test-Path -LiteralPath $lock) {
        try {
            $fs = [System.IO.File]::Open($lock, 'Open', 'ReadWrite', 'ReadWrite')
            try { $fs.Lock(0, 1); $fs.Unlock(0, 1) } finally { $fs.Dispose() }
        }
        catch { throw "A server is running on $RestoreDir; stop it first." }
    }
    $aside = "$RestoreDir.pre-restore-$((Get-Date).ToUniversalTime().ToString('yyyyMMddTHHmmssZ'))"
    Rename-Item -LiteralPath $RestoreDir -NewName (Split-Path -Leaf $aside)
    Write-Host "Moved the existing directory to $aside (delete it once the restore is verified)"
}

$serverArgs = @('--restore-to', $RestoreDir, '--s3-bucket', $Bucket, '--s3-prefix', $Prefix, '--s3-region', $Region)
if ($Endpoint) { $serverArgs += @('--s3-endpoint', $Endpoint) }
if ($PathStyle) { $serverArgs += '--s3-path-style' }
if ($AtTime) { $serverArgs += @('--restore-at-time', $AtTime) }
if ($AtLsn) { $serverArgs += @('--restore-at-lsn', $AtLsn) }
if ($Snapshot) { $serverArgs += @('--restore-snapshot', $Snapshot) }

& $cmd.Source @serverArgs
if ($LASTEXITCODE -ne 0) { throw "The restore failed (exit $LASTEXITCODE); the message is above." }

Write-Host ''
Write-Host "Restored into $RestoreDir. Open it, for example on spare ports:"
Write-Host "  & '$($cmd.Source)' --location '$RestoreDir' --protocol both --pg-port 55432 --rdb-port 61190"
Write-Host '  psql "postgresql://rasterized@127.0.0.1:55432/postgres?sslmode=require"'
Write-Host "The roles and passwords are the backed-up database's own."
Write-Host 'To back the restored database up, give it a new --s3-prefix: it is a new database to the backups,'
Write-Host 'and the prefix it came from refuses it.'
✓ page rendered in 213 µs axum + tera + htmx · no other javascript