diff --git a/backup-gitea-db.ps1 b/backup-gitea-db.ps1 index a20de87..b2c4db1 100644 --- a/backup-gitea-db.ps1 +++ b/backup-gitea-db.ps1 @@ -3,12 +3,22 @@ $timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss" $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path $backupDir = Join-Path $scriptDir "backups" $backupFile = Join-Path $backupDir "gitea-db-backup-$timestamp.sql" +$redundantBackupDir = "D:\Pr00jects\gitea-docker" # 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 { + "[$timestamp] Warning: Could not create redundant backup directory at $redundantBackupDir. Error: $_" | Out-File -Append -FilePath $logFile + } +} + # Log execution of the script $logFile = Join-Path $backupDir "backup-log.txt" "[$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 # 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 - "[$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 { "[$timestamp] Backup failed!" | Out-File -Append -FilePath $logFile } diff --git a/backup-volume.ps1 b/backup-volume.ps1 index 29f545d..ff346f9 100644 --- a/backup-volume.ps1 +++ b/backup-volume.ps1 @@ -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)" } \ No newline at end of file diff --git a/backups/backup-log.txt b/backups/backup-log.txt index 64f85eb..21f5373 100644 Binary files a/backups/backup-log.txt and b/backups/backup-log.txt differ diff --git a/backups/gitea-db-backup-2025-03-01_23-22-57.sql.zip b/backups/gitea-db-backup-2025-03-01_23-22-57.sql.zip deleted file mode 100644 index 455ee6e..0000000 Binary files a/backups/gitea-db-backup-2025-03-01_23-22-57.sql.zip and /dev/null differ diff --git a/backups/gitea-db-backup-2025-04-19_19-40-45.sql.zip b/backups/gitea-db-backup-2025-04-19_19-40-45.sql.zip new file mode 100644 index 0000000..d24ff69 Binary files /dev/null and b/backups/gitea-db-backup-2025-04-19_19-40-45.sql.zip differ diff --git a/backups/gitea-db-backup-2025-04-19_19-41-01.sql.zip b/backups/gitea-db-backup-2025-04-19_19-41-01.sql.zip new file mode 100644 index 0000000..df77ba9 Binary files /dev/null and b/backups/gitea-db-backup-2025-04-19_19-41-01.sql.zip differ diff --git a/backups/gitea-db-backup-2025-04-19_19-43-44.sql.zip b/backups/gitea-db-backup-2025-04-19_19-43-44.sql.zip new file mode 100644 index 0000000..fc66f55 Binary files /dev/null and b/backups/gitea-db-backup-2025-04-19_19-43-44.sql.zip differ diff --git a/backups/gitea-db-backup-2025-04-19_19-44-36.sql.zip b/backups/gitea-db-backup-2025-04-19_19-44-36.sql.zip new file mode 100644 index 0000000..ac5020d Binary files /dev/null and b/backups/gitea-db-backup-2025-04-19_19-44-36.sql.zip differ diff --git a/backups/postgres-volume-backup-2025-04-19_19-42-55.tar.zip b/backups/postgres-volume-backup-2025-04-19_19-42-55.tar.zip new file mode 100644 index 0000000..23266b6 Binary files /dev/null and b/backups/postgres-volume-backup-2025-04-19_19-42-55.tar.zip differ diff --git a/backups/postgres-volume-backup-2025-04-19_19-43-53.tar.zip b/backups/postgres-volume-backup-2025-04-19_19-43-53.tar.zip new file mode 100644 index 0000000..33cd340 Binary files /dev/null and b/backups/postgres-volume-backup-2025-04-19_19-43-53.tar.zip differ diff --git a/backups/postgres-volume-backup-2025-04-19_19-44-40.tar.zip b/backups/postgres-volume-backup-2025-04-19_19-44-40.tar.zip new file mode 100644 index 0000000..f7d7500 Binary files /dev/null and b/backups/postgres-volume-backup-2025-04-19_19-44-40.tar.zip differ diff --git a/backups/volume-backup-log.txt b/backups/volume-backup-log.txt new file mode 100644 index 0000000..738e582 --- /dev/null +++ b/backups/volume-backup-log.txt @@ -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 diff --git a/run-backup.ps1 b/run-backup.ps1 index d942bc7..96c8aac 100644 --- a/run-backup.ps1 +++ b/run-backup.ps1 @@ -1,5 +1,5 @@ # 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 $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 -# Then run the backup script -$backupScript = Join-Path $scriptDir "backup-gitea-db.ps1" -& $backupScript +# Then run the database backup script +$dbBackupScript = Join-Path $scriptDir "backup-gitea-db.ps1" +& $dbBackupScript + +# Finally run the volume backup script +$volumeBackupScript = Join-Path $scriptDir "backup-volume.ps1" +& $volumeBackupScript