Skip to content

Commit

Permalink
Add feature to control backups on metered connections
Browse files Browse the repository at this point in the history
restic-windows-backup runs backups irrespective of the host being on a metered connection or unrestricted. That can lead to situations in which the backup takes place when the user is on a metered connection and would rather not do it at that time, either because they would incur costs or use up their data allowance.

The default value of $BackupOnMeteredNetwork is set to $false following a conservative approach.

Closes kmwoley#82

Signed-off-by: Manuel Fombuena <[email protected]>
  • Loading branch information
innovara committed Nov 11, 2024
1 parent f759630 commit 09b9066
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
46 changes: 31 additions & 15 deletions backup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,15 @@ function Invoke-ConnectivityCheck {
}
}

# check if on metered network
function Invoke-MeteredCheck {

[void][Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType = WindowsRuntime]

$cost = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile().GetConnectionCost()
$cost.ApproachingDataLimit -or $cost.OverDataLimit -or $cost.Roaming -or $cost.BackgroundDataUsageRestricted -or ($cost.NetworkCostType -ne "Unrestricted")
}

# check previous logs
function Invoke-HistoryCheck {
Param($SuccessLog, $ErrorLog, $Action)
Expand Down Expand Up @@ -470,23 +479,30 @@ function Invoke-Main {
$error_log = Join-Path $LogPath ($timestamp + ".backup.err.txt")

$repository_available = Invoke-ConnectivityCheck $success_log $error_log
if($repository_available -eq $true) {
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

# test to see if maintenance is needed if the backup was successful
$maintenance_needed = Test-Maintenance $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

# 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

# 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++
}
}
else {
Write-Output "[[Backup]] Ran with errors on attempt $total_attempts" | Tee-Object -Append $success_log | Tee-Object -Append $error_log
Write-Output "[[Backup]] Failed - currently on metered connection." | Tee-Object -Append $success_log | Tee-Object -Append $error_log
$error_count++
}
}
Expand Down
1 change: 1 addition & 0 deletions config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ $WindowsExcludeFile = Join-Path $InstallPath "windows.exclude"
$LocalExcludeFile = Join-Path $InstallPath "local.exclude"
$LogPath = Join-Path $InstallPath "logs"
$LogRetentionDays = 30
$BackupOnMeteredNetwork = $false
$InternetTestAttempts = 10
$GlobalRetryAttempts = 4
$IgnoreMissingBackupSources = $false
Expand Down

0 comments on commit 09b9066

Please sign in to comment.