From bfdf8f5eae2f59cb98eb0f7427b75735876ec972 Mon Sep 17 00:00:00 2001 From: ek-dyoder Date: Tue, 17 Nov 2026 00:36:59 -0500 Subject: [PATCH] made the backup update script fail faster --- Tools.ps1 | 27 +++++++++++---- Update-EmberkomCloudBackup.ps1 | 32 ++++++++++++------ docs/EmberkomCloudBackup-Updates.md | 4 ++- rmm/Install-EmberkomCloudBackup.ps1 | 3 +- tests/Update-EmberkomCloudBackup.Tests.ps1 | 38 +++++++++++++++++----- 5 files changed, 76 insertions(+), 28 deletions(-) diff --git a/Tools.ps1 b/Tools.ps1 index 4e5edd7..8be3a9f 100644 --- a/Tools.ps1 +++ b/Tools.ps1 @@ -886,19 +886,32 @@ Function Disable-Log { } # Published packages for both Install- and Update-EmberkomCloudBackup. -Function Get-EmberkomCloudBackupRelease { - param([Parameter(Mandatory=$true)][ValidateSet('Windows','VM')][string]$Build) +Function Get-EmberkomCloudBackupReleases { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $client = New-Object Net.WebClient try { $json = $client.DownloadString('https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/resources/EmberkomCloudBackup.json') } finally { $client.Dispose() } - $release = ($json | ConvertFrom-Json -ErrorAction Stop).$Build - if ($release.Version -notmatch '^\d+\.\d+\.\d+\.\d+$' -or $release.Url -notmatch '^https://' -or - $release.SHA256 -notmatch '^[a-f0-9]{64}$') { - throw "Incomplete Emberkom Cloud Backup release for $Build." + $manifest = $json | ConvertFrom-Json -ErrorAction Stop + $releases = @{} + foreach ($build in 'Windows','VM') { + $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 { diff --git a/Update-EmberkomCloudBackup.ps1 b/Update-EmberkomCloudBackup.ps1 index 63ee043..df7641e 100644 --- a/Update-EmberkomCloudBackup.ps1 +++ b/Update-EmberkomCloudBackup.ps1 @@ -1,7 +1,7 @@ #Requires -Version 5.1 # 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. # 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' } @@ -15,7 +15,10 @@ function Get-CloudBackupAgent { 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.' } $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 { if ($product) { $product.Dispose() } if ($marker) { $marker.Dispose() } @@ -25,8 +28,9 @@ function Get-CloudBackupAgent { } function Read-CloudBackupAgent { - param([string]$Cli) - $details = & $Cli ver + param($Installation) + if (!$Installation) { return } + $details = & $Installation.Cli ver if ($LASTEXITCODE -ne 0) { throw "MSP360 could not report the installed agent details (exit $LASTEXITCODE)." } $edition = $service = $null foreach ($line in $details) { @@ -43,7 +47,7 @@ function Read-CloudBackupAgent { } if (!$service) { throw 'MSP360 did not report its backup service name.' } [pscustomobject]@{ - Version = [version][Diagnostics.FileVersionInfo]::GetVersionInfo($Cli).FileVersion + Version = $Installation.Version Build = $build Service = $service } @@ -72,9 +76,17 @@ function Test-CloudBackupPlanRunning { function Invoke-CloudBackupUpdate { param([string]$Window) if (!(Test-CloudBackupUpdateWindow $Window)) { Write-Host "Deferred. Outside the local update window $Window."; return 0 } - $agent = Get-CloudBackupAgent - if (!$agent) { Write-Host 'Skipped. MSP360 Managed Backup is not installed.'; return 0 } - $release = Get-EmberkomCloudBackupRelease -Build $agent.Build + $installation = Get-CloudBackupInstallation + if (!$installation) { Write-Host 'Skipped. MSP360 Managed Backup is not installed.'; return 0 } + $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 if ($agent.Version -ge $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." Save-EmberkomCloudBackupInstaller -Release $release -Path $installer # 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.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 } @@ -95,7 +107,7 @@ function Invoke-CloudBackupUpdate { $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 -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) { throw "Setup exited successfully but the installed $($agent.Build) agent could not be verified at $target or newer." } diff --git a/docs/EmberkomCloudBackup-Updates.md b/docs/EmberkomCloudBackup-Updates.md index d21660d..1e3dc53 100644 --- a/docs/EmberkomCloudBackup-Updates.md +++ b/docs/EmberkomCloudBackup-Updates.md @@ -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. -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** diff --git a/rmm/Install-EmberkomCloudBackup.ps1 b/rmm/Install-EmberkomCloudBackup.ps1 index 882cd5e..d6638a2 100644 --- a/rmm/Install-EmberkomCloudBackup.ps1 +++ b/rmm/Install-EmberkomCloudBackup.ps1 @@ -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'))) # Set the edition of the ECB agent. @@ -9,4 +10,4 @@ $MSP360Username = '{[MSP360_Username]}' $MSP360Password = '{[MSP360_Password]}' # Run the installation script -Run-Script -LivePSScript Install-EmberkomCloudBackup \ No newline at end of file +Run-Script -LivePSScript Install-EmberkomCloudBackup diff --git a/tests/Update-EmberkomCloudBackup.Tests.ps1 b/tests/Update-EmberkomCloudBackup.Tests.ps1 index 562b2fb..2af388c 100644 --- a/tests/Update-EmberkomCloudBackup.Tests.ps1 +++ b/tests/Update-EmberkomCloudBackup.Tests.ps1 @@ -31,10 +31,10 @@ try { } } # Substitute only discovery of the installed product in the downloaded core. - $discovery = Find-TestFunction $core 'Get-CloudBackupAgent' - $coreSource = $core.Extent.Text.Replace($discovery.Extent.Text, 'function Get-CloudBackupAgent { Get-FixtureAgent }') + $discovery = Find-TestFunction $core 'Get-CloudBackupInstallation' + $coreSource = $core.Extent.Text.Replace($discovery.Extent.Text, 'function Get-CloudBackupInstallation { Get-FixtureInstallation }') $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 $fixtureBytes = [byte[]](1,2,3) $sha = [Security.Cryptography.SHA256]::Create() @@ -60,7 +60,10 @@ try { $client | Add-Member ScriptMethod DownloadString { param($url) if ($url -like '*/Tools.ps1') { return $shimTools } 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" } $client | Add-Member ScriptMethod DownloadFile { param($url,$path) @@ -74,13 +77,21 @@ try { $client | Add-Member ScriptMethod Dispose {} return $client } - function Get-FixtureAgent { + function Get-FixtureInstallation { if ($scenario -eq 'absent') { return } $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 '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 { if ($scenario -eq 'window ended' -and $record.Downloads) { return [datetime]'2026-01-01T13:00' } @@ -88,6 +99,7 @@ try { } function Get-CimInstance { param($ClassName,$Filter,$Property,$ErrorAction) + $record.PlanChecks++ 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 'busy' -or ($record.Downloads -and $scenario -eq 'late plan')) { [pscustomobject]@{ ProcessId=42 } } @@ -110,6 +122,7 @@ try { } foreach ($case in @( @('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), @('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), @@ -117,17 +130,24 @@ try { @('service down',1,1,1), @('reboot',3010,1,1) )) { $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) { 'bad window' { '25:00-26:00' } 'window ended' { '12:00-13:00' } 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 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" } + 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" } Write-Host "PASS $scenario" }