How to specify destination repository in the "snapshot create" command for consistent results? (and auto-create the repo if it doesn't exist)

I’m trying to generate .bat batch files (for Windows) and .sh shell scripts (for Linux) to perform automated backups.

Is there a kopia snapshot create command where I can actually specify the destination repo? i.e. Without it being reliant on whatever commands may or may not have been run before it? (and whether those previous commands succeeded or failed)

I’m concerned about race conditions and other issues in the case where the source servers have multiple repos they connect to, and it also just makes scripting a lot more complicated needing to to deal with this “modal” nature of running separate commands to “connect” to one repo at a time, and then actually do backups to which ever is the “current” repo is. Or maybe I’ve misunderstood how Kopia works?

Ideally I’m also looking for a command that can first check if the repo exists first, and auto-create it if it doesn’t. Is this possible?

I’d love a simple one-liner that gives consistent results like:

kopia snapshot create --auto-create-repo --repo="b2:bucketname/prefixpath/" /home/my-stuff-to-backup

This would greatly simplify the usage of Kopia without needing to write complicate scripts for this common use case.

Or could this somehow be done with environment variables? Are they documented somewhere?

Sorry if I’ve missed something! I’ve been trying to figure this out today and yesterday, but haven’t been able to figure out if it’s possible at all.

Kopia parameters are all derived from a configuration file which is created when you do repo connect. If you want to avoid connecting (or have multiple connected repositories) you can specify config file yourself.

Basically you can create the config once (by repo create/repo connect) and bundle it together with your script using --config-file ~/my.config to point at it. The last thing you need to to do is passing the password which by default is not stored in the config but in OS-specific keychain. You can either always pass it using KOPIA_PASSWORD environment variable, alternatively you can persist it in a file:

For example:

kopia repo create filesystem --config-file ~/myconfig --path /tmp/foo --no-use-keychain

This wii create ~/myconfig and ~/myconfig.kopia-password which you can bundle together with your scripts.

Hope it helps.

1 Like

Cheers, thanks for your help on this!

Really loving Kopia! Especially the fact that mounting even works on Windows!

A couple of follow up questions:

  1. Any thoughts on the --auto-create-repo thing? Is it something you’d consider adding in the future? It would be amazing to just be able to deploy a single idempotent command out to my servers so that they can auto-create repos in a simple way. (password for the new repo can just be taken from the same KOPIA_PASSWORD environment variable)
  2. Does Kopia have any environment variables other than KOPIA_PASSWORD ? Any doco anywhere on them?

Thanks once again!