Files
gitea-docker/schedule-backup.ps1
2025-06-20 19:38:38 -06:00

67 lines
2.9 KiB
PowerShell

# Script to create a scheduled task for Gitea database backups
$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"
# Check if the backup script exists
if (-not (Test-Path $scriptPath)) {
Write-Host "Backup script not found at: $scriptPath" -ForegroundColor Red
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 `"$batchScriptPath`"" -WorkingDirectory "$workingDir"
$trigger = New-ScheduledTaskTrigger -Daily -At 3AM
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -DontStopOnIdleEnd -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
# Create a principal that runs with highest privileges
$principal = New-ScheduledTaskPrincipal -UserId "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
# Register the scheduled task
$taskExists = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
if ($taskExists) {
Write-Host "Task '$taskName' already exists. Updating..." -ForegroundColor Yellow
# Get the existing task
$task = Get-ScheduledTask -TaskName $taskName
# Update the task properties
$task.Actions = $action
$task.Triggers = $trigger
$task.Settings = $settings
$task.Principal = $principal
$task.Description = $taskDescription
# Save the updated task
Set-ScheduledTask -InputObject $task
} else {
Write-Host "Creating new scheduled task '$taskName'..." -ForegroundColor Green
Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Settings $settings -Description $taskDescription -Principal $principal
}
Write-Host "Scheduled task setup complete. The database will be backed up daily at 3 AM." -ForegroundColor Green
Write-Host "Backup files will be stored in the 'backups' folder in your Gitea Docker directory." -ForegroundColor Green