define custom variable in configuration file #612
Labels
No Label
bug
data loss
design finalized
good first issue
new feature area
question / support
security
waiting for response
No Milestone
No Assignees
4 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: borgmatic-collective/borgmatic#612
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What I'm trying to do and why
I'd like to deploy a per application (containerized web apps) configuration with a factorization of my configuration.
In my current configuration, I use the same borg repo for all my apps, but with a specific prefix for each one.
Steps to reproduce (if a bug)
Actual behavior (if a bug)
I need to specify my prefix in several sections (storage, retention, consistency, and ntfy).
Expected behavior (if a bug)
I'd like to define a variable "prefix" that I can use in all my prefix entries. And I'd like this variable to be expendanded after the configuration files merge.
Other notes / implementation ideas
Environment
borgmatic version: 1.7.4
borgmatic installation method: Debian package (from testing)
Borg version: 1.2.2
Python version: 3.9.2
Database version (if applicable): NA
operating system and version: Debian 11
Have you seen the environment variable interpolation feature? Does that work for your use case? Or do you actually need to lay down the variable values within the configuration before runtime for some reason?
Hi,
My borgmatic jobs are triggered by the systemd timer. Since a few releases, the debian package deploys the borgmatic.service and borgmatic.timer. Since then, I replaced my custom cron jobs by the usage of this timer and associated service.
I do not see how to use the env variable interpollation in my case, as all my per application configurations uses the same environment (the one provided by the systemd service).
I think such variable mechanism would help configurations like this:
I do check on only one repo (which might not be a good idea... I wille change this), but more important I use a custom prefix, and thus need to copy/paste this prefix in several places.
Got it. So you're looking for something like the following?
Is that the sort of thing you had in mind?
Yes something like that.
Maybe "variables" instead of "constants" ?
And it would be great if these can be handled in "deepe merge". So we can define a default value in a template file, and then override it in each per-application file.
That makes sense!
Nice idea! I'd like to add this functionality to Borgmatic. @witten could you help me understand where the default ({now} etc) interpolation from config takes place, maybe that'll help.
There are actually two levels of interpolation going on, and unfortunately I don't think either of them will work directly for this use case (although maybe some of the code could be reused/repurposed):
{now}
, used in generating archive names.Edit: Skip this and see the solution below.
Right, I was able to get this working with a "trick", at the end of
normalize.py
, and at the beginning ofborgmatic.py::run_configuration()
, I added this:However, this works for the config you mentioned above only when you add quotes to string parameters, like so:
Otherwise yaml interprets it as a dict. Now we can get this to work for strings by adding quotes, but what about integer params etc.?
Two options:
int
/bool
/str
?) and replace the string by this value internally, and continue execution.Let me know if you have a better option too.
Simpler solution that supports all types too:
load.py::load_configuration()
(I think this is where Borgmatic first checks the config file too).I can move this to another function, and this seems to be a cleaner and straightforward approach if I am not missing anything.
Your most recent approach seems totally reasonable to me. A couple of thoughts:
constants
is not found in the configuration, then don't reload the configuration a second time.constants
from the loaded configuration after you consume them?constants
, you probably need to YAML-encode thevalue
when you replace the constant name—not just callstr()
. For instance, let's say a constant value is an entire YAML dict or list like:See
render_configuration()
for one example of dumping YAML to a string.Thanks for digging into this!
This is implemented in master now (thanks to @diivi!) and will be part of the next release. We did end up going with
constants:
though. You can find the documentation here once it goes live in the next few minutes: https://torsion.org/borgmatic/docs/how-to/make-per-application-backups/#constant-interpolationAnd thank you for suggesting this feature!
Released in borgmatic 1.7.10!
I know this is closed, but I had a question about it - let me know if I should open a new issue instead.
The original ask was:
However, the docs for the implementation say:
Am I understanding right that this doesn't actually work for the original use case?
I'm asking because I'm trying to do this:
common.yaml:
someApp.yaml:
But that fails with:
I think you're correct that "after configuration files merge" portion isn't implemented, but if you look at the example above, that use case is addressed. Namely, if you've got multiple
prefix
options in a single configuration file, you can use a user-defined constant in them. (Perhaps ironically, those particularprefix
options are now deprecated.)As for your example use case, I agree that would be a pretty useful enhancement. Would you mind filing a separate ticket for it?
Got it, thanks! Will file a separate ticket.