diff --git a/backup-gitea-db.ps1 b/backup-gitea-db.ps1 index ecdf86d..a20de87 100644 --- a/backup-gitea-db.ps1 +++ b/backup-gitea-db.ps1 @@ -1,32 +1,58 @@ # Gitea Database Backup Script $timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss" -$backupDir = ".\backups" -$backupFile = "$backupDir\gitea-db-backup-$timestamp.sql" +$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path +$backupDir = Join-Path $scriptDir "backups" +$backupFile = Join-Path $backupDir "gitea-db-backup-$timestamp.sql" # Ensure backup directory exists if (-not (Test-Path $backupDir)) { New-Item -ItemType Directory -Path $backupDir } +# Log execution of the script +$logFile = Join-Path $backupDir "backup-log.txt" +"[$timestamp] Starting database backup..." | Out-File -Append -FilePath $logFile + +# Check if Docker is running +$dockerRunning = $false +try { + $dockerStatus = docker info 2>&1 + $dockerRunning = $LASTEXITCODE -eq 0 +} catch { + $dockerRunning = $false +} + +if (-not $dockerRunning) { + "[$timestamp] Error: Docker is not running. Backup failed." | Out-File -Append -FilePath $logFile + exit 1 +} + +# Check if Gitea container is running +$containerRunning = docker ps --format "{{.Names}}" | Select-String -Pattern "gitea-db" -Quiet +if (-not $containerRunning) { + "[$timestamp] Error: Gitea database container is not running. Backup failed." | Out-File -Append -FilePath $logFile + exit 1 +} + # Create database dump -Write-Host "Creating database backup to $backupFile..." +"[$timestamp] Creating database backup to $backupFile..." | Out-File -Append -FilePath $logFile docker exec gitea-db pg_dump -U gitea -d gitea > $backupFile # Check if backup was successful if ($LASTEXITCODE -eq 0 -and (Test-Path $backupFile) -and (Get-Item $backupFile).Length -gt 0) { - Write-Host "Backup completed successfully!" + "[$timestamp] Backup completed successfully!" | Out-File -Append -FilePath $logFile # Optional: Compress the backup file Compress-Archive -Path $backupFile -DestinationPath "$backupFile.zip" -Force Remove-Item $backupFile - Write-Host "Backup compressed to $backupFile.zip" + "[$timestamp] Backup compressed to $backupFile.zip" | Out-File -Append -FilePath $logFile } else { - Write-Host "Backup failed!" -ForegroundColor Red + "[$timestamp] Backup failed!" | Out-File -Append -FilePath $logFile } # Optional: Clean up old backups (keep last 10) $oldBackups = Get-ChildItem -Path $backupDir -Filter "gitea-db-backup-*.zip" | Sort-Object LastWriteTime -Descending | Select-Object -Skip 10 foreach ($backup in $oldBackups) { Remove-Item $backup.FullName - Write-Host "Removed old backup: $($backup.Name)" + "[$timestamp] Removed old backup: $($backup.Name)" | Out-File -Append -FilePath $logFile } \ No newline at end of file diff --git a/backups/backup-log.txt b/backups/backup-log.txt new file mode 100644 index 0000000..64f85eb Binary files /dev/null and b/backups/backup-log.txt differ diff --git a/backups/gitea-db-backup-2025-04-19_19-32-22.sql.zip b/backups/gitea-db-backup-2025-04-19_19-32-22.sql.zip new file mode 100644 index 0000000..a18ba2a Binary files /dev/null and b/backups/gitea-db-backup-2025-04-19_19-32-22.sql.zip differ diff --git a/backups/gitea-db-backup-2025-04-19_19-34-24.sql.zip b/backups/gitea-db-backup-2025-04-19_19-34-24.sql.zip new file mode 100644 index 0000000..140ef4f Binary files /dev/null and b/backups/gitea-db-backup-2025-04-19_19-34-24.sql.zip differ diff --git a/backups/gitea-db-backup-2025-04-19_19-35-59.sql.zip b/backups/gitea-db-backup-2025-04-19_19-35-59.sql.zip new file mode 100644 index 0000000..024aea8 Binary files /dev/null and b/backups/gitea-db-backup-2025-04-19_19-35-59.sql.zip differ diff --git a/ensure-docker-running.ps1 b/ensure-docker-running.ps1 new file mode 100644 index 0000000..9b35ad1 --- /dev/null +++ b/ensure-docker-running.ps1 @@ -0,0 +1,83 @@ +# Script to ensure Docker is running before scheduled backup +$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path +$logFile = Join-Path $scriptDir "backups\docker-status.log" +$timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss" + +# Ensure log directory exists +$backupDir = Join-Path $scriptDir "backups" +if (-not (Test-Path $backupDir)) { + New-Item -ItemType Directory -Path $backupDir +} + +"[$timestamp] Checking Docker status..." | Out-File -Append -FilePath $logFile + +# Check if Docker Desktop is running +$dockerProcess = Get-Process "Docker Desktop" -ErrorAction SilentlyContinue +if ($null -eq $dockerProcess) { + "[$timestamp] Docker Desktop is not running. Attempting to start..." | Out-File -Append -FilePath $logFile + + # Path to Docker Desktop + $dockerPath = "C:\Program Files\Docker\Docker\Docker Desktop.exe" + if (Test-Path $dockerPath) { + Start-Process $dockerPath + "[$timestamp] Started Docker Desktop." | Out-File -Append -FilePath $logFile + + # Wait for Docker to start up (give it 60 seconds) + $maxWaitTime = 60 + $waitTime = 0 + $dockerRunning = $false + + while (-not $dockerRunning -and $waitTime -lt $maxWaitTime) { + Start-Sleep -Seconds 5 + $waitTime += 5 + + try { + $dockerStatus = docker info 2>&1 + if ($LASTEXITCODE -eq 0) { + $dockerRunning = $true + "[$timestamp] Docker is now running after waiting $waitTime seconds." | Out-File -Append -FilePath $logFile + } + } catch { + # Keep waiting + } + } + + if (-not $dockerRunning) { + "[$timestamp] Docker did not start successfully after waiting $maxWaitTime seconds." | Out-File -Append -FilePath $logFile + } + } else { + "[$timestamp] Docker Desktop executable not found at expected location: $dockerPath" | Out-File -Append -FilePath $logFile + } +} else { + "[$timestamp] Docker Desktop is already running." | Out-File -Append -FilePath $logFile +} + +# Check if containers are running +try { + $containersRunning = docker ps 2>&1 + if ($LASTEXITCODE -eq 0) { + "[$timestamp] Docker containers status: " | Out-File -Append -FilePath $logFile + $runningContainers = docker ps --format "{{.Names}}" 2>&1 + $runningContainers | Out-File -Append -FilePath $logFile + + # Check specifically for gitea containers + $giteaRunning = $runningContainers | Select-String -Pattern "gitea" -Quiet + if (-not $giteaRunning) { + "[$timestamp] Gitea containers are not running. Starting containers..." | Out-File -Append -FilePath $logFile + + # Navigate to gitea-docker directory and start containers + Set-Location $scriptDir + docker-compose up -d 2>&1 | Out-File -Append -FilePath $logFile + + if ($LASTEXITCODE -eq 0) { + "[$timestamp] Successfully started Gitea containers." | Out-File -Append -FilePath $logFile + } else { + "[$timestamp] Failed to start Gitea containers." | Out-File -Append -FilePath $logFile + } + } + } else { + "[$timestamp] Failed to check running containers. Docker might not be ready yet." | Out-File -Append -FilePath $logFile + } +} catch { + "[$timestamp] Error checking Docker containers: $_" | Out-File -Append -FilePath $logFile +} \ No newline at end of file diff --git a/run-backup.ps1 b/run-backup.ps1 new file mode 100644 index 0000000..d942bc7 --- /dev/null +++ b/run-backup.ps1 @@ -0,0 +1,13 @@ +# This script is automatically generated - do not edit manually +# It runs the Docker check script followed by the backup script + +# Get the script directory +$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path + +# Run the Docker check script first +$dockerCheckScript = Join-Path $scriptDir "ensure-docker-running.ps1" +& $dockerCheckScript + +# Then run the backup script +$backupScript = Join-Path $scriptDir "backup-gitea-db.ps1" +& $backupScript diff --git a/schedule-backup.ps1 b/schedule-backup.ps1 index ace60c5..c18d8cb 100644 --- a/schedule-backup.ps1 +++ b/schedule-backup.ps1 @@ -1,5 +1,7 @@ # Script to create a scheduled task for Gitea database backups -$scriptPath = Join-Path (Get-Location) "backup-gitea-db.ps1" +$workingDir = Split-Path -Parent $MyInvocation.MyCommand.Path +$scriptPath = Join-Path $workingDir "backup-gitea-db.ps1" +$dockerCheckScript = Join-Path $workingDir "ensure-docker-running.ps1" $taskName = "GiteaDatabaseBackup" $taskDescription = "Regular backup of Gitea PostgreSQL database" @@ -9,8 +11,32 @@ if (-not (Test-Path $scriptPath)) { exit 1 } +# Check if the Docker check script exists +if (-not (Test-Path $dockerCheckScript)) { + Write-Host "Docker check script not found at: $dockerCheckScript" -ForegroundColor Red + exit 1 +} + +# Create a batch script that runs both scripts +$batchScriptPath = Join-Path $workingDir "run-backup.ps1" +@" +# This script is automatically generated - do not edit manually +# It runs the Docker check script followed by the backup script + +# Get the script directory +`$scriptDir = Split-Path -Parent `$MyInvocation.MyCommand.Path + +# Run the Docker check script first +`$dockerCheckScript = Join-Path `$scriptDir "ensure-docker-running.ps1" +& `$dockerCheckScript + +# Then run the backup script +`$backupScript = Join-Path `$scriptDir "backup-gitea-db.ps1" +& `$backupScript +"@ | Out-File -FilePath $batchScriptPath -Encoding utf8 + # Create a scheduled task to run daily at 3 AM -$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`"" +$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$batchScriptPath`"" -WorkingDirectory "$workingDir" $trigger = New-ScheduledTaskTrigger -Daily -At 3AM $settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -DontStopOnIdleEnd -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries