made the backup update script fail faster

This commit is contained in:
2026-11-17 00:36:59 -05:00
parent 467f699103
commit bfdf8f5eae
5 changed files with 76 additions and 28 deletions
+20 -7
View File
@@ -886,19 +886,32 @@ Function Disable-Log {
} }
# Published packages for both Install- and Update-EmberkomCloudBackup. # Published packages for both Install- and Update-EmberkomCloudBackup.
Function Get-EmberkomCloudBackupRelease { Function Get-EmberkomCloudBackupReleases {
param([Parameter(Mandatory=$true)][ValidateSet('Windows','VM')][string]$Build)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$client = New-Object Net.WebClient $client = New-Object Net.WebClient
try { try {
$json = $client.DownloadString('https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/resources/EmberkomCloudBackup.json') $json = $client.DownloadString('https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/resources/EmberkomCloudBackup.json')
} finally { $client.Dispose() } } finally { $client.Dispose() }
$release = ($json | ConvertFrom-Json -ErrorAction Stop).$Build $manifest = $json | ConvertFrom-Json -ErrorAction Stop
if ($release.Version -notmatch '^\d+\.\d+\.\d+\.\d+$' -or $release.Url -notmatch '^https://' -or $releases = @{}
$release.SHA256 -notmatch '^[a-f0-9]{64}$') { foreach ($build in 'Windows','VM') {
throw "Incomplete Emberkom Cloud Backup release for $Build." $release = $manifest.$build
$version = $null
if ([version]::TryParse($release.Version, [ref]$version) -and $version.Revision -ge 0 -and $release.Url -match '^https://' -and
$release.SHA256 -match '^[a-f0-9]{64}$') {
$releases[$build] = $release
} }
return $release }
return $releases
}
Function Get-EmberkomCloudBackupRelease {
param(
[Parameter(Mandatory=$true)][ValidateSet('Windows','VM')][string]$Build,
[hashtable]$Releases = (Get-EmberkomCloudBackupReleases)
)
if (!$Releases[$Build]) { throw "Incomplete Emberkom Cloud Backup release for $Build." }
return $Releases[$Build]
} }
Function Save-EmberkomCloudBackupInstaller { Function Save-EmberkomCloudBackupInstaller {
+22 -10
View File
@@ -1,7 +1,7 @@
#Requires -Version 5.1 #Requires -Version 5.1
# Update an existing Emberkom MSP360 Managed Backup agent during an idle RMM window. # Update an existing Emberkom MSP360 Managed Backup agent during an idle RMM window.
function Get-CloudBackupAgent { function Get-CloudBackupInstallation {
# MSP360's managed-agent registration points to the branded uninstall key. # MSP360's managed-agent registration points to the branded uninstall key.
# Open the registry view explicitly so a 32-bit RMM host can see a 64-bit agent. # Open the registry view explicitly so a 32-bit RMM host can see a 64-bit agent.
$views = if ([Environment]::Is64BitOperatingSystem) { 'Registry64', 'Registry32' } else { 'Registry32' } $views = if ([Environment]::Is64BitOperatingSystem) { 'Registry64', 'Registry32' } else { 'Registry32' }
@@ -15,7 +15,10 @@ function Get-CloudBackupAgent {
if (!$product -or !$product.GetValue('')) { throw 'The MSP360 agent registration has no executable path.' } if (!$product -or !$product.GetValue('')) { throw 'The MSP360 agent registration has no executable path.' }
if ($product.GetValue('Publisher') -ne 'Emberkom') { throw 'The registered backup agent belongs to a different publisher.' } if ($product.GetValue('Publisher') -ne 'Emberkom') { throw 'The registered backup agent belongs to a different publisher.' }
$cli = Join-Path (Split-Path -LiteralPath $product.GetValue('')) 'cbb.exe' $cli = Join-Path (Split-Path -LiteralPath $product.GetValue('')) 'cbb.exe'
return (Read-CloudBackupAgent -Cli $cli) return [pscustomobject]@{
Cli = $cli
Version = [version][Diagnostics.FileVersionInfo]::GetVersionInfo($cli).FileVersion
}
} finally { } finally {
if ($product) { $product.Dispose() } if ($product) { $product.Dispose() }
if ($marker) { $marker.Dispose() } if ($marker) { $marker.Dispose() }
@@ -25,8 +28,9 @@ function Get-CloudBackupAgent {
} }
function Read-CloudBackupAgent { function Read-CloudBackupAgent {
param([string]$Cli) param($Installation)
$details = & $Cli ver if (!$Installation) { return }
$details = & $Installation.Cli ver
if ($LASTEXITCODE -ne 0) { throw "MSP360 could not report the installed agent details (exit $LASTEXITCODE)." } if ($LASTEXITCODE -ne 0) { throw "MSP360 could not report the installed agent details (exit $LASTEXITCODE)." }
$edition = $service = $null $edition = $service = $null
foreach ($line in $details) { foreach ($line in $details) {
@@ -43,7 +47,7 @@ function Read-CloudBackupAgent {
} }
if (!$service) { throw 'MSP360 did not report its backup service name.' } if (!$service) { throw 'MSP360 did not report its backup service name.' }
[pscustomobject]@{ [pscustomobject]@{
Version = [version][Diagnostics.FileVersionInfo]::GetVersionInfo($Cli).FileVersion Version = $Installation.Version
Build = $build Build = $build
Service = $service Service = $service
} }
@@ -72,9 +76,17 @@ function Test-CloudBackupPlanRunning {
function Invoke-CloudBackupUpdate { function Invoke-CloudBackupUpdate {
param([string]$Window) param([string]$Window)
if (!(Test-CloudBackupUpdateWindow $Window)) { Write-Host "Deferred. Outside the local update window $Window."; return 0 } if (!(Test-CloudBackupUpdateWindow $Window)) { Write-Host "Deferred. Outside the local update window $Window."; return 0 }
$agent = Get-CloudBackupAgent $installation = Get-CloudBackupInstallation
if (!$agent) { Write-Host 'Skipped. MSP360 Managed Backup is not installed.'; return 0 } if (!$installation) { Write-Host 'Skipped. MSP360 Managed Backup is not installed.'; return 0 }
$release = Get-EmberkomCloudBackupRelease -Build $agent.Build $releases = Get-EmberkomCloudBackupReleases
# If neither package is newer, the daily check can finish without launching cbb.exe.
if ($releases.Count -eq 2 -and $installation.Version -ge [version]$releases.Windows.Version -and
$installation.Version -ge [version]$releases.VM.Version) {
Write-Host "No update needed. Installed $($installation.Version), published Windows $($releases.Windows.Version), VM $($releases.VM.Version)."
return 0
}
$agent = Read-CloudBackupAgent $installation
$release = Get-EmberkomCloudBackupRelease -Build $agent.Build -Releases $releases
$target = [version]$release.Version $target = [version]$release.Version
if ($agent.Version -ge $target) { if ($agent.Version -ge $target) {
Write-Host "No update needed. Installed $($agent.Version), published $($agent.Build) build $target." Write-Host "No update needed. Installed $($agent.Version), published $($agent.Build) build $target."
@@ -87,7 +99,7 @@ function Invoke-CloudBackupUpdate {
Write-Host "Downloading the published $($agent.Build) update from $($agent.Version) to $target." Write-Host "Downloading the published $($agent.Build) update from $($agent.Version) to $target."
Save-EmberkomCloudBackupInstaller -Release $release -Path $installer Save-EmberkomCloudBackupInstaller -Release $release -Path $installer
# A scheduled plan or the agent's own updater could have started during download. # A scheduled plan or the agent's own updater could have started during download.
$current = Get-CloudBackupAgent $current = Read-CloudBackupAgent (Get-CloudBackupInstallation)
if (!$current -or $current.Build -ne $agent.Build) { throw 'The installed agent changed during download.' } if (!$current -or $current.Build -ne $agent.Build) { throw 'The installed agent changed during download.' }
if ($current.Version -ge $target) { Write-Host "Already updated to $($current.Version)."; return 0 } if ($current.Version -ge $target) { Write-Host "Already updated to $($current.Version)."; return 0 }
if (!(Test-CloudBackupUpdateWindow $Window)) { Write-Host 'Deferred. The update window ended during download.'; return 0 } if (!(Test-CloudBackupUpdateWindow $Window)) { Write-Host 'Deferred. The update window ended during download.'; return 0 }
@@ -95,7 +107,7 @@ function Invoke-CloudBackupUpdate {
$setup = Start-Process -FilePath $installer -ArgumentList '/S' -WindowStyle Hidden -Wait -PassThru -ErrorAction Stop $setup = Start-Process -FilePath $installer -ArgumentList '/S' -WindowStyle Hidden -Wait -PassThru -ErrorAction Stop
if ($setup.ExitCode -eq 3010) { Write-Host 'MSP360 setup requires a restart to finish. Run the check again after restart.'; return 3010 } if ($setup.ExitCode -eq 3010) { Write-Host 'MSP360 setup requires a restart to finish. Run the check again after restart.'; return 3010 }
if ($setup.ExitCode -ne 0) { throw "MSP360 setup returned exit code $($setup.ExitCode)." } if ($setup.ExitCode -ne 0) { throw "MSP360 setup returned exit code $($setup.ExitCode)." }
$updated = Get-CloudBackupAgent $updated = Read-CloudBackupAgent (Get-CloudBackupInstallation)
if (!$updated -or $updated.Build -ne $agent.Build -or $updated.Version -lt $target) { if (!$updated -or $updated.Build -ne $agent.Build -or $updated.Version -lt $target) {
throw "Setup exited successfully but the installed $($agent.Build) agent could not be verified at $target or newer." throw "Setup exited successfully but the installed $($agent.Build) agent could not be verified at $target or newer."
} }
+3 -1
View File
@@ -4,7 +4,9 @@ Publish the repository changes to the live `master` branch, then upload [the RMM
For endpoints that might receive a delayed RMM job, configure the optional **string** variable `MSP360UpdateWindow`. For example, `12:00-13:00` admits installation starts from noon until just before 1 p.m. in the endpoint's local time. Overnight windows such as `23:30-00:30` also work. With an omitted or blank value, Syncro controls the timing. Allow enough time after the last permitted start for setup to finish. For endpoints that might receive a delayed RMM job, configure the optional **string** variable `MSP360UpdateWindow`. For example, `12:00-13:00` admits installation starts from noon until just before 1 p.m. in the endpoint's local time. Overnight windows such as `23:30-00:30` also work. With an omitted or blank value, Syncro controls the timing. Allow enough time after the last permitted start for setup to finish.
The updater compares the installed version with the published Windows or VM package. Before downloading and again immediately before launching setup, it checks 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) On each daily run, the updater reads the installed executable's file version and fetches the small release file once. If the installed version is at least as new as both published builds, it immediately reports **No update needed** and returns success (exit code 0). If either build might be newer, it asks the agent for its edition and compares the matching build.
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)
**Syncro output and exit codes** **Syncro output and exit codes**
+1
View File
@@ -1,3 +1,4 @@
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
. $([Scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/Tools.ps1'))) . $([Scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/Tools.ps1')))
# Set the edition of the ECB agent. # Set the edition of the ECB agent.
+29 -9
View File
@@ -31,10 +31,10 @@ try {
} }
} }
# Substitute only discovery of the installed product in the downloaded core. # Substitute only discovery of the installed product in the downloaded core.
$discovery = Find-TestFunction $core 'Get-CloudBackupAgent' $discovery = Find-TestFunction $core 'Get-CloudBackupInstallation'
$coreSource = $core.Extent.Text.Replace($discovery.Extent.Text, 'function Get-CloudBackupAgent { Get-FixtureAgent }') $coreSource = $core.Extent.Text.Replace($discovery.Extent.Text, 'function Get-CloudBackupInstallation { Get-FixtureInstallation }')
$toolsAst = Read-TestAst (Join-Path $repo 'Tools.ps1') $toolsAst = Read-TestAst (Join-Path $repo 'Tools.ps1')
$shimTools = (@('Run-Script','Get-EmberkomCloudBackupRelease','Save-EmberkomCloudBackupInstaller') | $shimTools = (@('Run-Script','Get-EmberkomCloudBackupReleases','Get-EmberkomCloudBackupRelease','Save-EmberkomCloudBackupInstaller') |
ForEach-Object { (Find-TestFunction $toolsAst $_).Extent.Text }) -join [Environment]::NewLine ForEach-Object { (Find-TestFunction $toolsAst $_).Extent.Text }) -join [Environment]::NewLine
$fixtureBytes = [byte[]](1,2,3) $fixtureBytes = [byte[]](1,2,3)
$sha = [Security.Cryptography.SHA256]::Create() $sha = [Security.Cryptography.SHA256]::Create()
@@ -60,7 +60,10 @@ try {
$client | Add-Member ScriptMethod DownloadString { param($url) $client | Add-Member ScriptMethod DownloadString { param($url)
if ($url -like '*/Tools.ps1') { return $shimTools } if ($url -like '*/Tools.ps1') { return $shimTools }
if ($url -like '*/Update-EmberkomCloudBackup.ps1') { return $coreSource } if ($url -like '*/Update-EmberkomCloudBackup.ps1') { return $coreSource }
if ($url -like '*/resources/EmberkomCloudBackup.json') { return ($manifest | ConvertTo-Json) } if ($url -like '*/resources/EmberkomCloudBackup.json') {
$record.ManifestReads++
return ($manifest | ConvertTo-Json)
}
throw "Unexpected URL" throw "Unexpected URL"
} }
$client | Add-Member ScriptMethod DownloadFile { param($url,$path) $client | Add-Member ScriptMethod DownloadFile { param($url,$path)
@@ -74,13 +77,21 @@ try {
$client | Add-Member ScriptMethod Dispose {} $client | Add-Member ScriptMethod Dispose {}
return $client return $client
} }
function Get-FixtureAgent { function Get-FixtureInstallation {
if ($scenario -eq 'absent') { return } if ($scenario -eq 'absent') { return }
$v = '8.9.1.177' $v = '8.9.1.177'
if ($scenario -eq 'current' -or ($record.Installs -and $scenario -ne 'unverified')) { $v = '8.10.1.177' } if ($scenario -in @('current','only VM newer','VM needs update','incomplete Windows')) { $v = '8.10.1.177' }
if ($record.Installs -and $scenario -ne 'unverified') { $v = $manifest.$kind.Version }
if ($scenario -eq 'newer') { $v = '8.11.0.1' } if ($scenario -eq 'newer') { $v = '8.11.0.1' }
if ($scenario -eq 'already updated' -and $record.Downloads) { $v = '8.10.1.177' } if ($scenario -eq 'already updated' -and $record.Downloads) { $v = '8.10.1.177' }
[pscustomobject]@{ Version=[version]$v; Build=$kind; Service='Fixture Service' } [pscustomobject]@{ Version=[version]$v; Cli='Get-FixtureCli' }
}
function Get-FixtureCli($Command) {
if ($Command -ne 'ver') { throw 'Unexpected agent command' }
$record.CliCalls++
$global:LASTEXITCODE = 0
if ($kind -eq 'VM') { 'Backup Agent Edition: VMware' } else { 'Backup Agent Edition: DesktopIBB' }
'Service name: Fixture Service'
} }
function Get-Date { function Get-Date {
if ($scenario -eq 'window ended' -and $record.Downloads) { return [datetime]'2026-01-01T13:00' } if ($scenario -eq 'window ended' -and $record.Downloads) { return [datetime]'2026-01-01T13:00' }
@@ -88,6 +99,7 @@ try {
} }
function Get-CimInstance { function Get-CimInstance {
param($ClassName,$Filter,$Property,$ErrorAction) param($ClassName,$Filter,$Property,$ErrorAction)
$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 -eq 'late plan')) { [pscustomobject]@{ ProcessId=42 } }
@@ -110,6 +122,7 @@ try {
} }
foreach ($case in @( foreach ($case in @(
@('absent',0,0,0), @('current',0,0,0), @('newer',0,0,0), @('absent',0,0,0), @('current',0,0,0), @('newer',0,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),
@('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),
@@ -117,17 +130,24 @@ try {
@('service down',1,1,1), @('reboot',3010,1,1) @('service down',1,1,1), @('reboot',3010,1,1)
)) { )) {
$scenario,$expected,$expectedDownloads,$expectedInstalls = $case $scenario,$expected,$expectedDownloads,$expectedInstalls = $case
$kind = if ($scenario -eq 'VM') { 'VM' } else { 'Windows' } $kind = if ($scenario -in @('VM','VM needs update')) { 'VM' } else { 'Windows' }
$manifest.VM.Version = if ($scenario -in @('only VM newer','VM needs update')) { '8.11.1.177' } else { '8.10.1.177' }
$manifest.Windows.SHA256 = if ($scenario -eq 'incomplete Windows') { '' } else { $hash }
$MSP360UpdateWindow = switch ($scenario) { $MSP360UpdateWindow = switch ($scenario) {
'bad window' { '25:00-26:00' } 'bad window' { '25:00-26:00' }
'window ended' { '12:00-13:00' } 'window ended' { '12:00-13:00' }
default { '' } default { '' }
} }
$record = @{ Downloads=0; Installs=0; Staged=$null } $record = @{ Downloads=0; Installs=0; Staged=$null; ManifestReads=0; CliCalls=0; PlanChecks=0 }
$messages = & (Join-Path $repo 'rmm/Update-EmberkomCloudBackup.ps1') *>&1 $messages = & (Join-Path $repo 'rmm/Update-EmberkomCloudBackup.ps1') *>&1
if ($LASTEXITCODE -ne $expected -or $record.Downloads -ne $expectedDownloads -or $record.Installs -ne $expectedInstalls) { if ($LASTEXITCODE -ne $expected -or $record.Downloads -ne $expectedDownloads -or $record.Installs -ne $expectedInstalls) {
throw "$scenario failed. Exit=$LASTEXITCODE Downloads=$($record.Downloads) Installs=$($record.Installs). $messages" throw "$scenario failed. Exit=$LASTEXITCODE Downloads=$($record.Downloads) Installs=$($record.Installs). $messages"
} }
if ($scenario -in @('current','newer','only VM newer')) {
$expectedCliCalls = if ($scenario -eq 'only VM newer') { 1 } else { 0 }
if ($record.ManifestReads -ne 1 -or $record.CliCalls -ne $expectedCliCalls -or $record.PlanChecks -ne 0 -or
($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" }
Write-Host "PASS $scenario" Write-Host "PASS $scenario"
} }