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

View File

@@ -4,10 +4,10 @@
# =========== start configuration =========== #
# set restic configuration parmeters (destination, passwords, etc.)
# load restic configuration parmeters (destination, passwords, etc.)
$SecretsScript = Join-Path $PSScriptRoot "secrets.ps1"
# backup configuration variables
# load backup configuration variables
$ConfigScript = Join-Path $PSScriptRoot "config.ps1"
# =========== end configuration =========== #
@@ -79,10 +79,10 @@ function Set-BackupState {
function Invoke-Unlock {
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) {
# 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
Start-Sleep 120
}
@@ -127,7 +127,7 @@ function Invoke-Maintenance {
# forget snapshots based upon the retention policy
"[[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 $?) {
"[[Maintenance]] Forget operation completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog
$maintenance_success = $false
@@ -136,7 +136,7 @@ function Invoke-Maintenance {
# 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
"[[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 $?) {
"[[Maintenance]] Prune operation completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog
$maintenance_success = $false
@@ -163,7 +163,7 @@ function Invoke-Maintenance {
$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 $?) {
"[[Maintenance]] Check completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog
$maintenance_success = $false
@@ -172,7 +172,7 @@ function Invoke-Maintenance {
if($AllowResticSelfUpdate -eq $true) {
# check for updated restic version
"[[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 $?) {
"[[Maintenance]] Self-update of restic.exe completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog
$maintenance_success = $false
@@ -239,7 +239,7 @@ function Invoke-Backup {
$folder_list = New-Object System.Collections.Generic.List[System.Object]
if ($item.Value.Count -eq 0) {
# backup everything in the root if no folders are provided
$folder_list.Add($root_path)
$folder_list.Add("`"$root_path`"")
}
else {
# Build the list of folders from settings
@@ -248,7 +248,7 @@ function Invoke-Backup {
if(Test-Path ($p -replace '"')) {
# add the folder if it exists
$folder_list.Add($p)
$folder_list.Add("`"$p`"")
}
else {
# if the folder doesn't exist, log a warning/error
@@ -280,7 +280,7 @@ function Invoke-Backup {
}
else {
# 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 $?) {
"[[Backup]] Completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog
$return_value = $false
@@ -480,6 +480,14 @@ function Invoke-Main {
# initialize config
. $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
if(!(Test-Path $LogPath)) {

View File

@@ -1,16 +1,13 @@
# general configuration
$ExeName = "restic.exe"
$InstallPath = "C:\restic"
$ResticExe = Join-Path $InstallPath $ExeName
$StateFile = Join-Path $InstallPath "state.xml"
$LogPath = Join-Path $InstallPath "logs"
$ExeName = "restic.exe"
$GlobalParameters = @()
$LogRetentionDays = 30
$InternetTestAttempts = 10
$GlobalRetryAttempts = 4
$AdditionalParameters = @()
# email configuration
$SendEmailOnSuccess = $true
$SendEmailOnSuccess = $false
$SendEmailOnError = $true
# backup configuration