Refactor backup scripts to include redundant backup directory creation, improved logging, and ensure successful backup copying; update run script to execute both database and volume backups sequentially.
This commit is contained in:
@@ -3,12 +3,22 @@ $timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
|
|||||||
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||||
$backupDir = Join-Path $scriptDir "backups"
|
$backupDir = Join-Path $scriptDir "backups"
|
||||||
$backupFile = Join-Path $backupDir "gitea-db-backup-$timestamp.sql"
|
$backupFile = Join-Path $backupDir "gitea-db-backup-$timestamp.sql"
|
||||||
|
$redundantBackupDir = "D:\Pr00jects\gitea-docker"
|
||||||
|
|
||||||
# Ensure backup directory exists
|
# Ensure backup directory exists
|
||||||
if (-not (Test-Path $backupDir)) {
|
if (-not (Test-Path $backupDir)) {
|
||||||
New-Item -ItemType Directory -Path $backupDir
|
New-Item -ItemType Directory -Path $backupDir
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Ensure redundant backup directory exists
|
||||||
|
if (-not (Test-Path $redundantBackupDir)) {
|
||||||
|
try {
|
||||||
|
New-Item -ItemType Directory -Path $redundantBackupDir -Force
|
||||||
|
} catch {
|
||||||
|
"[$timestamp] Warning: Could not create redundant backup directory at $redundantBackupDir. Error: $_" | Out-File -Append -FilePath $logFile
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# Log execution of the script
|
# Log execution of the script
|
||||||
$logFile = Join-Path $backupDir "backup-log.txt"
|
$logFile = Join-Path $backupDir "backup-log.txt"
|
||||||
"[$timestamp] Starting database backup..." | Out-File -Append -FilePath $logFile
|
"[$timestamp] Starting database backup..." | Out-File -Append -FilePath $logFile
|
||||||
@@ -43,9 +53,23 @@ if ($LASTEXITCODE -eq 0 -and (Test-Path $backupFile) -and (Get-Item $backupFile)
|
|||||||
"[$timestamp] Backup completed successfully!" | Out-File -Append -FilePath $logFile
|
"[$timestamp] Backup completed successfully!" | Out-File -Append -FilePath $logFile
|
||||||
|
|
||||||
# Optional: Compress the backup file
|
# Optional: Compress the backup file
|
||||||
Compress-Archive -Path $backupFile -DestinationPath "$backupFile.zip" -Force
|
$zipFileName = "$backupFile.zip"
|
||||||
|
Compress-Archive -Path $backupFile -DestinationPath $zipFileName -Force
|
||||||
Remove-Item $backupFile
|
Remove-Item $backupFile
|
||||||
"[$timestamp] Backup compressed to $backupFile.zip" | Out-File -Append -FilePath $logFile
|
"[$timestamp] Backup compressed to $zipFileName" | Out-File -Append -FilePath $logFile
|
||||||
|
|
||||||
|
# Copy backup to redundant location
|
||||||
|
if (Test-Path $redundantBackupDir) {
|
||||||
|
try {
|
||||||
|
$redundantBackupFile = Join-Path $redundantBackupDir (Split-Path -Leaf $zipFileName)
|
||||||
|
Copy-Item -Path $zipFileName -Destination $redundantBackupFile -Force
|
||||||
|
"[$timestamp] Backup copied to redundant location: $redundantBackupFile" | Out-File -Append -FilePath $logFile
|
||||||
|
} catch {
|
||||||
|
"[$timestamp] Error copying backup to redundant location: $_" | Out-File -Append -FilePath $logFile
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
"[$timestamp] Warning: Redundant backup directory not found: $redundantBackupDir" | Out-File -Append -FilePath $logFile
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
"[$timestamp] Backup failed!" | Out-File -Append -FilePath $logFile
|
"[$timestamp] Backup failed!" | Out-File -Append -FilePath $logFile
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,36 +1,82 @@
|
|||||||
# Script to backup the entire Postgres Docker volume
|
# Script to backup the entire Postgres Docker volume
|
||||||
$timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
|
$timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
|
||||||
$backupDir = ".\backups"
|
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||||
|
$backupDir = Join-Path $scriptDir "backups"
|
||||||
$volumeName = "gitea-docker_postgres-data"
|
$volumeName = "gitea-docker_postgres-data"
|
||||||
$backupFile = "$backupDir\postgres-volume-backup-$timestamp.tar"
|
$backupFile = "postgres-volume-backup-$timestamp.tar"
|
||||||
|
$backupFilePath = Join-Path $backupDir $backupFile
|
||||||
|
$redundantBackupDir = "D:\Pr00jects\gitea-docker"
|
||||||
|
$logFile = Join-Path $backupDir "volume-backup-log.txt"
|
||||||
|
$currentDir = Get-Location
|
||||||
|
|
||||||
# Ensure backup directory exists
|
# Ensure backup directory exists
|
||||||
if (-not (Test-Path $backupDir)) {
|
if (-not (Test-Path $backupDir)) {
|
||||||
New-Item -ItemType Directory -Path $backupDir
|
New-Item -ItemType Directory -Path $backupDir
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Ensure redundant backup directory exists
|
||||||
|
if (-not (Test-Path $redundantBackupDir)) {
|
||||||
|
try {
|
||||||
|
New-Item -ItemType Directory -Path $redundantBackupDir -Force
|
||||||
|
} catch {
|
||||||
|
Add-Content -Path $logFile -Value "[$timestamp] Warning: Could not create redundant backup directory at $redundantBackupDir. Error: $_"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Log start of backup
|
||||||
|
Add-Content -Path $logFile -Value "[$timestamp] Starting volume backup for $volumeName..."
|
||||||
|
|
||||||
# Check if volume exists
|
# Check if volume exists
|
||||||
$volumeExists = docker volume ls --format "{{.Name}}" | Select-String -Pattern "^$volumeName$"
|
$volumeExists = docker volume ls --format "{{.Name}}" | Select-String -Pattern "^$volumeName$"
|
||||||
if (-not $volumeExists) {
|
if (-not $volumeExists) {
|
||||||
Write-Host "Volume $volumeName not found!" -ForegroundColor Red
|
Write-Host "Volume $volumeName not found!" -ForegroundColor Red
|
||||||
|
Add-Content -Path $logFile -Value "[$timestamp] Error: Volume $volumeName not found!"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# Create a temporary container to access the volume
|
# Create a temporary container to access the volume
|
||||||
Write-Host "Creating backup of Docker volume $volumeName..."
|
Write-Host "Creating backup of Docker volume $volumeName..."
|
||||||
docker run --rm -v ${volumeName}:/volume -v ${PWD}/${backupDir}:/backup alpine tar -cf /backup/$(Split-Path $backupFile -Leaf) -C /volume ./
|
Add-Content -Path $logFile -Value "[$timestamp] Creating backup to $backupFilePath..."
|
||||||
|
|
||||||
|
# Change to backup directory and use simple relative paths for Docker
|
||||||
|
Set-Location -Path $backupDir
|
||||||
|
docker run --rm -v ${volumeName}:/volume -v ${PWD}:/backup alpine tar -cf /backup/$backupFile -C /volume ./
|
||||||
|
|
||||||
|
# Restore original directory
|
||||||
|
Set-Location -Path $currentDir
|
||||||
|
|
||||||
# Check if backup was successful
|
# Check if backup was successful
|
||||||
if ($LASTEXITCODE -eq 0 -and (Test-Path $backupFile) -and (Get-Item $backupFile).Length -gt 0) {
|
if ($LASTEXITCODE -eq 0 -and (Test-Path $backupFilePath) -and (Get-Item $backupFilePath).Length -gt 0) {
|
||||||
Write-Host "Volume backup completed successfully to $backupFile!" -ForegroundColor Green
|
Write-Host "Volume backup completed successfully to $backupFilePath!" -ForegroundColor Green
|
||||||
|
Add-Content -Path $logFile -Value "[$timestamp] Volume backup completed successfully!"
|
||||||
|
|
||||||
# Optional: Compress the backup file
|
# Optional: Compress the backup file
|
||||||
Write-Host "Compressing backup file..."
|
Write-Host "Compressing backup file..."
|
||||||
Compress-Archive -Path $backupFile -DestinationPath "$backupFile.zip" -Force
|
Add-Content -Path $logFile -Value "[$timestamp] Compressing backup file..."
|
||||||
Remove-Item $backupFile
|
$zipFileName = "$backupFilePath.zip"
|
||||||
Write-Host "Backup compressed to $backupFile.zip" -ForegroundColor Green
|
Compress-Archive -Path $backupFilePath -DestinationPath $zipFileName -Force
|
||||||
|
Remove-Item $backupFilePath
|
||||||
|
Write-Host "Backup compressed to $zipFileName" -ForegroundColor Green
|
||||||
|
Add-Content -Path $logFile -Value "[$timestamp] Backup compressed to $zipFileName"
|
||||||
|
|
||||||
|
# Copy backup to redundant location
|
||||||
|
if (Test-Path $redundantBackupDir) {
|
||||||
|
try {
|
||||||
|
$redundantBackupFile = Join-Path $redundantBackupDir (Split-Path -Leaf $zipFileName)
|
||||||
|
Copy-Item -Path $zipFileName -Destination $redundantBackupFile -Force
|
||||||
|
Write-Host "Backup copied to redundant location: $redundantBackupFile" -ForegroundColor Green
|
||||||
|
Add-Content -Path $logFile -Value "[$timestamp] Backup copied to redundant location: $redundantBackupFile"
|
||||||
|
} catch {
|
||||||
|
Write-Host "Error copying backup to redundant location: $_" -ForegroundColor Red
|
||||||
|
Add-Content -Path $logFile -Value "[$timestamp] Error copying backup to redundant location: $_"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Write-Host "Warning: Redundant backup directory not found: $redundantBackupDir" -ForegroundColor Yellow
|
||||||
|
Add-Content -Path $logFile -Value "[$timestamp] Warning: Redundant backup directory not found: $redundantBackupDir"
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Write-Host "Volume backup failed!" -ForegroundColor Red
|
Write-Host "Volume backup failed!" -ForegroundColor Red
|
||||||
|
Add-Content -Path $logFile -Value "[$timestamp] Volume backup failed!"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Optional: Clean up old volume backups (keep last 5)
|
# Optional: Clean up old volume backups (keep last 5)
|
||||||
@@ -38,4 +84,5 @@ $oldBackups = Get-ChildItem -Path $backupDir -Filter "postgres-volume-backup-*.z
|
|||||||
foreach ($backup in $oldBackups) {
|
foreach ($backup in $oldBackups) {
|
||||||
Remove-Item $backup.FullName
|
Remove-Item $backup.FullName
|
||||||
Write-Host "Removed old volume backup: $($backup.Name)"
|
Write-Host "Removed old volume backup: $($backup.Name)"
|
||||||
|
Add-Content -Path $logFile -Value "[$timestamp] Removed old volume backup: $($backup.Name)"
|
||||||
}
|
}
|
||||||
Binary file not shown.
Binary file not shown.
BIN
backups/gitea-db-backup-2025-04-19_19-40-45.sql.zip
Normal file
BIN
backups/gitea-db-backup-2025-04-19_19-40-45.sql.zip
Normal file
Binary file not shown.
BIN
backups/gitea-db-backup-2025-04-19_19-41-01.sql.zip
Normal file
BIN
backups/gitea-db-backup-2025-04-19_19-41-01.sql.zip
Normal file
Binary file not shown.
BIN
backups/gitea-db-backup-2025-04-19_19-43-44.sql.zip
Normal file
BIN
backups/gitea-db-backup-2025-04-19_19-43-44.sql.zip
Normal file
Binary file not shown.
BIN
backups/gitea-db-backup-2025-04-19_19-44-36.sql.zip
Normal file
BIN
backups/gitea-db-backup-2025-04-19_19-44-36.sql.zip
Normal file
Binary file not shown.
BIN
backups/postgres-volume-backup-2025-04-19_19-42-55.tar.zip
Normal file
BIN
backups/postgres-volume-backup-2025-04-19_19-42-55.tar.zip
Normal file
Binary file not shown.
BIN
backups/postgres-volume-backup-2025-04-19_19-43-53.tar.zip
Normal file
BIN
backups/postgres-volume-backup-2025-04-19_19-43-53.tar.zip
Normal file
Binary file not shown.
BIN
backups/postgres-volume-backup-2025-04-19_19-44-40.tar.zip
Normal file
BIN
backups/postgres-volume-backup-2025-04-19_19-44-40.tar.zip
Normal file
Binary file not shown.
24
backups/volume-backup-log.txt
Normal file
24
backups/volume-backup-log.txt
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
[2025-04-19_19-40-51] Starting volume backup for gitea-docker_postgres-data...
|
||||||
|
[2025-04-19_19-40-51] Creating backup to C:\Users\benne\Projects\gitea-docker\backups\postgres-volume-backup-2025-04-19_19-40-51.tar...
|
||||||
|
[2025-04-19_19-40-51] Volume backup failed!
|
||||||
|
[2025-04-19_19-41-05] Starting volume backup for gitea-docker_postgres-data...
|
||||||
|
[2025-04-19_19-41-05] Creating backup to C:\Users\benne\Projects\gitea-docker\backups\postgres-volume-backup-2025-04-19_19-41-05.tar...
|
||||||
|
[2025-04-19_19-41-05] Volume backup failed!
|
||||||
|
[2025-04-19_19-42-55] Starting volume backup for gitea-docker_postgres-data...
|
||||||
|
[2025-04-19_19-42-55] Creating backup to C:\Users\benne\Projects\gitea-docker\backups\postgres-volume-backup-2025-04-19_19-42-55.tar...
|
||||||
|
[2025-04-19_19-42-55] Volume backup completed successfully!
|
||||||
|
[2025-04-19_19-42-55] Compressing backup file...
|
||||||
|
[2025-04-19_19-42-55] Backup compressed to C:\Users\benne\Projects\gitea-docker\backups\postgres-volume-backup-2025-04-19_19-42-55.tar.zip
|
||||||
|
[2025-04-19_19-42-55] Backup copied to redundant location: D:\Pr00jects\gitea-docker\postgres-volume-backup-2025-04-19_19-42-55.tar.zip
|
||||||
|
[2025-04-19_19-43-53] Starting volume backup for gitea-docker_postgres-data...
|
||||||
|
[2025-04-19_19-43-53] Creating backup to C:\Users\benne\Projects\gitea-docker\backups\postgres-volume-backup-2025-04-19_19-43-53.tar...
|
||||||
|
[2025-04-19_19-43-53] Volume backup completed successfully!
|
||||||
|
[2025-04-19_19-43-53] Compressing backup file...
|
||||||
|
[2025-04-19_19-43-53] Backup compressed to C:\Users\benne\Projects\gitea-docker\backups\postgres-volume-backup-2025-04-19_19-43-53.tar.zip
|
||||||
|
[2025-04-19_19-43-53] Backup copied to redundant location: D:\Pr00jects\gitea-docker\postgres-volume-backup-2025-04-19_19-43-53.tar.zip
|
||||||
|
[2025-04-19_19-44-40] Starting volume backup for gitea-docker_postgres-data...
|
||||||
|
[2025-04-19_19-44-40] Creating backup to C:\Users\benne\Projects\gitea-docker\backups\postgres-volume-backup-2025-04-19_19-44-40.tar...
|
||||||
|
[2025-04-19_19-44-40] Volume backup completed successfully!
|
||||||
|
[2025-04-19_19-44-40] Compressing backup file...
|
||||||
|
[2025-04-19_19-44-40] Backup compressed to C:\Users\benne\Projects\gitea-docker\backups\postgres-volume-backup-2025-04-19_19-44-40.tar.zip
|
||||||
|
[2025-04-19_19-44-40] Backup copied to redundant location: D:\Pr00jects\gitea-docker\postgres-volume-backup-2025-04-19_19-44-40.tar.zip
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
# This script is automatically generated - do not edit manually
|
# This script is automatically generated - do not edit manually
|
||||||
# It runs the Docker check script followed by the backup script
|
# It runs the Docker check script followed by database and volume backup scripts
|
||||||
|
|
||||||
# Get the script directory
|
# Get the script directory
|
||||||
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||||
@@ -8,6 +8,10 @@ $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|||||||
$dockerCheckScript = Join-Path $scriptDir "ensure-docker-running.ps1"
|
$dockerCheckScript = Join-Path $scriptDir "ensure-docker-running.ps1"
|
||||||
& $dockerCheckScript
|
& $dockerCheckScript
|
||||||
|
|
||||||
# Then run the backup script
|
# Then run the database backup script
|
||||||
$backupScript = Join-Path $scriptDir "backup-gitea-db.ps1"
|
$dbBackupScript = Join-Path $scriptDir "backup-gitea-db.ps1"
|
||||||
& $backupScript
|
& $dbBackupScript
|
||||||
|
|
||||||
|
# Finally run the volume backup script
|
||||||
|
$volumeBackupScript = Join-Path $scriptDir "backup-volume.ps1"
|
||||||
|
& $volumeBackupScript
|
||||||
|
|||||||
Reference in New Issue
Block a user