-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb-restore.ps1
More file actions
61 lines (48 loc) · 1.88 KB
/
Copy pathdb-restore.ps1
File metadata and controls
61 lines (48 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Chateando - PostgreSQL restore helper (Windows / PowerShell).
.DESCRIPTION
Restores a pg_dump produced by ops/db-backup.ps1 into the running `postgres`
service. The target database is dropped and recreated; existing data is lost.
.PARAMETER DumpFile
Path to the .dump produced by db-backup.ps1.
.PARAMETER Force
Skip the interactive confirmation prompt.
.EXAMPLE
./ops/db-restore.ps1 backups/chateando_20260528T120000Z.dump
./ops/db-restore.ps1 backups/last.dump -Force
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[string]$DumpFile,
[switch]$Force,
[string]$ComposeFile = 'docker-compose.yml',
[string]$DbService = 'postgres',
[string]$DbName = 'chateando',
[string]$DbUser = 'chateando'
)
$ErrorActionPreference = 'Stop'
if (-not (Test-Path -LiteralPath $DumpFile)) {
throw "Dump file '$DumpFile' does not exist."
}
if (-not $Force) {
Write-Host "[restore] About to DROP and RECREATE '$DbName' on service '$DbService'."
Write-Host "[restore] Source dump: $DumpFile"
$confirm = Read-Host "Type the database name to confirm"
if ($confirm -ne $DbName) {
throw 'Confirmation mismatch; aborting.'
}
}
Write-Host '[restore] dropping and recreating database...'
& docker compose -f $ComposeFile exec -T $DbService `
psql -v ON_ERROR_STOP=1 --username=$DbUser --dbname=postgres `
-c "DROP DATABASE IF EXISTS $DbName;"
& docker compose -f $ComposeFile exec -T $DbService `
psql -v ON_ERROR_STOP=1 --username=$DbUser --dbname=postgres `
-c "CREATE DATABASE $DbName OWNER $DbUser;"
Write-Host "[restore] restoring from $DumpFile..."
Get-Content -LiteralPath $DumpFile -AsByteStream -ReadCount 0 | & docker compose -f $ComposeFile exec -T $DbService `
pg_restore --no-owner --no-privileges --username=$DbUser --dbname=$DbName
Write-Host '[restore] done.'