bugfix: error checking was broken by release 1.7.1 (c8776b42c0)

This commit is contained in:
Kevin Woley
2025-02-20 15:27:39 -08:00
parent efd7c78f59
commit ce170f0685
2 changed files with 37 additions and 16 deletions

View File

@@ -12,6 +12,9 @@ $ConfigScript = Join-Path $PSScriptRoot "config.ps1"
# =========== end configuration =========== # # =========== end configuration =========== #
# make LASTEXITCODE global to enable error checking for Invoke-Expression commands
$global:LASTEXITCODE=0
# globals for state storage # globals for state storage
$Script:ResticStateRepositoryInitialized = $null $Script:ResticStateRepositoryInitialized = $null
$Script:ResticStateLastMaintenance = $null $Script:ResticStateLastMaintenance = $null
@@ -94,9 +97,15 @@ function Invoke-Unlock {
Param($SuccessLog, $ErrorLog) Param($SuccessLog, $ErrorLog)
$locks = Invoke-Expression "$Script:ResticExe list locks --no-lock -q 3>&1 2>> $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) { if($locks.Length -gt 0) {
# unlock the repository (assumes this machine is the only one that will ever use it) # 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" 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 "[[Unlock]] Repository was locked. Unlocking." | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog
Start-Sleep 120 Start-Sleep 120
} }
@@ -142,7 +151,7 @@ function Invoke-Maintenance {
# forget snapshots based upon the retention policy # forget snapshots based upon the retention policy
"[[Maintenance]] Start forgetting..." | Out-File -Append $SuccessLog "[[Maintenance]] Start forgetting..." | Out-File -Append $SuccessLog
Invoke-Expression "$Script:ResticExe forget $SnapshotRetentionPolicy 3>&1 2>> $ErrorLog | 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]] Forget operation completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog
$maintenance_success = $false $maintenance_success = $false
} }
@@ -151,7 +160,7 @@ function Invoke-Maintenance {
# `forget` only prunes when it detects removed snapshots upon invocation, not previously removed # `forget` only prunes when it detects removed snapshots upon invocation, not previously removed
"[[Maintenance]] Start pruning..." | Out-File -Append $SuccessLog "[[Maintenance]] Start pruning..." | Out-File -Append $SuccessLog
Invoke-Expression "$Script:ResticExe prune $SnapshotPrunePolicy 3>&1 2>> $ErrorLog | 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]] Prune operation completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog
$maintenance_success = $false $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" Invoke-Expression "$Script:ResticExe check $data_check 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog"
if(-not $?) { if($LASTEXITCODE) {
"[[Maintenance]] Check completed with errors" | Tee-Object -Append $ErrorLog | Tee-Object -Append $SuccessLog | Write-Host "[[Maintenance]] Data check completed with errors" | Tee-Object -Append $ErrorLog | Tee-Object -Append $SuccessLog | Write-Host
$maintenance_success = $false $maintenance_success = $false
} }
@@ -189,7 +198,7 @@ function Invoke-Maintenance {
# check for updated restic version # check for updated restic version
"[[Maintenance]] Checking for new version of restic..." | Out-File -Append $SuccessLog "[[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" 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]] Self-update of restic.exe completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog
$maintenance_success = $false $maintenance_success = $false
} }
@@ -298,7 +307,7 @@ function Invoke-Backup {
else { else {
# Launch Restic # 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" 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 "[[Backup]] Completed with errors" | Tee-Object -Append $ErrorLog | Tee-Object -Append $SuccessLog | Write-Host
$return_value = $false $return_value = $false
} }

View File

@@ -20,22 +20,31 @@ $ConfigScript = Join-Path $PSScriptRoot "config.ps1"
$ResticExe = Join-Path $InstallPath $ExeName $ResticExe = Join-Path $InstallPath $ExeName
$LogPath = Join-Path $InstallPath "logs" $LogPath = Join-Path $InstallPath "logs"
# make LASTEXITCODE global to enable error checking for Invoke-Expression commands
$global:LASTEXITCODE=0
# =========== end configuration =========== # # =========== end configuration =========== #
# download restic # download restic
if(-not (Test-Path $ResticExe)) { if(-not (Test-Path $ResticExe)) {
$url = $null $url = $null
if([Environment]::Is64BitOperatingSystem){ 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 { else {
$url = "https://github.com/restic/restic/releases/download/v0.17.3/restic_0.17.3_windows_386.zip" $url = "https://github.com/restic/restic/releases/download/v0.17.3/restic_0.17.3_windows_386.zip"
} }
$output = Join-Path $InstallPath "restic.zip" try {
Invoke-WebRequest -Uri $url -OutFile $output $output = Join-Path $InstallPath "restic.zip"
Expand-Archive -LiteralPath $output $InstallPath Invoke-WebRequest -Uri $url -OutFile $output
Remove-Item $output Expand-Archive -LiteralPath $output $InstallPath
Get-ChildItem *.exe | Rename-Item -NewName $ExeName 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 # 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 # This is enabled by default unless configuration disables self-update
if ([String]::IsNullOrEmpty($SelfUpdateEnabled) -or ($SelfUpdateEnabled -eq $true)) { if ([String]::IsNullOrEmpty($SelfUpdateEnabled) -or ($SelfUpdateEnabled -eq $true)) {
Invoke-Expression "$ResticExe self-update" 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 # Create log directory if it doesn't exit
@@ -62,11 +74,11 @@ if(-not (Test-Path $LocalExcludeFile)) {
# Initialize the restic repository # Initialize the restic repository
Invoke-Expression "$ResticExe --verbose init" Invoke-Expression "$ResticExe --verbose init"
if($?) { if($LASTEXITCODE) {
Write-Output "[[Init]] Repository successfully initialized." Write-Warning "[[Init]] Repository initialization failed. Check errors and resolve: $_"
} }
else { 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 # Scheduled Windows Task Scheduler to run the backup
@@ -82,7 +94,7 @@ if($null -eq $backup_task) {
Write-Output "[[Scheduler]] Backup task scheduled." Write-Output "[[Scheduler]] Backup task scheduled."
} }
catch { catch {
Write-Warning "[[Scheduler]] Scheduling failed." Write-Error "[[Scheduler]] Setting up backup task schedule failed: $_"
} }
} }
else { else {