#Requires -Version 5.1 <# .SYNOPSIS RasterizedDB: prepare the EXISTING server to pair with a new node seeded from its backup. Run it while that server is stopped. .DESCRIPTION Moves the server's replication files aside (never deletes them): they describe the stream it had before the copy was made, and a pair that starts from one copy must both start from nothing. Then makes the server's replication identity and prints the flags to start it with. .EXAMPLE .\prepare-existing-node.ps1 -NodeAddr 10.0.0.1 -PeerAddr 10.0.0.2 -PeerFingerprint 3b1c… #> [CmdletBinding()] param( [string]$RdbBin = $(if ($env:RDB_BIN) { $env:RDB_BIN } else { 'rasterizeddb.exe' }), # The existing server's --location. [string]$Data = $(if ($env:RDB_DATA) { $env:RDB_DATA } else { 'C:\ProgramData\RasterizedDB\data' }), [string]$IdentityDir = $(if ($env:RDB_IDENTITY_DIR) { $env:RDB_IDENTITY_DIR } else { 'C:\ProgramData\RasterizedDB\replication' }), [int]$NodeId = $(if ($env:RDB_NODE_ID) { [int]$env:RDB_NODE_ID } else { 1 }), [string]$NodeAddr = $env:RDB_NODE_ADDR, # The new node's address. [string]$PeerAddr = $env:RDB_PEER_ADDR, [string]$PeerName = $(if ($env:RDB_PEER_NAME) { $env:RDB_PEER_NAME } else { 'two' }), [string]$PeerFingerprint = $(if ($env:RDB_PEER_FINGERPRINT) { $env:RDB_PEER_FINGERPRINT } else { '' }), [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]$Cluster = $(if ($env:RDB_CLUSTER) { $env:RDB_CLUSTER } else { 'rasterizeddb' }) ) $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 (Test-Path -LiteralPath $Data -PathType Container)) { throw "$Data is not a directory; pass -Data with the existing server's --location." } if (-not $NodeAddr) { throw "Pass -NodeAddr with this server's address." } if (-not $PeerAddr) { throw "Pass -PeerAddr with the new node's address." } $Data = (Resolve-Path -LiteralPath $Data).Path.TrimEnd('\') # A running server holds a lock on rasterizeddb.lock. $lock = Join-Path $Data '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 "The server on $Data is running; stop it first (after BACKUP WAL, see the README)." } } # 1. The replication files, every database's, moved aside with their paths. $aside = "$Data.replication-aside-$((Get-Date).ToUniversalTime().ToString('yyyyMMddTHHmmssZ'))" $files = @(Get-ChildItem -LiteralPath $Data -Recurse -File -Force -Filter 'db_replication.*') foreach ($f in $files) { $rel = $f.FullName.Substring($Data.Length).TrimStart('\') $dest = Join-Path $aside $rel New-Item -ItemType Directory -Force -Path (Split-Path -Parent $dest) | Out-Null Move-Item -LiteralPath $f.FullName -Destination $dest Write-Host "moved aside: $rel" } if ($files.Count -gt 0) { Write-Host "Kept in $aside" } else { Write-Host "No replication files in $Data" } # 2. This server'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.' } $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 } Write-Host '' Write-Host "This server's fingerprint: $((Get-Content -LiteralPath $fpFile -Raw).Trim())" Write-Host '' Write-Host 'Start it with its usual flags plus (one array element per argument for -ExtraArgs):' Write-Host " --replication --node-id $NodeId --replication-cluster $Cluster" Write-Host " --replication-addr $NodeAddr --replication-port $ReplPort" Write-Host " --replication-auth pinned --replication-cert $cert --replication-key $key" Write-Host " --replication-peer-fingerprint $PeerFingerprint" Write-Host " --peer $PeerName@${PeerAddr}:$PeerPort"