add retry logic to Cloud Backup download

This commit is contained in:
2026-11-17 01:01:10 -05:00
parent bfdf8f5eae
commit 2bbb07c05e
3 changed files with 69 additions and 12 deletions
+41 -3
View File
@@ -916,16 +916,54 @@ Function Get-EmberkomCloudBackupRelease {
Function Save-EmberkomCloudBackupInstaller { Function Save-EmberkomCloudBackupInstaller {
param([Parameter(Mandatory=$true)]$Release, [Parameter(Mandatory=$true)][string]$Path) param([Parameter(Mandatory=$true)]$Release, [Parameter(Mandatory=$true)][string]$Path)
$client = New-Object Net.WebClient $ProgressPreference = 'SilentlyContinue'
$sourceHost = ([uri]$Release.Url).Host
$maxAttempts = 2
$timeoutSeconds = 240
Write-Host "MSP360 $($Release.Version) installer target: $Path (expected SHA-256 $($Release.SHA256))."
try { try {
$client.DownloadFile($Release.Url, $Path) for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) {
$elapsed = [Diagnostics.Stopwatch]::StartNew()
$utc = [datetime]::UtcNow.ToString('u')
Write-Host "$utc $env:COMPUTERNAME - MSP360 installer download from $sourceHost (attempt $attempt of $maxAttempts, request timeout $timeoutSeconds seconds)."
try {
Invoke-WebRequest -UseBasicParsing -Uri $Release.Url -OutFile $Path -TimeoutSec $timeoutSeconds -ErrorAction Stop
break
} catch {
$failure = $_.Exception
while ($failure.InnerException -and $failure -isnot [Net.WebException]) { $failure = $failure.InnerException }
$failureHost = $sourceHost
if ($failure -is [Net.WebException]) {
$reason = [string]$failure.Status
$retry = $reason -in @('Timeout','NameResolutionFailure','ProxyNameResolutionFailure','ConnectFailure',
'ConnectionClosed','KeepAliveFailure','SendFailure','ReceiveFailure','PipelineFailure')
if ($failure.Response -is [Net.HttpWebResponse]) {
$status = [int]$failure.Response.StatusCode
$failureHost = $failure.Response.ResponseUri.Host
$failure.Response.Close()
$reason = "HTTP $status"
$retry = $status -in @(408,429,500,502,503,504)
}
} elseif ($failure -is [Net.Sockets.SocketException]) {
$reason = [string]$failure.SocketErrorCode
$retry = $true
} else { throw }
$bytes = if (Test-Path -LiteralPath $Path) { (Get-Item -LiteralPath $Path).Length } else { 0 }
$detail = "MSP360 download attempt $attempt of $maxAttempts from $failureHost failed ($reason) after $([int]$elapsed.Elapsed.TotalSeconds)s, $bytes bytes saved."
if (!$retry -or $attempt -eq $maxAttempts) { throw $detail }
Write-Host "$detail Retrying in 3 seconds."
if (Test-Path -LiteralPath $Path) { Remove-Item -LiteralPath $Path -Force -ErrorAction Stop }
Start-Sleep -Seconds 3
}
}
if ((Get-FileHash -LiteralPath $Path -Algorithm SHA256 -ErrorAction Stop).Hash -ne $Release.SHA256) { if ((Get-FileHash -LiteralPath $Path -Algorithm SHA256 -ErrorAction Stop).Hash -ne $Release.SHA256) {
throw 'The installer SHA-256 does not match the published release.' throw 'The installer SHA-256 does not match the published release.'
} }
Write-Host 'MSP360 installer download complete. SHA-256 verified.'
} catch { } catch {
if (Test-Path -LiteralPath $Path) { Remove-Item -LiteralPath $Path -Force -ErrorAction Stop } if (Test-Path -LiteralPath $Path) { Remove-Item -LiteralPath $Path -Force -ErrorAction Stop }
throw throw
} finally { $client.Dispose() } }
} }
# Function to install the MSP360 Powershell module # Function to install the MSP360 Powershell module
+2
View File
@@ -8,6 +8,8 @@ On each daily run, the updater reads the installed executable's file version and
Only a newer matching build triggers a plan check and installer download. Immediately before launching setup, the updater checks again for MSP360's `CBBackupPlan.exe` worker. A busy endpoint defers to the next scheduled run. A plan can still start after the final check, so keep the RMM slot clear of scheduled plans. [MSP360 plan worker documentation](https://help.mspbackups.com/troubleshooting) Only a newer matching build triggers a plan check and installer download. Immediately before launching setup, the updater checks again for MSP360's `CBBackupPlan.exe` worker. A busy endpoint defers to the next scheduled run. A plan can still start after the final check, so keep the RMM slot clear of scheduled plans. [MSP360 plan worker documentation](https://help.mspbackups.com/troubleshooting)
The installer download uses a 240-second request timeout and retries once after a timeout or transient network failure. Syncro reports the endpoint name, UTC start time, source hostname, temporary installer path and expected SHA-256. For a failed network attempt, it also reports the reason, elapsed time and bytes saved. An endpoint security provider can use these details to look for a matching event. If both attempts time out, check outbound HTTPS from the endpoint under SYSTEM to the console download host and the redirected storage host. The Windows 8.7.1.177 link redirects from `console.msp360.com` to `s3.us-east-1.amazonaws.com`.
**Syncro output and exit codes** **Syncro output and exit codes**
| Code | Meaning | | Code | Meaning |
+26 -9
View File
@@ -66,17 +66,23 @@ try {
} }
throw "Unexpected URL" throw "Unexpected URL"
} }
$client | Add-Member ScriptMethod DownloadFile { param($url,$path)
$record.Downloads++
if ($url -cne $manifest.$kind.Url) { throw 'Downloaded the wrong edition' }
$record.Staged = $path
[IO.File]::WriteAllBytes($path, $fixtureBytes)
if ($scenario -eq 'corrupt') { [IO.File]::AppendAllText($path,'x') }
if ($scenario -eq 'download failure') { throw 'Transfer interrupted' }
}
$client | Add-Member ScriptMethod Dispose {} $client | Add-Member ScriptMethod Dispose {}
return $client return $client
} }
function Invoke-WebRequest {
param([switch]$UseBasicParsing,$Uri,$OutFile,$TimeoutSec,$ErrorAction)
$record.Downloads++
if ($Uri -cne $manifest.$kind.Url -or !$UseBasicParsing -or $TimeoutSec -ne 240) { throw 'Unexpected installer request' }
if (Test-Path -LiteralPath $OutFile) { throw 'Partial installer was not removed before retry' }
$record.Staged = $OutFile
[IO.File]::WriteAllBytes($OutFile, $fixtureBytes)
if ($scenario -eq 'corrupt') { [IO.File]::AppendAllText($OutFile,'x') }
if ($scenario -eq 'download failure') { throw [Net.WebException]::new('Certificate validation failed',[Net.WebExceptionStatus]::TrustFailure) }
if ($scenario -eq 'persistent timeout' -or ($scenario -in @('timeout then success','plan during retry') -and $record.Downloads -eq 1)) {
throw [Net.WebException]::new('The operation has timed out',[Net.WebExceptionStatus]::Timeout)
}
}
function Start-Sleep { param($Seconds) if ($Seconds -ne 3) { throw 'Unexpected retry delay' } }
function Get-FixtureInstallation { function Get-FixtureInstallation {
if ($scenario -eq 'absent') { return } if ($scenario -eq 'absent') { return }
$v = '8.9.1.177' $v = '8.9.1.177'
@@ -102,7 +108,7 @@ try {
$record.PlanChecks++ $record.PlanChecks++
if ($ClassName -ne 'Win32_Process' -or $Filter -ne "Name = 'CBBackupPlan.exe'") { throw 'Unexpected worker query' } if ($ClassName -ne 'Win32_Process' -or $Filter -ne "Name = 'CBBackupPlan.exe'") { throw 'Unexpected worker query' }
if ($scenario -eq 'query failure') { throw 'Cannot enumerate processes' } if ($scenario -eq 'query failure') { throw 'Cannot enumerate processes' }
if ($scenario -eq 'busy' -or ($record.Downloads -and $scenario -eq 'late plan')) { [pscustomobject]@{ ProcessId=42 } } if ($scenario -eq 'busy' -or ($record.Downloads -and $scenario -in @('late plan','plan during retry'))) { [pscustomobject]@{ ProcessId=42 } }
} }
function Start-Process { function Start-Process {
param($FilePath,$ArgumentList,$WindowStyle,[switch]$Wait,[switch]$PassThru,$ErrorAction) param($FilePath,$ArgumentList,$WindowStyle,[switch]$Wait,[switch]$PassThru,$ErrorAction)
@@ -125,6 +131,7 @@ try {
@('only VM newer',0,0,0), @('VM needs update',0,1,1), @('incomplete Windows',1,0,0), @('only VM newer',0,0,0), @('VM needs update',0,1,1), @('incomplete Windows',1,0,0),
@('busy',0,0,0), @('late plan',0,1,0), @('query failure',1,0,0), @('busy',0,0,0), @('late plan',0,1,0), @('query failure',1,0,0),
@('download failure',1,1,0), @('already updated',0,1,0), @('window ended',0,1,0), @('download failure',1,1,0), @('already updated',0,1,0), @('window ended',0,1,0),
@('timeout then success',0,2,1), @('plan during retry',0,2,0), @('persistent timeout',1,2,0),
@('bad window',1,0,0), @('corrupt',1,1,0), @('success',0,1,1), @('bad window',1,0,0), @('corrupt',1,1,0), @('success',0,1,1),
@('VM',0,1,1), @('failure',1,1,1), @('unverified',1,1,1), @('VM',0,1,1), @('failure',1,1,1), @('unverified',1,1,1),
@('service down',1,1,1), @('reboot',3010,1,1) @('service down',1,1,1), @('reboot',3010,1,1)
@@ -149,6 +156,16 @@ try {
($messages -join ' ') -notmatch 'No update needed') { throw "$scenario did not take the expected early exit" } ($messages -join ' ') -notmatch 'No update needed') { throw "$scenario did not take the expected early exit" }
} }
if ($record.Staged -and (Test-Path -LiteralPath $record.Staged)) { throw "$scenario left staged media" } if ($record.Staged -and (Test-Path -LiteralPath $record.Staged)) { throw "$scenario left staged media" }
if ($scenario -eq 'persistent timeout') {
$output = $messages -join ' '
if ($output -notmatch 'attempt 2 of 2.*Timeout.*3 bytes saved' -or $output -match 'urlParams=') {
throw 'Persistent timeout must report the failed attempt and byte count without the signed URL.'
}
if (!$output.Contains($record.Staged) -or !$output.Contains($hash) -or
$output -notmatch "Z $([regex]::Escape($env:COMPUTERNAME)) - MSP360 installer download") {
throw 'Missing endpoint, UTC time, installer path or hash for endpoint security correlation.'
}
}
Write-Host "PASS $scenario" Write-Host "PASS $scenario"
} }