Merge branch 'release_1.7.1' into metered-check
This commit is contained in:
+144
-106
@@ -2,12 +2,12 @@
|
||||
# Restic Windows Backup Script
|
||||
#
|
||||
|
||||
# =========== start configuration =========== #
|
||||
# =========== 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 =========== #
|
||||
@@ -19,8 +19,8 @@ $Script:ResticStateLastDeepMaintenance = $null
|
||||
$Script:ResticStateMaintenanceCounter = $null
|
||||
$Script:ResticStateLastBackupSuccessful = $true
|
||||
$Script:ResticStateLastMaintenanceSuccessful = $true
|
||||
|
||||
# Returns all drive letters which exactly match the serial number, drive label, or drive name of
|
||||
|
||||
# Returns all drive letters which exactly match the serial number, drive label, or drive name of
|
||||
# the input parameter. Returns all drives if no input parameter is provided.
|
||||
# inspiration: https://stackoverflow.com/questions/31088930/combine-get-disk-info-and-logicaldisk-info-in-powershell
|
||||
function Get-Drives {
|
||||
@@ -35,16 +35,16 @@ function Get-Drives {
|
||||
$drives = Get-CimAssociatedInstance -ResultClassName Win32_LogicalDisk -InputObject $partition
|
||||
|
||||
foreach($drive in $drives) {
|
||||
|
||||
|
||||
$volume = Get-Volume |
|
||||
Where-Object { $_.DriveLetter -eq $drive.DeviceID.Trim(":") } |
|
||||
Select-Object -First 1
|
||||
|
||||
if(($diskMetadata.SerialNumber.trim() -eq $ID) -or
|
||||
if(($diskMetadata.SerialNumber.trim() -eq $ID) -or
|
||||
($disk.Caption -eq $ID) -or
|
||||
($volume.FileSystemLabel -eq $ID) -or
|
||||
($null -eq $ID)) {
|
||||
|
||||
|
||||
[PSCustomObject] @{
|
||||
DriveLetter = $drive.DeviceID
|
||||
Number = $disk.Index
|
||||
@@ -67,24 +67,24 @@ function Get-Drives {
|
||||
|
||||
# restore backup state from disk
|
||||
function Get-BackupState {
|
||||
if(Test-Path $StateFile) {
|
||||
Import-Clixml $StateFile | ForEach-Object{ Set-Variable -Scope Script $_.Name $_.Value }
|
||||
if(Test-Path $Script:StateFile) {
|
||||
Import-Clixml $Script:StateFile | ForEach-Object{ Set-Variable -Scope Script $_.Name $_.Value }
|
||||
}
|
||||
}
|
||||
function Set-BackupState {
|
||||
Get-Variable ResticState* | Export-Clixml $StateFile
|
||||
Get-Variable ResticState* | Export-Clixml $Script:StateFile
|
||||
}
|
||||
|
||||
# unlock the repository if need be
|
||||
function Invoke-Unlock {
|
||||
Param($SuccessLog, $ErrorLog)
|
||||
|
||||
$locks = & $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($locks.Length -gt 0) {
|
||||
# unlock the repository (assumes this machine is the only one that will ever use it)
|
||||
& $ResticExe unlock 3>&1 2>> $ErrorLog | Out-File -Append $SuccessLog
|
||||
Invoke-Expression "$Script: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
|
||||
Start-Sleep 120
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ function Test-Maintenance {
|
||||
}
|
||||
}
|
||||
else {
|
||||
"[[Maintenance]] Running - no past maintenance history known." | Out-File -Append $SuccessLog
|
||||
"[[Maintenance]] Running - no past maintenance history known." | Out-File -Append $SuccessLog
|
||||
return $true
|
||||
}
|
||||
}
|
||||
@@ -120,14 +120,14 @@ function Test-Maintenance {
|
||||
# run maintenance on the backup set
|
||||
function Invoke-Maintenance {
|
||||
Param($SuccessLog, $ErrorLog)
|
||||
|
||||
"[[Maintenance]] Start $(Get-Date)" | Out-File -Append $SuccessLog
|
||||
|
||||
"[[Maintenance]] Start $(Get-Date)" | Tee-Object -Append $SuccessLog | Write-Host
|
||||
$maintenance_success = $true
|
||||
Start-Sleep 120
|
||||
|
||||
# forget snapshots based upon the retention policy
|
||||
"[[Maintenance]] Start forgetting..." | Out-File -Append $SuccessLog
|
||||
& $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 $?) {
|
||||
"[[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 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 $?) {
|
||||
"[[Maintenance]] Prune operation completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog
|
||||
$maintenance_success = $false
|
||||
@@ -163,22 +163,26 @@ function Invoke-Maintenance {
|
||||
$Script:ResticStateLastDeepMaintenance = Get-Date
|
||||
}
|
||||
|
||||
& $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 $?) {
|
||||
"[[Maintenance]] Check completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog
|
||||
"[[Maintenance]] Check completed with errors" | Tee-Object -Append $ErrorLog | Tee-Object -Append $SuccessLog | Write-Host
|
||||
$maintenance_success = $false
|
||||
}
|
||||
|
||||
# check for updated restic version
|
||||
"[[Maintenance]] Checking for new version of restic..." | Out-File -Append $SuccessLog
|
||||
& $ResticExe 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
|
||||
# Invoke restic self-update to check for a newer version
|
||||
# This is enabled by default unless configuration disables self-update
|
||||
if ([String]::IsNullOrEmpty($SelfUpdateEnabled) -or ($SelfUpdateEnabled -eq $true)) {
|
||||
# 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 $?) {
|
||||
"[[Maintenance]] Self-update of restic.exe completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog
|
||||
$maintenance_success = $false
|
||||
}
|
||||
}
|
||||
|
||||
"[[Maintenance]] End $(Get-Date)" | Out-File -Append $SuccessLog
|
||||
|
||||
"[[Maintenance]] End $(Get-Date)" | Tee-Object -Append $SuccessLog | Write-Host
|
||||
|
||||
if($maintenance_success -eq $true) {
|
||||
$Script:ResticStateLastMaintenance = Get-Date
|
||||
$Script:ResticStateMaintenanceCounter = 0
|
||||
@@ -187,11 +191,11 @@ function Invoke-Maintenance {
|
||||
return $maintenance_success
|
||||
}
|
||||
|
||||
# Run restic backup
|
||||
# Run restic backup
|
||||
function Invoke-Backup {
|
||||
Param($SuccessLog, $ErrorLog)
|
||||
|
||||
"[[Backup]] Start $(Get-Date)" | Out-File -Append $SuccessLog
|
||||
"[[Backup]] Start $(Get-Date)" | Tee-Object -Append $SuccessLog | Write-Host
|
||||
$return_value = $true
|
||||
$starting_location = Get-Location
|
||||
ForEach ($item in $BackupSources.GetEnumerator()) {
|
||||
@@ -215,7 +219,7 @@ function Invoke-Backup {
|
||||
$ignore_error = ($null -ne $IgnoreMissingBackupSources) -and $IgnoreMissingBackupSources
|
||||
$warning_message = "[[Backup]] Warning - backup path $root_path not found."
|
||||
if($ignore_error) {
|
||||
$warning_message | Out-File -Append $SuccessLog
|
||||
$warning_message | Out-File -Append $SuccessLog
|
||||
}
|
||||
else {
|
||||
$warning_message | Tee-Object -Append $SuccessLog | Out-File -Append $ErrorLog
|
||||
@@ -223,30 +227,30 @@ function Invoke-Backup {
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
$root_path = Join-Path $drives[0].DriveLetter ""
|
||||
|
||||
|
||||
# disable VSS / file system snapshot for external drives
|
||||
# TODO: would be best to just test for VSS compatibility on the drive, rather than assume it won't work
|
||||
$vss_option = $null
|
||||
}
|
||||
|
||||
"[[Backup]] Start $(Get-Date) [$tag]" | Out-File -Append $SuccessLog
|
||||
|
||||
|
||||
# build the list of folders to 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
|
||||
ForEach ($path in $item.Value) {
|
||||
$p = '"{0}"' -f ((Join-Path $root_path $path) -replace "\\$")
|
||||
|
||||
$p = '{0}' -f ((Join-Path $root_path $path) -replace "\\$")
|
||||
|
||||
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
|
||||
@@ -263,7 +267,7 @@ function Invoke-Backup {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
if(-not $folder_list) {
|
||||
# there are no folders to backup
|
||||
$ignore_error = ($null -ne $IgnoreMissingBackupSources) -and $IgnoreMissingBackupSources
|
||||
@@ -278,18 +282,18 @@ function Invoke-Backup {
|
||||
}
|
||||
else {
|
||||
# Launch Restic
|
||||
& $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 $?) {
|
||||
"[[Backup]] Completed with errors" | Tee-Object -Append $ErrorLog | Out-File -Append $SuccessLog
|
||||
"[[Backup]] Completed with errors" | Tee-Object -Append $ErrorLog | Tee-Object -Append $SuccessLog | Write-Host
|
||||
$return_value = $false
|
||||
}
|
||||
}
|
||||
|
||||
"[[Backup]] End $(Get-Date) [$tag]" | Out-File -Append $SuccessLog
|
||||
}
|
||||
|
||||
|
||||
Set-Location $starting_location
|
||||
"[[Backup]] End $(Get-Date)" | Out-File -Append $SuccessLog
|
||||
"[[Backup]] End $(Get-Date)" | Tee-Object -Append $SuccessLog | Write-Host
|
||||
|
||||
return $return_value
|
||||
}
|
||||
@@ -297,13 +301,36 @@ function Invoke-Backup {
|
||||
function Send-Email {
|
||||
Param($SuccessLog, $ErrorLog, $Action)
|
||||
|
||||
Import-Module Send-MailKitMessage
|
||||
|
||||
# default the action string to "Backup"
|
||||
if($null -eq $Action) {
|
||||
$Action = "Backup"
|
||||
}
|
||||
|
||||
$password = ConvertTo-SecureString $ResticEmailPassword -AsPlainText -Force
|
||||
$credentials = New-Object System.Management.Automation.PSCredential ($ResticEmailUsername, $password)
|
||||
# set email credentials if a username and passsword are provided in configuration
|
||||
$credentials = @{}
|
||||
if (-not [String]::IsNullOrEmpty($ResticEmailPassword) -and -not [String]::IsNullOrEmpty($ResticEmailUsername)) {
|
||||
$password = ConvertTo-SecureString -String $ResticEmailPassword -AsPlainText -Force
|
||||
$credentials = @{
|
||||
"Credential" = [System.Management.Automation.PSCredential]::new($ResticEmailUsername, $password)
|
||||
}
|
||||
}
|
||||
|
||||
# Backwards compatability for $ResticEmailConfig port definition:
|
||||
# $ResticEmailConfig is obsolete and should be replaced with $ResticEmailPort
|
||||
if ($null -ne $ResticEmailConfig -and $ResticEmailConfig.ContainsKey('Port')) {
|
||||
if ($null -eq $ResticEmailPort) {
|
||||
$ResticEmailPort = $ResticEmailConfig['Port']
|
||||
'[[Email]] Warning - $ResticEmailConfig is deprecated. Define $ResticEmailPort in secrets.ps1 instead.' | Tee-Object -Append $ErrorLog | Tee-Object -Append $SuccessLog | Write-Host
|
||||
}
|
||||
}
|
||||
|
||||
# Backwards compatibility for $PSEmailServer rename to $ResticEmailServer
|
||||
if (($null -ne $PSEmailServer) -and ($null -eq $ResticEmailServer)) {
|
||||
$ResticEmailServer = $PSEmailServer
|
||||
'[[Email]] Warning - $PSEmailServer is deprecated. Define $ResticEmailServer in secrets.ps1 instead.' | Tee-Object -Append $ErrorLog | Tee-Object -Append $SuccessLog | Write-Host
|
||||
}
|
||||
|
||||
$status = "SUCCESS"
|
||||
$past_failure = $false
|
||||
@@ -311,7 +338,7 @@ function Send-Email {
|
||||
if (($null -ne $SuccessLog) -and (Test-Path $SuccessLog) -and (Get-Item $SuccessLog).Length -gt 0) {
|
||||
$body = $(Get-Content -Raw $SuccessLog)
|
||||
|
||||
# if previous run contained an error, send the success email confirming that the error has been resolved
|
||||
# if previous run contained an error, send the success email confirming that the error has been resolved
|
||||
if($Action -eq "Backup") {
|
||||
$past_failure = -not $Script:ResticStateLastBackupSuccessful
|
||||
}
|
||||
@@ -320,24 +347,30 @@ function Send-Email {
|
||||
}
|
||||
}
|
||||
else {
|
||||
$body = "Crtical Error! Restic $Action log is empty or missing. Check log file path."
|
||||
$body = "Critical Error! Restic $Action log is empty or missing. Check log file path."
|
||||
$status = "ERROR"
|
||||
}
|
||||
$attachments = @{}
|
||||
|
||||
$attachments = [System.Collections.Generic.List[string]]::new()
|
||||
if (($null -ne $ErrorLog) -and (Test-Path $ErrorLog) -and (Get-Item $ErrorLog).Length -gt 0) {
|
||||
$attachments = @{Attachments = $ErrorLog}
|
||||
$attachments.Add("$ErrorLog")
|
||||
$status = "ERROR"
|
||||
}
|
||||
|
||||
if((($status -eq "SUCCESS") -and ($SendEmailOnSuccess -ne $false)) -or ((($status -eq "ERROR") -or $past_failure) -and ($SendEmailOnError -ne $false))) {
|
||||
$subject = "$env:COMPUTERNAME Restic $Action Report [$status]"
|
||||
|
||||
# create a temporary error log to log errors; can't write to the same file that Send-MailMessage is reading
|
||||
$temp_error_log = $ErrorLog + "_temp"
|
||||
|
||||
Send-MailMessage @ResticEmailConfig -From $ResticEmailFrom -To $ResticEmailTo -Credential $credentials -Subject $subject -Body $body @attachments 3>&1 2>> $temp_error_log
|
||||
$from = [MimeKit.MailboxAddress]$ResticEmailFrom;
|
||||
$recipients = [MimeKit.InternetAddressList]::new();
|
||||
$recipients.Add([MimeKit.InternetAddress]$ResticEmailTo);
|
||||
|
||||
Send-MailKitMessage -SMTPServer $ResticEmailServer -Port $ResticEmailPort -UseSecureConnectionIfAvailable @credentials -From $from -RecipientList $recipients -Subject $subject -TextBody $body -AttachmentList $attachments 3>&1 2>> $temp_error_log | Out-File -Append $SuccessLog
|
||||
|
||||
if(-not $?) {
|
||||
"[[Email]] Sending email completed with errors" | Tee-Object -Append $temp_error_log | Out-File -Append $SuccessLog
|
||||
"[[Email]] Sending email completed with errors" | Tee-Object -Append $temp_error_log | Tee-Object -Append $SuccessLog | Write-Host
|
||||
}
|
||||
|
||||
# join error logs and remove the temporary
|
||||
@@ -348,22 +381,22 @@ function Send-Email {
|
||||
|
||||
function Invoke-ConnectivityCheck {
|
||||
Param($SuccessLog, $ErrorLog)
|
||||
|
||||
|
||||
if($InternetTestAttempts -le 0) {
|
||||
"[[Internet]] Internet connectivity check disabled. Skipping." | Out-File -Append $SuccessLog
|
||||
"[[Internet]] Internet connectivity check disabled. Skipping." | Out-File -Append $SuccessLog
|
||||
return $true
|
||||
}
|
||||
|
||||
# skip the internet connectivity check for local repos
|
||||
if(Test-Path $env:RESTIC_REPOSITORY) {
|
||||
"[[Internet]] Local repository. Skipping internet connectivity check." | Out-File -Append $SuccessLog
|
||||
"[[Internet]] Local repository. Skipping internet connectivity check." | Out-File -Append $SuccessLog
|
||||
return $true
|
||||
}
|
||||
|
||||
$repository_host = ''
|
||||
|
||||
# use generic internet service for non-specific repo types (e.g. swift:, rclone:, etc. )
|
||||
if(($env:RESTIC_REPOSITORY -match "^swift:") -or
|
||||
if(($env:RESTIC_REPOSITORY -match "^swift:") -or
|
||||
($env:RESTIC_REPOSITORY -match "^rclone:")) {
|
||||
$repository_host = "cloudflare.com"
|
||||
}
|
||||
@@ -409,6 +442,10 @@ function Invoke-ConnectivityCheck {
|
||||
"[[Internet]] Waiting for connection to repository ($repository_host)... $sleep_count" | Out-File -Append $SuccessLog
|
||||
Start-Sleep 30
|
||||
}
|
||||
elseif((-not ([String]::IsNullOrEmpty($BackupOnMeteredNetwork)) -or $BackupOnMeteredNetwork)) -and Invoke-MeteredCheck)) {
|
||||
"[[Internet]] Waiting for an unmetered network connection... $sleep_count" | Out-File -Append $SuccessLog
|
||||
Start-Sleep 30
|
||||
}
|
||||
else {
|
||||
return $true
|
||||
}
|
||||
@@ -435,16 +472,16 @@ function Invoke-HistoryCheck {
|
||||
}
|
||||
|
||||
$filter = "*$Action.err.txt".ToLower()
|
||||
$logs = Get-ChildItem $LogPath -Filter $filter | ForEach-Object{$_.Length -gt 0}
|
||||
$logs = Get-ChildItem $Script:LogPath -Filter $filter | ForEach-Object{$_.Length -gt 0}
|
||||
$logs_with_success = ($logs | Where-Object {($_ -eq $false)}).Count
|
||||
if($logs.Count -gt 0) {
|
||||
Write-Output "[[History]] $Action success rate: $logs_with_success / $($logs.Count) ($(($logs_with_success / $logs.Count).tostring("P")))" | Tee-Object -Append $SuccessLog
|
||||
"[[History]] $Action success rate: $logs_with_success / $($logs.Count) ($(($logs_with_success / $logs.Count).tostring("P")))" | Tee-Object -Append $SuccessLog | Write-Host
|
||||
}
|
||||
}
|
||||
|
||||
# main function
|
||||
function Invoke-Main {
|
||||
|
||||
|
||||
# check for elevation, required for creation of shadow copy (VSS)
|
||||
if (-not (New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
|
||||
{
|
||||
@@ -454,14 +491,22 @@ function Invoke-Main {
|
||||
|
||||
# initialize secrets
|
||||
. $SecretsScript
|
||||
|
||||
|
||||
# initialize config
|
||||
. $ConfigScript
|
||||
|
||||
|
||||
# apply global configuration
|
||||
$Script:ResticExe = Join-Path $InstallPath $ExeName
|
||||
if(-not [String]::IsNullOrEmpty($GlobalParameters)) {
|
||||
$Script:ResticExe = "$Script:ResticExe $GlobalParameters"
|
||||
}
|
||||
$Script:StateFile = Join-Path $InstallPath "state.xml"
|
||||
$Script:LogPath = Join-Path $InstallPath "logs"
|
||||
|
||||
Get-BackupState
|
||||
|
||||
if(!(Test-Path $LogPath)) {
|
||||
Write-Error "[[Backup]] Log file directory $LogPath does not exist. Exiting."
|
||||
if(!(Test-Path $Script:LogPath)) {
|
||||
Write-Error "[[Backup]] Log file directory $Script:LogPath does not exist. Exiting."
|
||||
Send-Email
|
||||
exit 1
|
||||
}
|
||||
@@ -475,51 +520,44 @@ function Invoke-Main {
|
||||
while ($attempt_count -gt 0) {
|
||||
# setup logfiles
|
||||
$timestamp = Get-Date -Format FileDateTime
|
||||
$success_log = Join-Path $LogPath ($timestamp + ".backup.log.txt")
|
||||
$error_log = Join-Path $LogPath ($timestamp + ".backup.err.txt")
|
||||
|
||||
$success_log = Join-Path $Script:LogPath ($timestamp + ".backup.log.txt")
|
||||
$error_log = Join-Path $Script:LogPath ($timestamp + ".backup.err.txt")
|
||||
|
||||
$repository_available = Invoke-ConnectivityCheck $success_log $error_log
|
||||
if($repository_available -eq $true) {
|
||||
$metered_network = Invoke-MeteredCheck
|
||||
if ($BackupOnMeteredNetwork -eq $true -or $metered_network -eq $false) {
|
||||
Invoke-Unlock $success_log $error_log
|
||||
$backup_success = Invoke-Backup $success_log $error_log
|
||||
Invoke-Unlock $success_log $error_log
|
||||
$backup_success = Invoke-Backup $success_log $error_log
|
||||
|
||||
# NOTE: a previously locked repository will cause errors in the log; but backup would be 'successful'
|
||||
# Removing this overly-aggressive test for backup success and relying upon Invoke-Backup to report on success/failure
|
||||
# $backup_success = ($backup_success -eq $true) -and (!(Test-Path $error_log) -or ((Get-Item $error_log).Length -eq 0))
|
||||
$total_attempts = $GlobalRetryAttempts - $attempt_count + 1
|
||||
if($backup_success -eq $true) {
|
||||
# successful backup
|
||||
Write-Output "[[Backup]] Succeeded after $total_attempts attempt(s)" | Tee-Object -Append $success_log
|
||||
# NOTE: a previously locked repository will cause errors in the log; but backup would be 'successful'
|
||||
# Removing this overly-aggressive test for backup success and relying upon Invoke-Backup to report on success/failure
|
||||
# $backup_success = ($backup_success -eq $true) -and (!(Test-Path $error_log) -or ((Get-Item $error_log).Length -eq 0))
|
||||
$total_attempts = $GlobalRetryAttempts - $attempt_count + 1
|
||||
if($backup_success -eq $true) {
|
||||
# successful backup
|
||||
"[[Backup]] Succeeded after $total_attempts attempt(s)" | Tee-Object -Append $success_log | Write-Host
|
||||
|
||||
# test to see if maintenance is needed if the backup was successful
|
||||
$maintenance_needed = Test-Maintenance $success_log $error_log
|
||||
}
|
||||
else {
|
||||
Write-Output "[[Backup]] Ran with errors on attempt $total_attempts" | Tee-Object -Append $success_log | Tee-Object -Append $error_log
|
||||
$error_count++
|
||||
}
|
||||
# test to see if maintenance is needed if the backup was successful
|
||||
$maintenance_needed = Test-Maintenance $success_log $error_log
|
||||
}
|
||||
else {
|
||||
Write-Output "[[Backup]] Failed - currently on metered connection." | Tee-Object -Append $success_log | Tee-Object -Append $error_log
|
||||
"[[Backup]] Ran with errors on attempt $total_attempts" | Tee-Object -Append $success_log | Tee-Object -Append $error_log | Write-Host
|
||||
$error_count++
|
||||
}
|
||||
}
|
||||
else {
|
||||
Write-Output "[[Backup]] Failed - cannot access repository." | Tee-Object -Append $success_log | Tee-Object -Append $error_log
|
||||
"[[Backup]] Failed - cannot access repository." | Tee-Object -Append $success_log | Tee-Object -Append $error_log | Write-Host
|
||||
$error_count++
|
||||
}
|
||||
|
||||
$attempt_count--
|
||||
|
||||
|
||||
# update logs prior to sending email
|
||||
if($backup_success -eq $false) {
|
||||
if($backup_success -eq $false) {
|
||||
if($attempt_count -gt 0) {
|
||||
Write-Output "[[Backup]] Sleeping for 15 min and then retrying..." | Tee-Object -Append $success_log
|
||||
"[[Backup]] Sleeping for 15 min and then retrying..." | Tee-Object -Append $success_log | Write-Host
|
||||
}
|
||||
else {
|
||||
Write-Output "[[Backup]] Retry limit has been reached. No more attempts to backup will be made." | Tee-Object -Append $success_log
|
||||
"[[Backup]] Retry limit has been reached. No more attempts to backup will be made." | Tee-Object -Append $success_log | Write-Host
|
||||
}
|
||||
}
|
||||
|
||||
@@ -528,7 +566,7 @@ function Invoke-Main {
|
||||
|
||||
# update the state of the last backup success or failure
|
||||
$Script:ResticStateLastBackupSuccessful = $backup_success
|
||||
|
||||
|
||||
# Save state to file
|
||||
Set-BackupState
|
||||
|
||||
@@ -539,44 +577,44 @@ function Invoke-Main {
|
||||
else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# only run maintenance if the backup was successful and maintenance is needed
|
||||
$attempt_count = $GlobalRetryAttempts
|
||||
while (($maintenance_needed -eq $true) -and ($attempt_count -gt 0)) {
|
||||
# setup logfiles
|
||||
$timestamp = Get-Date -Format FileDateTime
|
||||
$success_log = Join-Path $LogPath ($timestamp + ".maintenance.log.txt")
|
||||
$error_log = Join-Path $LogPath ($timestamp + ".maintenance.err.txt")
|
||||
|
||||
$success_log = Join-Path $Script:LogPath ($timestamp + ".maintenance.log.txt")
|
||||
$error_log = Join-Path $Script:LogPath ($timestamp + ".maintenance.err.txt")
|
||||
|
||||
$repository_available = Invoke-ConnectivityCheck $success_log $error_log
|
||||
if($repository_available -eq $true) {
|
||||
if($repository_available -eq $true) {
|
||||
$maintenance_success = Invoke-Maintenance $success_log $error_log
|
||||
|
||||
# $maintenance_success = ($maintenance_success -eq $true) -and (!(Test-Path $error_log) -or ((Get-Item $error_log).Length -eq 0))
|
||||
$total_attempts = $GlobalRetryAttempts - $attempt_count + 1
|
||||
if($maintenance_success -eq $true) {
|
||||
Write-Output "[[Maintenance]] Succeeded after $total_attempts attempt(s)" | Tee-Object -Append $success_log
|
||||
"[[Maintenance]] Succeeded after $total_attempts attempt(s)" | Tee-Object -Append $success_log | Write-Host
|
||||
}
|
||||
else {
|
||||
Write-Output "[[Maintenance]] Ran with errors on attempt $total_attempts" | Tee-Object -Append $success_log | Tee-Object -Append $error_log
|
||||
"[[Maintenance]] Ran with errors on attempt $total_attempts" | Tee-Object -Append $success_log | Tee-Object -Append $error_log | Write-Host
|
||||
$error_count++
|
||||
}
|
||||
}
|
||||
else {
|
||||
Write-Output "[[Maintenance]] Failed - cannot access repository." | Tee-Object -Append $success_log | Tee-Object -Append $error_log
|
||||
"[[Maintenance]] Failed - cannot access repository." | Tee-Object -Append $success_log | Tee-Object -Append $error_log | Write-Host
|
||||
$error_count++
|
||||
}
|
||||
|
||||
|
||||
$attempt_count--
|
||||
|
||||
# update logs prior to sending email
|
||||
if($maintenance_success -eq $false) {
|
||||
if($maintenance_success -eq $false) {
|
||||
if($attempt_count -gt 0) {
|
||||
Write-Output "[[Maintenance]] Sleeping for 15 min and then retrying..." | Tee-Object -Append $success_log
|
||||
"[[Maintenance]] Sleeping for 15 min and then retrying..." | Tee-Object -Append $success_log | Write-Host
|
||||
}
|
||||
else {
|
||||
Write-Output "[[Maintenance]] Retry limit has been reached. No more attempts to run maintenance will be made." | Tee-Object -Append $success_log
|
||||
"[[Maintenance]] Retry limit has been reached. No more attempts to run maintenance will be made." | Tee-Object -Append $success_log | Write-Host
|
||||
}
|
||||
}
|
||||
|
||||
@@ -585,7 +623,7 @@ function Invoke-Main {
|
||||
|
||||
# update the state of the last maintenance success or failure
|
||||
$Script:ResticStateLastMaintenanceSuccessful = $maintenance_success
|
||||
|
||||
|
||||
# Save state to file
|
||||
Set-BackupState
|
||||
|
||||
@@ -596,13 +634,13 @@ function Invoke-Main {
|
||||
else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Save state to file
|
||||
Set-BackupState
|
||||
|
||||
# cleanup older log files
|
||||
Get-ChildItem $LogPath | Where-Object {$_.CreationTime -lt $(Get-Date).AddDays(-$LogRetentionDays)} | Remove-Item
|
||||
Get-ChildItem $Script:LogPath | Where-Object {$_.CreationTime -lt $(Get-Date).AddDays(-$LogRetentionDays)} | Remove-Item
|
||||
|
||||
exit $error_count
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user