#Requires -Version 5.1 <# .SYNOPSIS RasterizedDB: turn a running single server into a replicated pair by adding a new node, seeded from the existing server's S3 backups. .DESCRIPTION Run this on the NEW node; run prepare-existing-node.ps1 on the existing one. The order is in the README. The first run restores the copy, removes what belongs to the existing node (its replication files, its key/value store definitions), makes this node's replication identity and prints its fingerprint. Run it again with -PeerFingerprint to start (foreground). .EXAMPLE $env:AWS_ACCESS_KEY_ID = '…'; $env:AWS_SECRET_ACCESS_KEY = '…' .\setup.ps1 -NodeAddr 10.0.0.2 -PeerAddr 10.0.0.1 -Bucket my-backups -Prefix prod/main .EXAMPLE .\setup.ps1 -NodeAddr 10.0.0.2 -PeerAddr 10.0.0.1 -PeerFingerprint 9f86d081… #> [CmdletBinding()] param( [string]$RdbBin = $(if ($env:RDB_BIN) { $env:RDB_BIN } else { 'rasterizeddb.exe' }), # The new node's database directory. [string]$Data = $(if ($env:RDB_DATA) { $env:RDB_DATA } else { '.\data' }), [string]$IdentityDir = $(if ($env:RDB_IDENTITY_DIR) { $env:RDB_IDENTITY_DIR } else { '.\replication-identity' }), # Must differ from the existing node's (1 by default). [int]$NodeId = $(if ($env:RDB_NODE_ID) { [int]$env:RDB_NODE_ID } else { 2 }), [string]$NodeAddr = $env:RDB_NODE_ADDR, # The existing node's address. [string]$PeerAddr = $env:RDB_PEER_ADDR, [string]$PeerName = $(if ($env:RDB_PEER_NAME) { $env:RDB_PEER_NAME } else { 'one' }), [string]$PeerFingerprint = $env:RDB_PEER_FINGERPRINT, [int]$ReplPort = $(if ($env:RDB_REPL_PORT) { [int]$env:RDB_REPL_PORT } else { 61171 }), [int]$PeerPort = $(if ($env:RDB_PEER_PORT) { [int]$env:RDB_PEER_PORT } else { 61171 }), [string]$Mode = $(if ($env:RDB_REPL_MODE) { $env:RDB_REPL_MODE } else { 'async' }), [string]$Cluster = $(if ($env:RDB_CLUSTER) { $env:RDB_CLUSTER } else { 'rasterizeddb' }), [string]$PgListen = $(if ($env:RDB_PG_LISTEN) { $env:RDB_PG_LISTEN } else { '0.0.0.0' }), # Where the existing node's backups are (the s3-backup-pitr recipe). [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 $NodeAddr) { throw "Pass -NodeAddr with this node's address." } if (-not $PeerAddr) { throw "Pass -PeerAddr with the existing node's address." } if ($NodeId -le 0) { throw "-NodeId must differ from the existing node's and not be 0." } $parent = Split-Path -Parent $Data if (-not $parent) { $parent = '.' } New-Item -ItemType Directory -Force -Path $parent | Out-Null $Data = Join-Path (Resolve-Path -LiteralPath $parent).Path (Split-Path -Leaf $Data) $seeded = "$Data.seeded" # 1. The copy: the existing node's latest backup, restored here once. if (-not (Test-Path -LiteralPath $seeded)) { if ((Test-Path -LiteralPath $Data) -and (Get-ChildItem -LiteralPath $Data -Force | Select-Object -First 1)) { throw "$Data already holds files but was not seeded by this script; give an empty -Data." } if (-not $Bucket) { throw "Pass -Bucket and -Prefix for the existing node's backups." } 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.' } $restore = @('--restore-to', $Data, '--s3-bucket', $Bucket, '--s3-prefix', $Prefix, '--s3-region', $Region) if ($Endpoint) { $restore += @('--s3-endpoint', $Endpoint) } if ($PathStyle) { $restore += '--s3-path-style' } & $cmd.Source @restore if ($LASTEXITCODE -ne 0) { throw "The restore failed (exit $LASTEXITCODE); the message is above." } # What describes the existing node rather than this one: its position in # its own replication stream (left behind, this node would silently skip # its first writes), and its key/value store definitions and credentials. @(Get-ChildItem -LiteralPath $Data -Recurse -File -Force) | Where-Object { $_.Name -like 'db_replication.*' -or $_.Name -in @('db_kv.cfg', 'db_kv_secrets.cfg', 'backup_instance.id') } | ForEach-Object { Remove-Item -LiteralPath $_.FullName -Force; Write-Host "removed from the copy: $($_.FullName)" } Set-Content -LiteralPath $seeded -Value ((Get-Date).ToUniversalTime().ToString('o')) -Encoding ascii Write-Host "Seeded $Data" } # 2. This node's identity. New-Item -ItemType Directory -Force -Path $IdentityDir | Out-Null $IdentityDir = (Resolve-Path -LiteralPath $IdentityDir).Path $cert = Join-Path $IdentityDir 'replication-cert.pem' $key = Join-Path $IdentityDir 'replication-key.pem' $fpFile = Join-Path $IdentityDir 'fingerprint' if (-not (Test-Path -LiteralPath $cert) -or -not (Test-Path -LiteralPath $key)) { $out = & $cmd.Source --replication-generate-identity $IdentityDir --replication-addr $NodeAddr if ($LASTEXITCODE -ne 0) { throw 'Could not generate the replication identity; the message is above.' } $out | ForEach-Object { Write-Host $_ } $fp = $out | ForEach-Object { if ($_ -match '^fingerprint:\s*(\S+)') { $Matches[1] } } | Select-Object -First 1 Set-Content -LiteralPath $fpFile -Value $fp -Encoding ascii & icacls.exe $key /inheritance:r /grant:r "$($env:USERNAME):F" '*S-1-5-18:F' '*S-1-5-32-544:F' | Out-Null } $mine = if (Test-Path -LiteralPath $fpFile) { (Get-Content -LiteralPath $fpFile -Raw).Trim() } else { '' } if (-not $mine) { throw "No fingerprint saved in $fpFile; delete the identity files there to make a new one." } if (-not $PeerFingerprint) { Write-Host '' Write-Host "This node's fingerprint: $mine" Write-Host 'Give it to the existing node (prepare-existing-node.ps1 prints the flags), then run this' Write-Host "again with -PeerFingerprint to start." exit 0 } # 3. Start, replicating with the existing node. The roles came with the copy. $serverArgs = @('--location', $Data, '--protocol', 'both', '--pg-addr', $PgListen, '--replication', '--node-id', "$NodeId", '--replication-cluster', $Cluster, '--replication-addr', $NodeAddr, '--replication-port', "$ReplPort", '--replication-auth', 'pinned', '--replication-cert', $cert, '--replication-key', $key, '--replication-peer-fingerprint', $PeerFingerprint, '--peer', "$PeerName@${PeerAddr}:$PeerPort", '--replication-mode', $Mode) Write-Host "Node ${NodeId}: replication on ${NodeAddr}:$ReplPort, peer $PeerName at ${PeerAddr}:$PeerPort." Write-Host "PostgreSQL: postgresql://rasterized@${NodeAddr}:5432/postgres?sslmode=require (the existing node's roles)" Write-Host '' & $cmd.Source @serverArgs exit $LASTEXITCODE