From ce170f06851b7f4e27f7cf2c0b715b23aaaa85b2 Mon Sep 17 00:00:00 2001 From: Kevin Woley Date: Thu, 20 Feb 2025 15:27:39 -0800 Subject: [PATCH] bugfix: error checking was broken by release 1.7.1 (https://github.com/kmwoley/restic-windows-backup/commit/c8776b42c0a9080ba9ffb6607e9dec238ac42c9e) --- backup.ps1 | 21 +++++++++++++++------ install.ps1 | 32 ++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/backup.ps1 b/backup.ps1 index 6ab9e9f..afac82b 100755 --- a/backup.ps1 +++ b/backup.ps1 @@ -12,6 +12,9 @@ $ConfigScript = Join-Path $PSScriptRoot "config.ps1" # =========== end configuration =========== # +# make LASTEXITCODE global to enable error checking for Invoke-Expression commands +$global:LASTEXITCODE=0 + # globals for state storage $Script:ResticStateRepositoryInitialized = $null $Script:ResticStateLastMaintenance = $null @@ -94,9 +97,15 @@ function Invoke-Unlock { Param($SuccessLog, $ErrorLog) $locks = Invoke-Expression "$Script:ResticExe list locks --no-lock -q 3>&1 2>> $ErrorLog" + if($LASTEXITCODE) { + "[[Unlock]] Warning: unable to list locks." | Tee-Object -Append $ErrorLog + } if($locks.Length -gt 0) { # unlock the repository (assumes this machine is the only one that will ever use it) Invoke-Expression "$Script:ResticExe unlock 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog" + if($LASTEXITCODE) { + "[[Unlock]] Error - unable to unlock repository." | Tee-Object -Append $ErrorLog + } "[[Unlock]] Repository was locked. Unlocking." | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog Start-Sleep 120 } @@ -142,7 +151,7 @@ function Invoke-Maintenance { # forget snapshots based upon the retention policy "[[Maintenance]] Start forgetting..." | Out-File -Append $SuccessLog Invoke-Expression "$Script:ResticExe forget $SnapshotRetentionPolicy 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog" - if(-not $?) { + if($LASTEXITCODE) { "[[Maintenance]] Forget operation completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog $maintenance_success = $false } @@ -151,7 +160,7 @@ function Invoke-Maintenance { # `forget` only prunes when it detects removed snapshots upon invocation, not previously removed "[[Maintenance]] Start pruning..." | Out-File -Append $SuccessLog Invoke-Expression "$Script:ResticExe prune $SnapshotPrunePolicy 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog" - if(-not $?) { + if($LASTEXITCODE) { "[[Maintenance]] Prune operation completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog $maintenance_success = $false } @@ -178,8 +187,8 @@ function Invoke-Maintenance { } Invoke-Expression "$Script:ResticExe check $data_check 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog" - if(-not $?) { - "[[Maintenance]] Check completed with errors" | Tee-Object -Append $ErrorLog | Tee-Object -Append $SuccessLog | Write-Host + if($LASTEXITCODE) { + "[[Maintenance]] Data check completed with errors" | Tee-Object -Append $ErrorLog | Tee-Object -Append $SuccessLog | Write-Host $maintenance_success = $false } @@ -189,7 +198,7 @@ function Invoke-Maintenance { # check for updated restic version "[[Maintenance]] Checking for new version of restic..." | Out-File -Append $SuccessLog Invoke-Expression "$Script:ResticExe self-update 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog" - if(-not $?) { + if($LASTEXITCODE) { "[[Maintenance]] Self-update of restic.exe completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog $maintenance_success = $false } @@ -298,7 +307,7 @@ function Invoke-Backup { else { # Launch Restic Invoke-Expression "$Script:ResticExe backup $folder_list $vss_option --tag $tag --exclude-file=$WindowsExcludeFile --exclude-file=$LocalExcludeFile $AdditionalBackupParameters 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog" - if(-not $?) { + if($LASTEXITCODE) { "[[Backup]] Completed with errors" | Tee-Object -Append $ErrorLog | Tee-Object -Append $SuccessLog | Write-Host $return_value = $false } diff --git a/install.ps1 b/install.ps1 index 2ac8f89..6d5fe3d 100644 --- a/install.ps1 +++ b/install.ps1 @@ -20,22 +20,31 @@ $ConfigScript = Join-Path $PSScriptRoot "config.ps1" $ResticExe = Join-Path $InstallPath $ExeName $LogPath = Join-Path $InstallPath "logs" +# make LASTEXITCODE global to enable error checking for Invoke-Expression commands +$global:LASTEXITCODE=0 + # =========== end configuration =========== # # download restic if(-not (Test-Path $ResticExe)) { $url = $null if([Environment]::Is64BitOperatingSystem){ - $url = "https://github.com/restic/restic/releases/download/v0.17.3/restic_0.17.3_windows_amd64.zip" + $url = "fixmetestingnotrealhttps://github.com/restic/restic/releases/download/v0.17.3/restic_0.17.3_windows_amd64.zip" } else { $url = "https://github.com/restic/restic/releases/download/v0.17.3/restic_0.17.3_windows_386.zip" } - $output = Join-Path $InstallPath "restic.zip" - Invoke-WebRequest -Uri $url -OutFile $output - Expand-Archive -LiteralPath $output $InstallPath - Remove-Item $output - Get-ChildItem *.exe | Rename-Item -NewName $ExeName + try { + $output = Join-Path $InstallPath "restic.zip" + Invoke-WebRequest -Uri $url -OutFile $output + Expand-Archive -LiteralPath $output $InstallPath + Remove-Item $output + Get-ChildItem *.exe | Rename-Item -NewName $ExeName + } + catch { + Write-Error "[[Install]] restic.exe download failed. Check errors and resolve: $_" + exit 1 + } } # Apply global paramters to $ResticExe, after the $ResticExe has been downloaded/confirmed to exist @@ -47,6 +56,9 @@ if(-not [String]::IsNullOrEmpty($GlobalParameters)) { # This is enabled by default unless configuration disables self-update if ([String]::IsNullOrEmpty($SelfUpdateEnabled) -or ($SelfUpdateEnabled -eq $true)) { Invoke-Expression "$ResticExe self-update" + if($LASTEXITCODE) { + Write-Warning "[[Update]] Restic self-update failed. Check errors and resolve: $_" + } } # Create log directory if it doesn't exit @@ -62,11 +74,11 @@ if(-not (Test-Path $LocalExcludeFile)) { # Initialize the restic repository Invoke-Expression "$ResticExe --verbose init" -if($?) { - Write-Output "[[Init]] Repository successfully initialized." +if($LASTEXITCODE) { + Write-Warning "[[Init]] Repository initialization failed. Check errors and resolve: $_" } else { - Write-Warning "[[Init]] Repository initialization failed. Check errors and resolve." + Write-Output "[[Init]] Repository successfully initialized." } # Scheduled Windows Task Scheduler to run the backup @@ -82,7 +94,7 @@ if($null -eq $backup_task) { Write-Output "[[Scheduler]] Backup task scheduled." } catch { - Write-Warning "[[Scheduler]] Scheduling failed." + Write-Error "[[Scheduler]] Setting up backup task schedule failed: $_" } } else {