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:
2025-04-19 19:51:28 -06:00
parent cba5e8167b
commit 6695ad06a6
13 changed files with 113 additions and 14 deletions

View File

@@ -1,36 +1,82 @@
# Script to backup the entire Postgres Docker volume
$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"
$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
if (-not (Test-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
$volumeExists = docker volume ls --format "{{.Name}}" | Select-String -Pattern "^$volumeName$"
if (-not $volumeExists) {
Write-Host "Volume $volumeName not found!" -ForegroundColor Red
Add-Content -Path $logFile -Value "[$timestamp] Error: Volume $volumeName not found!"
exit 1
}
# Create a temporary container to access the volume
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
if ($LASTEXITCODE -eq 0 -and (Test-Path $backupFile) -and (Get-Item $backupFile).Length -gt 0) {
Write-Host "Volume backup completed successfully to $backupFile!" -ForegroundColor Green
if ($LASTEXITCODE -eq 0 -and (Test-Path $backupFilePath) -and (Get-Item $backupFilePath).Length -gt 0) {
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
Write-Host "Compressing backup file..."
Compress-Archive -Path $backupFile -DestinationPath "$backupFile.zip" -Force
Remove-Item $backupFile
Write-Host "Backup compressed to $backupFile.zip" -ForegroundColor Green
Add-Content -Path $logFile -Value "[$timestamp] Compressing backup file..."
$zipFileName = "$backupFilePath.zip"
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 {
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)
@@ -38,4 +84,5 @@ $oldBackups = Get-ChildItem -Path $backupDir -Filter "postgres-volume-backup-*.z
foreach ($backup in $oldBackups) {
Remove-Item $backup.FullName
Write-Host "Removed old volume backup: $($backup.Name)"
Add-Content -Path $logFile -Value "[$timestamp] Removed old volume backup: $($backup.Name)"
}