Support custom variables in borgmatic config files #472

Closed
opened 2021-11-27 15:12:23 +00:00 by tikeyknax · 6 comments

What I'm trying to do and why

I have several config files for borgmatic set up that all backup to the same repository. The reason to have them all back up to the same repository is the awesome deduplication feature of borg. The reason to use several config files instead of one is to e.g. use different retention policies.

Imagine e.g. the following config files:

Config pictures
location:
    source_directories:
        - /home/my_user/pictures

    <<: !include /etc/borgmatic.d/common/common_location.yaml

storage:
    archive_name_format: "{hostname}-pictures-{now:%Y-%m-%dT%H:%M:%S}"

    <<: !include /etc/borgmatic.d/common/common_storage.yaml


retention:
    <<: !include /etc/borgmatic.d/common/retention_1year.yaml

    prefix: "{hostname}-pictures-"

consistency:
    prefix: "{hostname}-pictures-"

hooks:
    on_error:
        - echo "[borgmatic - pictures] ERROR Error during prune/create/check." | mail -s "[borgmatic - pictures] ERROR" -r alert@mydomain.com alert@mydomain.com

    before_everything:
        - echo "[borgmatic - pictures] Starting actions."

    after_everything:
        - echo "[borgmatic - pictures] Finished all actions."
Config some_folder
location:
    source_directories:
        - /home/my_user/some_folder

    <<: !include /etc/borgmatic.d/common/common_location.yaml

storage:
    archive_name_format: "{hostname}-somefolder-{now:%Y-%m-%dT%H:%M:%S}"

    <<: !include /etc/borgmatic.d/common/common_storage.yaml


retention:
    <<: !include /etc/borgmatic.d/common/retention_short.yaml

    prefix: "{hostname}-somefolder-"

consistency:
    prefix: "{hostname}-somefolder-"

hooks:
    on_error:
        - echo "[borgmatic - somefolder] ERROR Error during prune/create/check." | mail -s "[borgmatic - somefolder] ERROR" -r alert@mydomain.com alert@mydomain.com

    before_everything:
        - echo "[borgmatic - somefolder] Starting actions."

    after_everything:
        - echo "[borgmatic - somefolder] Finished all actions."

Now, for only 2 configs this is still fairly easy but having more configs, it would be really nice if one could even shorten these configs. So if borgmatic would support (some) custom variables, the 2 config files could be simplified even further:

Config pictures
variables:
    app_name: pictures
    
location:
    source_directories:
        - /home/my_user/pictures

    <<: !include /etc/borgmatic.d/common/common_location.yaml

storage:
    archive_name_format: "{hostname}-{app_name}-{now:%Y-%m-%dT%H:%M:%S}"

    <<: !include /etc/borgmatic.d/common/common_storage.yaml


retention:
    <<: !include /etc/borgmatic.d/common/retention_1year.yaml

    prefix: "{hostname}-{app_name}-"

consistency:
    prefix: "{hostname}-{app_name}-"

hooks:
    on_error:
        - echo "[borgmatic - {app_name}] ERROR Error during prune/create/check." | mail -s "[borgmatic - pictures] ERROR" -r alert@mydomain.com alert@mydomain.com

    before_everything:
        - echo "[borgmatic - {app_name}] Starting actions."

    after_everything:
        - echo "[borgmatic - {app_name}] Finished all actions."
Config some_folder
variables:
    app_name: some_folder
    
location:
    source_directories:
        - /home/my_user/some_folder

    <<: !include /etc/borgmatic.d/common/common_location.yaml

storage:
    archive_name_format: "{hostname}-{app_name}-{now:%Y-%m-%dT%H:%M:%S}"

    <<: !include /etc/borgmatic.d/common/common_storage.yaml


retention:
    <<: !include /etc/borgmatic.d/common/retention_short.yaml

    prefix: "{hostname}-{app_name}-"

consistency:
    prefix: "{hostname}-{app_name}-"

hooks:
    on_error:
        - echo "[borgmatic - {app_name}] ERROR Error during prune/create/check." | mail -s "[borgmatic - somefolder] ERROR" -r alert@mydomain.com alert@mydomain.com

    before_everything:
        - echo "[borgmatic - {app_name}] Starting actions."

    after_everything:
        - echo "[borgmatic - {app_name}] Finished all actions."

Now, the archive_name_format, the prefixes and the hooks are exactly the same in both files and can thus be moved into the include files and one would end up with something like:

Config pictures
variables:
    app_name: pictures
    
location:
    source_directories:
        - /home/my_user/pictures

    <<: !include /etc/borgmatic.d/common/common_location.yaml

storage:
    <<: !include /etc/borgmatic.d/common/common_storage.yaml


retention:
    <<: !include /etc/borgmatic.d/common/retention_short.yaml

consistency:
    <<: !include /etc/borgmatic.d/common/common_consistency.yaml

