feat:backup und restore der contao Datenbank über sql file
This commit is contained in:
1 parent
1c1bf84b7a
commit
e44b16e523
6 files changed
+237
-1
No files matched your search
@@ -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.'
|
||||
Reference in new issue
Block a user