Enhance Gitea database backup script with relative paths, logging, and Docker checks; update scheduling script to include Docker check
This commit is contained in:
@@ -1,32 +1,58 @@
|
|||||||
# Gitea Database Backup Script
|
# Gitea Database Backup Script
|
||||||
$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
|
||||||
$backupFile = "$backupDir\gitea-db-backup-$timestamp.sql"
|
$backupDir = Join-Path $scriptDir "backups"
|
||||||
|
$backupFile = Join-Path $backupDir "gitea-db-backup-$timestamp.sql"
|
||||||
|
|
||||||
# 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# 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
|
# 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
|
docker exec gitea-db pg_dump -U gitea -d gitea > $backupFile
|
||||||
|
|
||||||
# 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 $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
|
# Optional: Compress the backup file
|
||||||
Compress-Archive -Path $backupFile -DestinationPath "$backupFile.zip" -Force
|
Compress-Archive -Path $backupFile -DestinationPath "$backupFile.zip" -Force
|
||||||
Remove-Item $backupFile
|
Remove-Item $backupFile
|
||||||
Write-Host "Backup compressed to $backupFile.zip"
|
"[$timestamp] Backup compressed to $backupFile.zip" | Out-File -Append -FilePath $logFile
|
||||||
} else {
|
} else {
|
||||||
Write-Host "Backup failed!" -ForegroundColor Red
|
"[$timestamp] Backup failed!" | Out-File -Append -FilePath $logFile
|
||||||
}
|
}
|
||||||
|
|
||||||
# Optional: Clean up old backups (keep last 10)
|
# 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
|
$oldBackups = Get-ChildItem -Path $backupDir -Filter "gitea-db-backup-*.zip" | Sort-Object LastWriteTime -Descending | Select-Object -Skip 10
|
||||||
foreach ($backup in $oldBackups) {
|
foreach ($backup in $oldBackups) {
|
||||||
Remove-Item $backup.FullName
|
Remove-Item $backup.FullName
|
||||||
Write-Host "Removed old backup: $($backup.Name)"
|
"[$timestamp] Removed old backup: $($backup.Name)" | Out-File -Append -FilePath $logFile
|
||||||
}
|
}
|
||||||
BIN
backups/backup-log.txt
Normal file
BIN
backups/backup-log.txt
Normal file
Binary file not shown.
BIN
backups/gitea-db-backup-2025-04-19_19-32-22.sql.zip
Normal file
BIN
backups/gitea-db-backup-2025-04-19_19-32-22.sql.zip
Normal file
Binary file not shown.
BIN
backups/gitea-db-backup-2025-04-19_19-34-24.sql.zip
Normal file
BIN
backups/gitea-db-backup-2025-04-19_19-34-24.sql.zip
Normal file
Binary file not shown.
BIN
backups/gitea-db-backup-2025-04-19_19-35-59.sql.zip
Normal file
BIN
backups/gitea-db-backup-2025-04-19_19-35-59.sql.zip
Normal file
Binary file not shown.
83
ensure-docker-running.ps1
Normal file
83
ensure-docker-running.ps1
Normal file
@@ -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
|
||||||
|
}
|
||||||
13
run-backup.ps1
Normal file
13
run-backup.ps1
Normal file
@@ -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
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
# Script to create a scheduled task for Gitea database backups
|
# 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"
|
$taskName = "GiteaDatabaseBackup"
|
||||||
$taskDescription = "Regular backup of Gitea PostgreSQL database"
|
$taskDescription = "Regular backup of Gitea PostgreSQL database"
|
||||||
|
|
||||||
@@ -9,8 +11,32 @@ if (-not (Test-Path $scriptPath)) {
|
|||||||
exit 1
|
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
|
# 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
|
$trigger = New-ScheduledTaskTrigger -Daily -At 3AM
|
||||||
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -DontStopOnIdleEnd -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
|
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -DontStopOnIdleEnd -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user