hooks:
    <<: !include /etc/borgmatic.d/common/common_hooks.yaml
Config some_folder
variables:
    app_name: some_folder
    
location:
    source_directories:
        - /home/my_user/some_folder

    <<: !include /etc/borgmatic.d/common/common_location.yaml

storage:
    <<: !include /etc/borgmatic.d/common/common_storage.yaml


retention:
    <<: !include /etc/borgmatic.d/common/retention_short.yaml

consistency:
    <<: !include /etc/borgmatic.d/common/common_consistency.yaml

hooks:
    <<: !include /etc/borgmatic.d/common/common_hooks.yaml
#### What I'm trying to do and why I have several config files for borgmatic set up that all backup to the same repository. The reason to have them all back up to the same repository is the awesome deduplication feature of borg. The reason to use several config files instead of one is to e.g. use different retention policies. Imagine e.g. the following config files: ##### Config pictures ``` location: source_directories: - /home/my_user/pictures <<: !include /etc/borgmatic.d/common/common_location.yaml storage: archive_name_format: "{hostname}-pictures-{now:%Y-%m-%dT%H:%M:%S}" <<: !include /etc/borgmatic.d/common/common_storage.yaml retention: <<: !include /etc/borgmatic.d/common/retention_1year.yaml prefix: "{hostname}-pictures-" consistency: prefix: "{hostname}-pictures-" hooks: on_error: - echo "[borgmatic - pictures] ERROR Error during prune/create/check." | mail -s "[borgmatic - pictures] ERROR" -r alert@mydomain.com alert@mydomain.com before_everything: - echo "[borgmatic - pictures] Starting actions." after_everything: - echo "[borgmatic - pictures] Finished all actions." ``` ##### Config some_folder ``` location: source_directories: - /home/my_user/some_folder <<: !include /etc/borgmatic.d/common/common_location.yaml storage: archive_name_format: "{hostname}-somefolder-{now:%Y-%m-%dT%H:%M:%S}" <<: !include /etc/borgmatic.d/common/common_storage.yaml retention: <<: !include /etc/borgmatic.d/common/retention_short.yaml prefix: "{hostname}-somefolder-" consistency: prefix: "{hostname}-somefolder-" hooks: on_error: - echo "[borgmatic - somefolder] ERROR Error during prune/create/check." | mail -s "[borgmatic - somefolder] ERROR" -r alert@mydomain.com alert@mydomain.com before_everything: - echo "[borgmatic - somefolder] Starting actions." after_everything: - echo "[borgmatic - somefolder] Finished all actions." ``` Now, for only 2 configs this is still fairly easy but having more configs, it would be really nice if one could even shorten these configs. So if borgmatic would support (some) custom variables, the 2 config files could be simplified even further: ##### Config pictures ``` variables: app_name: pictures location: source_directories: - /home/my_user/pictures <<: !include /etc/borgmatic.d/common/common_location.yaml storage: archive_name_format: "{hostname}-{app_name}-{now:%Y-%m-%dT%H:%M:%S}" <<: !include /etc/borgmatic.d/common/common_storage.yaml retention: <<: !include /etc/borgmatic.d/common/retention_1year.yaml prefix: "{hostname}-{app_name}-" consistency: prefix: "{hostname}-{app_name}-" hooks: on_error: - echo "[borgmatic - {app_name}] ERROR Error during prune/create/check." | mail -s "[borgmatic - pictures] ERROR" -r alert@mydomain.com alert@mydomain.com before_everything: - echo "[borgmatic - {app_name}] Starting actions." after_everything: - echo "[borgmatic - {app_name}] Finished all actions." ``` ##### Config some_folder ``` variables: app_name: some_folder location: source_directories: - /home/my_user/some_folder <<: !include /etc/borgmatic.d/common/common_location.yaml storage: archive_name_format: "{hostname}-{app_name}-{now:%Y-%m-%dT%H:%M:%S}" <<: !include /etc/borgmatic.d/common/common_storage.yaml retention: <<: !include /etc/borgmatic.d/common/retention_short.yaml prefix: "{hostname}-{app_name}-" consistency: prefix: "{hostname}-{app_name}-" hooks: on_error: - echo "[borgmatic - {app_name}] ERROR Error during prune/create/check." | mail -s "[borgmatic - somefolder] ERROR" -r alert@mydomain.com alert@mydomain.com before_everything: - echo "[borgmatic - {app_name}] Starting actions." after_everything: - echo "[borgmatic - {app_name}] Finished all actions." ``` Now, the archive_name_format, the prefixes and the hooks are exactly the same in both files and can thus be moved into the include files and one would end up with something like: ##### Config pictures ``` variables: app_name: pictures location: source_directories: - /home/my_user/pictures <<: !include /etc/borgmatic.d/common/common_location.yaml storage: <<: !include /etc/borgmatic.d/common/common_storage.yaml retention: <<: !include /etc/borgmatic.d/common/retention_short.yaml consistency: <<: !include /etc/borgmatic.d/common/common_consistency.yaml hooks: <<: !include /etc/borgmatic.d/common/common_hooks.yaml ``` ##### Config some_folder ``` variables: app_name: some_folder location: source_directories: - /home/my_user/some_folder <<: !include /etc/borgmatic.d/common/common_location.yaml storage: <<: !include /etc/borgmatic.d/common/common_storage.yaml retention: <<: !include /etc/borgmatic.d/common/retention_short.yaml consistency: <<: !include /etc/borgmatic.d/common/common_consistency.yaml hooks: <<: !include /etc/borgmatic.d/common/common_hooks.yaml ```
Author

