feat:backup und restore der contao Datenbank über sql file

This commit is contained in:
Robert Wartenberg committed 2026-08-18 17:24:12 +02:00
1 parent 1c1bf84b7a
commit e44b16e523
6 files changed
+237 -1

No files matched your search

@@ -0,0 +1,47 @@
[CmdletBinding()]
param(
[string]$ComposeFile = (Join-Path $PSScriptRoot '..\docker-compose.yml'),
[string]$OutputPath = (Join-Path $PSScriptRoot '..\database\contao-baseline.sql'),
[switch]$Force
)
$ErrorActionPreference = 'Stop'
$tables = @('tl_theme', 'tl_layout', 'tl_module', 'tl_page', 'tl_article', 'tl_content', 'tl_form', 'tl_form_field')
if ((Test-Path -LiteralPath $OutputPath) -and -not $Force) {
$confirmation = Read-Host "Die Datei '$OutputPath' existiert bereits. Überschreiben? [j/N]"
if ($confirmation.Trim().ToLowerInvariant() -notin @('j', 'ja', 'y', 'yes')) {
Write-Host 'Export nicht überschrieben.'
return
}
}
$composeFile = (Resolve-Path -LiteralPath $ComposeFile).Path
$outputDirectory = Split-Path -Parent $OutputPath
New-Item -ItemType Directory -Path $outputDirectory -Force | Out-Null
# Credentials bleiben im Compose-Service und werden nicht in den Export geschrieben.
$command = 'exec mariadb-dump -u"$MARIADB_USER" -p"$MARIADB_PASSWORD" --no-tablespaces --no-create-info --skip-comments --skip-triggers --skip-add-locks --skip-disable-keys --complete-insert --skip-extended-insert --order-by-primary --hex-blob "$MARIADB_DATABASE" tl_theme tl_layout tl_module tl_page tl_article tl_content tl_form tl_form_field'
$dump = & docker compose -f $composeFile exec -T db sh -lc $command
if ($LASTEXITCODE -ne 0) {
throw 'Der Contao-Baseline-Export ist fehlgeschlagen.'
}
$header = @(
'-- Contao website baseline for local development.',
'--',
'-- Contains only versionable website data. It deliberately excludes users,',
'-- credentials, sessions, logs, cache entries and file metadata.',
'-- Generated with scripts/Export-ContaoBaseline.ps1.',
''
)
$utf8WithoutBom = New-Object System.Text.UTF8Encoding($false)
[System.IO.File]::WriteAllText(
(Resolve-Path -LiteralPath $outputDirectory).Path + [System.IO.Path]::DirectorySeparatorChar + (Split-Path -Leaf $OutputPath),
(($header + $dump) -join [Environment]::NewLine) + [Environment]::NewLine,
$utf8WithoutBom
)
Write-Host "Contao-Baseline exportiert nach $OutputPath"
@@ -0,0 +1,55 @@
[CmdletBinding()]
param(
[string]$ComposeFile = (Join-Path $PSScriptRoot '..\docker-compose.yml'),
[string]$SeedPath = (Join-Path $PSScriptRoot '..\database\contao-baseline.sql'),
[switch]$ReplaceExisting
)
$ErrorActionPreference = 'Stop'
$tables = @('tl_theme', 'tl_layout', 'tl_module', 'tl_page', 'tl_article', 'tl_content', 'tl_form', 'tl_form_field')
if (-not (Test-Path -LiteralPath $SeedPath)) {
throw "Der Baseline-Seed '$SeedPath' wurde nicht gefunden."
}
$composeFile = (Resolve-Path -LiteralPath $ComposeFile).Path
$countQuery = ($tables | ForEach-Object { "SELECT COUNT(*) FROM $_" }) -join ' UNION ALL '
$checkCommand = "mariadb -N -u`"`$MARIADB_USER`" -p`"`$MARIADB_PASSWORD`" `"`$MARIADB_DATABASE`" -e `"$countQuery`""
$existingRows = (& docker compose -f $composeFile exec -T db sh -lc $checkCommand | ForEach-Object { [int]$_ } | Measure-Object -Sum).Sum
if ($existingRows -gt 0 -and -not $ReplaceExisting) {
throw 'Die versionierten Contao-Tabellen enthalten bereits Daten. Der Import wurde nicht ausgeführt. Nutze -ReplaceExisting nur, wenn du die aktuelle Seitenstruktur bewusst durch den Git-Stand ersetzen möchtest.'
}
$seed = Get-Content -LiteralPath $SeedPath -Raw
if ($ReplaceExisting) {
$deleteStatements = @(
'SET FOREIGN_KEY_CHECKS=0;',
'DELETE FROM tl_content;',
'DELETE FROM tl_article;',
'DELETE FROM tl_form_field;',
'DELETE FROM tl_form;',
'DELETE FROM tl_module;',
'DELETE FROM tl_page;',
'DELETE FROM tl_layout;',
'DELETE FROM tl_theme;',
'SET FOREIGN_KEY_CHECKS=1;',
''
) -join [Environment]::NewLine
$seed = $deleteStatements + $seed
}
$utf8WithoutBom = New-Object System.Text.UTF8Encoding($false)
[Console]::OutputEncoding = $utf8WithoutBom
$seed | & docker compose -f $composeFile exec -T db sh -lc 'exec mariadb -u"$MARIADB_USER" -p"$MARIADB_PASSWORD" "$MARIADB_DATABASE"'
if ($LASTEXITCODE -ne 0) {
throw 'Der Contao-Baseline-Import ist fehlgeschlagen.'
}
& docker compose -f $composeFile exec -T app sh -lc 'runuser -u www-data -- vendor/bin/contao-console cache:clear --env=dev'
if ($LASTEXITCODE -ne 0) {
throw 'Der Import war erfolgreich, aber der Contao-Cache konnte nicht geleert werden.'
}
Write-Host 'Contao-Baseline importiert und Cache geleert.'