#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.'