Only back up when on certain WiFi networks?

Is it possible to limit backups to when my machine is connected to certain WiFi networks?

I’m going to travel, and don’t want to run backups from a hotel where wifi is metered.

I could imagine adding “*/**” to a .kopiaignore in the root folder, manually or via script, depending on my current SSID. Any other suggestions?

Thanks,

-k.

Probably use a “before-snapshot” action to check if your environment checks out and exit with an error if not. That should prevent the snapshot from happening at all.

Thanks. That’s a great solution.

It doesn’t seem to be working. I have this script

SSID_REQUIRED="my_network"
SSID_REQUIRED="INVALID"
ssid=$(/usr/sbin/iwgetid -r 2>/dev/null)

date >> ~/Desktop/debug.txt

if [[ "$ssid" == "$SSID_REQUIRED" ]]; then
  exit 0
else
  exit 1
fi

And testing it with kopia_wifi.sh; echo $? shows it works from the command line. And checking ~/Desktop/debug.txt shows the script runs when Kopia runs.

I’m using KopiaUI, which may be the problem? The script runs, but the backup also always runs, it never skips a backup. I’m manually clicking the “snapshot now” button. Does manual snapshot override this script?

Have you enabled kopia actions? They are off by default:

When using KopiaUI, actions can be enabled globally by editing your repository.config (it is located in the Config File location in KopiaUI under Repository) and change "enableActions": false to "enableActions": true. Save the file and restart KopiaUI.

and make sure that your action is configured as essential - meaning that it must succeed for kopia backup to continue.

Yes, checking ~/Desktop/debug.txt shows the script runs when Kopia runs. I’ve edited the config files to enable actions.

I have been using actions for long. They work.

Try to hardcode exit 1 and see if it works - meaning it stops backup from happening.

kopia policy show --global

shows

Run command before snapshot root:              (defined for this target)
  Command:
    /home/kdm/bin/kopia_before.sh 
  Mode:                            essential
  Timeout:                              5m0s

And the contents of ~/bin/kopia_before.sh are

#!/bin/bash

exit 1

SSID_REQUIRED="my_network"
SSID_REQUIRED="INVALID"
ssid=$(/usr/sbin/iwgetid -r 2>/dev/null)

date >> ~/Desktop/debug.txt

if [[ "$ssid" == "$SSID_REQUIRED" ]]; then
  exit 0
else
  exit 1
fi

And the backup still occurs both at the scheduled time and when manually clicked in KopiaUI.

And repository.config contains, among other things:

  "enableActions": true,

If you’re running a Kopia server like me, than the path for your local script might be something else. E.g. my Kopia server runs on a Linux VM and the path to the script is like this:

/kopia/repos/actions/checkFreeSpace.sh

However, on my Mac the script has to be placed in:

~/.config/kopiaActions/checkFreeSpace.sh

Maybe your script doesn’t get executed…?

I’m not running a server, just a local KopiaUI. I’m testing this on a local backup (just some files into a different folder). My other backups are non-server too, run from local, over sftp to a remote disk.

The script is executed. I have a “date >> ~/Desktop/debug.txt” which records that the script runs. (not the current version pasted above, where the date command is after the exit command. But without exit 1, the script clearly runs.

The script returns 1 when I test it on the command line.

Actions are enabled.

Did you also alter your repo connection to actually allow actions, like mentioned in the docs?

Yes, I showed the cut and paste output of kopia policy show --global above. And the script is clearly running. Can it run if actions are not enabled? Run but ignore result??

I don’t mean the policy. You need actually to enable actions, when connecting to the repo. Check your repo config - it should have “enableActions”: true in it.

I did simple test.

With essential before snapshot action as my kopia_test.sh script.

$ kopia policy show --global
...
Run command before snapshot root:              (defined for this target)
  Embedded script (stored in repository):
    /Users/kptsky/bin/kopia/kopia_test.sh
  Mode:                            essential
  Timeout:                              5m0s
...

For script returning exit code 0:

$ cat kopia_test.sh
exit 0

$ kopia snapshot create --all
Snapshotting kptsky@kptsky-laptop:/Users/kptsky/Temp ...
 * 0 hashing, 0 hashed (0 B), 6188 cached (289.6 MB), uploaded 0 B, estimating...
Created snapshot with root kffdbf7d3a58c25290354c55e58786a88 and ID b119fc9e15cd6c13dc3b016bd6b0fa4c in 0s

For script returning exit code 1:

$ cat kopia_test.sh
exit 1

$ kopia snapshot create --all
Snapshotting kptsky@kptsky-laptop:/Users/kptsky/Temp ...
ERROR upload error: exit status 1

The same when using GUI. When script returns exit code 1 I am presented with notification that snapshot failed.

@mankoff - I am not sure why it does not work for you. Maybe your script always returns 0? Try with my super script:) Also are you on the latest kopia? v0.21.1? I have a feeling that you are on some old version as wording of your commands output is different.

@budy: Thank you for suggesting enabling actions. They are enabled and I have said so 3x and shown it with a cut-and-paste. They were not during my first attempt, but after kapitainsky suggested it, I enabled them and restarted kopia.

Your kopia policy show --global shows different results than mine above. Yours contains the output Embedded script (stored in repository): and mine does not contain that. Perhaps that is part of the problem? I am using v0.21.1. I am unable to test this further at the moment because I am in Greenland and on the WiFi network where I do not want kopia to run, so I have shut down the program. I will try it again when I am home.

maybe there is different wording on different OS (I am on macOS). But it would be strange regardless.

As I said, I do run a Kopia server which hosts my repo and the global policy for that repo also has this “Embedded script (stored in repo)”, which seems confusing to me, since the script itself is not stored in the repo. This is what my global policy states about my actions:

Run command before snapshot root: (defined for this target)
Embedded script (stored in repository):
/kopia/repos/actions/checkFreeSpace.sh
Mode: essential
Timeout: 5m0s

So the script on the Kopia server indeed is located at that path. However, on my KopiaUI client on the Mac, I have set a different location for the script, since it’s really not the same script anyway:

Run command before snapshot root: (defined for this target)
Embedded script (stored in repository):
/Users/stephan.budach/.config/kopiaActions/checkFreeSpace.sh
Mode: essential
Timeout: 30s

Do you, by any chance, have other policies, besides the global one?

Here are my policies:

And I’m setting this script under the 2nd one (actions).

Hello again.

I now have this working from the command line.

kopia policy set /home/kdm --before-snapshot-root-action ~/bin/kopia_network.sh --action-command-mode=essential

kopia snapshot create /home/kdm --force-enable-actions

If I have that, and kopia_network.sh contains exit 1 then when I try to make a backup I see the correct message:

$ kopia snapshot create /home/kdm 
Snapshotting kdm@fw13:/home/kdm ...
ERROR upload error: exit status 1

But if I open KopiaUI and press the “snapshot now” button, the snapshot runs. Is there something different between the UI and the command line?