replaced "&" execution with Invoke-Expression, enabled refactoring to set $GlobalParameters in one place

Removed $ResticExe, $StateFile, and $LogPath from config.ps1
This commit is contained in:
Kevin Woley
2025-01-26 12:53:12 -08:00
parent 33163ecb60
commit c8776b42c0
2 changed files with 62 additions and 57 deletions
+19 -11
View File
@@ -4,10 +4,10 @@
# =========== start configuration =========== # # =========== start configuration =========== #
# set restic configuration parmeters (destination, passwords, etc.) # load restic configuration parmeters (destination, passwords, etc.)
$SecretsScript = Join-Path $PSScriptRoot "secrets.ps1" $SecretsScript = Join-Path $PSScriptRoot "secrets.ps1"
# backup configuration variables # load backup configuration variables
$ConfigScript = Join-Path $PSScriptRoot "config.ps1" $ConfigScript = Join-Path $PSScriptRoot "config.ps1"
# =========== end configuration =========== # # =========== end configuration =========== #
@@ -79,10 +79,10 @@ function Set-BackupState {
function Invoke-Unlock { function Invoke-Unlock {
Param($SuccessLog, $ErrorLog) Param($SuccessLog, $ErrorLog)
$locks = & $ResticExe $AdditionalParameters list locks --no-lock -q 3>&1 2>> $ErrorLog $locks = Invoke-Expression "$ResticExe list locks --no-lock -q 3>&1 2>> $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)
& $ResticExe $AdditionalParameters unlock 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog Invoke-Expression "$ResticExe unlock 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog"
"[[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
} }
@@ -127,7 +127,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
& $ResticExe $AdditionalParameters forget $SnapshotRetentionPolicy 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog Invoke-Expression "$ResticExe forget $SnapshotRetentionPolicy 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog"
if(-not $?) { if(-not $?) {
"[[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
@@ -136,7 +136,7 @@ function Invoke-Maintenance {
# prune (remove) data from the backup step. Running this separate from `forget` because # prune (remove) data from the backup step. Running this separate from `forget` because
# `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
& $ResticExe $AdditionalParameters prune $SnapshotPrunePolicy 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog Invoke-Expression "$ResticExe prune $SnapshotPrunePolicy 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog"
if(-not $?) { if(-not $?) {
"[[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
@@ -163,7 +163,7 @@ function Invoke-Maintenance {
$Script:ResticStateLastDeepMaintenance = Get-Date $Script:ResticStateLastDeepMaintenance = Get-Date
} }
& $ResticExe $AdditionalParameters check @data_check 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog Invoke-Expression "$ResticExe check @data_check 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog"
if(-not $?) { if(-not $?) {
"[[Maintenance]] Check completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog "[[Maintenance]] Check completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog
$maintenance_success = $false $maintenance_success = $false
@@ -172,7 +172,7 @@ function Invoke-Maintenance {
if($AllowResticSelfUpdate -eq $true) { if($AllowResticSelfUpdate -eq $true) {
# 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
& $ResticExe $SelfUpdateParameters self-update 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog Invoke-Expression "$ResticExe $SelfUpdateParameters self-update 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog"
if(-not $?) { if(-not $?) {
"[[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
@@ -239,7 +239,7 @@ function Invoke-Backup {
$folder_list = New-Object System.Collections.Generic.List[System.Object] $folder_list = New-Object System.Collections.Generic.List[System.Object]
if ($item.Value.Count -eq 0) { if ($item.Value.Count -eq 0) {
# backup everything in the root if no folders are provided # backup everything in the root if no folders are provided
$folder_list.Add($root_path) $folder_list.Add("`"$root_path`"")
} }
else { else {
# Build the list of folders from settings # Build the list of folders from settings
@@ -248,7 +248,7 @@ function Invoke-Backup {
if(Test-Path ($p -replace '"')) { if(Test-Path ($p -replace '"')) {
# add the folder if it exists # add the folder if it exists
$folder_list.Add($p) $folder_list.Add("`"$p`"")
} }
else { else {
# if the folder doesn't exist, log a warning/error # if the folder doesn't exist, log a warning/error
@@ -280,7 +280,7 @@ function Invoke-Backup {
} }
else { else {
# Launch Restic # Launch Restic
& $ResticExe backup $folder_list $vss_option --tag $tag --exclude-file=$WindowsExcludeFile --exclude-file=$LocalExcludeFile $AdditionalParameters $AdditionalBackupParameters 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog Invoke-Expression "$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(-not $?) {
"[[Backup]] Completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog "[[Backup]] Completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog
$return_value = $false $return_value = $false
@@ -480,6 +480,14 @@ function Invoke-Main {
# initialize config # initialize config
. $ConfigScript . $ConfigScript
# apply global configuration
$global:ResticExe = Join-Path $InstallPath $ExeName
if(-not [String]::IsNullOrEmpty($GlobalParameters)) {
$ResticExe = "$ResticExe $GlobalParameters"
}
$global:StateFile = Join-Path $InstallPath "state.xml"
$global:LogPath = Join-Path $InstallPath "logs"
Get-BackupState Get-BackupState
if(!(Test-Path $LogPath)) { if(!(Test-Path $LogPath)) {
+3 -6
View File
@@ -1,16 +1,13 @@
# general configuration # general configuration
$ExeName = "restic.exe"
$InstallPath = "C:\restic" $InstallPath = "C:\restic"
$ResticExe = Join-Path $InstallPath $ExeName $ExeName = "restic.exe"
$StateFile = Join-Path $InstallPath "state.xml" $GlobalParameters = @()
$LogPath = Join-Path $InstallPath "logs"
$LogRetentionDays = 30 $LogRetentionDays = 30
$InternetTestAttempts = 10 $InternetTestAttempts = 10
$GlobalRetryAttempts = 4 $GlobalRetryAttempts = 4
$AdditionalParameters = @()
# email configuration # email configuration
$SendEmailOnSuccess = $true $SendEmailOnSuccess = $false
$SendEmailOnError = $true $SendEmailOnError = $true
# backup configuration # backup configuration