Pre-backup script to prevent running over a metered connection

Has anybody written a pre-backup script that can prevent the WebUI from initiating a backup when connected via a hotspot metered connection? I don’t even know where to begin…

On what OS?

For windows it’s easy, as connection can explicitly be marked as “metered”, you can then query if it had associated cost: powershell - Check if current network connection is metered in Windows batch file - Stack Overflow

For macOS and other OSes perhaps you can do the opposite and whitelist networks based on SSID (macos - Get wireless SSID through shell script on Mac OS X - Stack Overflow) or by gateway address (macos - How to get default gateway in Mac OSX - Stack Overflow).

Still, this won’t get you far — because what if you start backup, pick up laptop and go roam to metered network? Ideally you would want to check on connection change (or periodically, for simplicity, and as a proof of concept, or even check if it runs on battery vs wall power — it may be enough in your circumstances) and if connection is blacklisted — kill any running instances of duplicacy CLI or block it in OS firewall. This would be standalone script and you won’t need to hunt down prebackup script location.

To be done properly thought this functionality should be in WebUI itself.

So I am using the WebUI on Windows. I followed the first link you provided and found the below script:

[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”)

I ran it from powershell while connected via wifi and it returned the value “False”. I then connected to my phone as a hotspot and it still returned the same. Not sure why this would be. And do I just insert this script raw into the pre-backup script field?

You also stated that this won’t be effective at killing the backup process if the backup is already in progress and I move to my hotspot. I presume when using this method it would be setup as a windows scheduled task? I don’t know that this would be as important to me but it makes sense.

See this Metered connections in Windows 10

If you connect to your phone as a hotspot it is just another wifi network. How should windows know it is metered unless you tell it? (And even if it knew it is cellular — most cellular plans are unlimited nowadays in US at least…. some configuration would still be required). In other words you need to tell windows that the specific connection is metered.

But now I’m thinking — maybe there is a way to block app from using specific network connections on an application firewall. Then you don’t need to mess with scripts — duplicacy simply won’t be able to connect.