I figured out that {configuration_filename} is not only supported in the on error hook but also in the other hooks. So a way to at least unify the hooks for all the backups is:

after_backup:
- echo "[backup] Finished backup in `echo {configuration_filename} | awk -F\/ '{print $NF}'` on `date +'%a %d.%m.%Y - %H:%M:%S'`"

I have not yet checked if this would also work for the prefixes.

I figured out that `{configuration_filename}` is not only supported in the on error hook but also in the other hooks. So a way to at least unify the hooks for all the backups is: ``` after_backup: - echo "[backup] Finished backup in `echo {configuration_filename} | awk -F\/ '{print $NF}'` on `date +'%a %d.%m.%Y - %H:%M:%S'`" ``` I have not yet checked if this would also work for the prefixes.
Owner

Sorry for the delay here. This is a pretty interesting idea! I'll have to think about how this would work given that the interpolation would (presumably) have to work for all values and not just within hooks like the existing borgmatic variable interpolation.

Anyway, I wanted to mention that with #381 now implemented in master, you will be able to simplify configuration like the following:

location:
    source_directories:
        - /home/my_user/some_folder

    <<: !include /etc/borgmatic.d/common/common_location.yaml

storage:
    <<: !include /etc/borgmatic.d/common/common_storage.yaml


retention:
    <<: !include /etc/borgmatic.d/common/retention_short.yaml

consistency:
    <<: !include /etc/borgmatic.d/common/common_consistency.yaml

hooks:
    <<: !include /etc/borgmatic.d/common/common_hooks.yaml

... to something more like this:

<<: !include /etc/borgmatic.d/common/common.yaml

location:
    source_directories:
        - /home/my_user/some_folder

retention:
    <<: !include /etc/borgmatic.d/common/retention_short.yaml

That wouldn't support the custom variables described in this ticket (yet), but it would at minimum cut down on some of the boilerplate repeated in each configuration file.

Sorry for the delay here. This is a pretty interesting idea! I'll have to think about how this would work given that the interpolation would (presumably) have to work for all values and not just within hooks like the existing borgmatic variable interpolation. Anyway, I wanted to mention that with #381 now implemented in master, you will be able to simplify configuration like the following: ```yaml location: source_directories: - /home/my_user/some_folder <<: !include /etc/borgmatic.d/common/common_location.yaml storage: <<: !include /etc/borgmatic.d/common/common_storage.yaml retention: <<: !include /etc/borgmatic.d/common/retention_short.yaml consistency: <<: !include /etc/borgmatic.d/common/common_consistency.yaml hooks: <<: !include /etc/borgmatic.d/common/common_hooks.yaml ``` ... to something more like this: ```yaml <<: !include /etc/borgmatic.d/common/common.yaml location: source_directories: - /home/my_user/some_folder retention: <<: !include /etc/borgmatic.d/common/retention_short.yaml ``` That wouldn't support the custom variables described in this ticket (yet), but it would at minimum cut down on some of the boilerplate repeated in each configuration file.
Author

Thanks for the hint and for implementing #381. This is indeed already a very big step. If that possibility existed when I was writing my config files, I wouldn't have opened this ticket. :)

Thanks for the hint and for implementing #381. This is indeed already a very big step. If that possibility existed when I was writing my config files, I wouldn't have opened this ticket. :)
Owner

Glad to hear it might be useful. Now that #381 is released, do you still have the need for custom variables? Either way, I just wanted to confirm. Thanks!

Glad to hear it might be useful. Now that #381 is released, do you still have the need for custom variables? Either way, I just wanted to confirm. Thanks!
Author

Thanks for getting back to me. I think there would be an advantage fo also having the variables but to be honest it is now rather small. So I'm totally happy with what you have done.

Thanks for getting back to me. I think there would be an advantage fo also having the variables but to be honest it is now rather small. So I'm totally happy with what you have done.
Owner

Okay, I'll close this for now. But if the need for the feature increases or anyone else has the same use case, please feel free to reopen!

Okay, I'll close this for now. But if the need for the feature increases or anyone else has the same use case, please feel free to reopen!
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: borgmatic-collective/borgmatic#472
No description provided.