83 lines
3.5 KiB
PowerShell
83 lines
3.5 KiB
PowerShell
# 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
|
|
} |