Support Overriding System Hostname #1317

Closed
opened 2026-05-30 06:08:54 +00:00 by quietquark · 13 comments

What I'd like to do and why

Just wanted to add an upstream borgbackup request that I have open:

https://github.com/borgbackup/borg/issues/9651

(borg2 already supports this).

This simplifies some use cases with borg (as documented in the above issue).

I think it is equally useful for borgmatic to support custom hostnames when one "controller" impersonates othe machines.

May also help with some other edge cases depending on how borgmatic is used. See #1310

Other notes / implementation ideas

Borgmatic should respect an environment variable (e.g. BORGMATIC_HOSTNAME or something like this depending on what gets finalized with borg as well) and use this instead of hostname wherever relevant.

Additionally if the BORG_HOSTNAME (or equivalent env var) is NOT set, then prior to invoking borg, borgmatic should duplicate the BORGMATIC_HOSTNAME variable into BORG_HOSTNAME (or equivalent) OR explicitly invoke borg via the --hostname (or whatever final option is implemented).

It will also be useful if this field/value can be used by hooks/scripts etc. when called via borgmatic.

NOTE: This requires borg support which as of creating this issue is still open (for 1.4.x but completed for 2.x).

### What I'd like to do and why Just wanted to add an upstream borgbackup request that I have open: https://github.com/borgbackup/borg/issues/9651 (borg2 already supports this). This simplifies some use cases with borg (as documented in the above issue). I think it is equally useful for borgmatic to support custom hostnames when one "controller" impersonates othe machines. May also help with some other edge cases depending on how borgmatic is used. See #1310 ### Other notes / implementation ideas Borgmatic should respect an environment variable (e.g. `BORGMATIC_HOSTNAME` or something like this depending on what gets finalized with borg as well) and use this instead of `hostname` wherever relevant. Additionally if the `BORG_HOSTNAME` (or equivalent env var) is NOT set, then prior to invoking borg, borgmatic should duplicate the `BORGMATIC_HOSTNAME` variable into `BORG_HOSTNAME` (or equivalent) OR explicitly invoke borg via the `--hostname` (or whatever final option is implemented). It will also be useful if this field/value can be used by hooks/scripts etc. when called via borgmatic. NOTE: This requires `borg` support which as of creating this issue is still open (for 1.4.x but completed for 2.x).
Owner

Seems handy. This is implemented now in main via --archive-hostname and an archive_hostname option as well. I didn't go with --hostname/hostname because that would collide with the database flags/options. Tested with Borg 2.x, but let me know if the 1.4.5 feature ends up not using a Borg flag named --hostname (which is what borgmatic passes in).

If any environment variable is used, borgmatic doesn't touch it, and it'll get passed through to Borg directly.

Seems handy. This is implemented now in main via `--archive-hostname` and an `archive_hostname` option as well. I didn't go with `--hostname`/`hostname` because that would collide with the database flags/options. Tested with Borg 2.x, but let me know if the 1.4.5 feature ends up not using a Borg flag named `--hostname` (which is what borgmatic passes in). If any environment variable is used, borgmatic doesn't touch it, and it'll get passed through to Borg directly.
Author

This is also particularly useful for prune but it looks like this is currently only implemented for create.

I think in general, this option could be useful in some other scenarios as well - essentially when borgmatic is being run on a host as "proxy" for another host to perform some operations (like prune, delete, archive checks, maybe even for a spot check in some cases).

This option should replace {hostname} everywhere - not just for create.

Could you also support an environment variable override for this? This will allow config file and script reuse with simple environment overrides (and may be specially useful for container setups).

This is also particularly useful for prune but it looks like this is currently only implemented for create. I think in general, this option could be useful in some other scenarios as well - essentially when borgmatic is being run on a host as "proxy" for another host to perform some operations (like prune, delete, archive checks, maybe even for a spot check in some cases). This option should replace `{hostname}` _everywhere_ - not just for create. Could you also support an environment variable override for this? This will allow config file and script reuse with simple environment overrides (and may be specially useful for container setups).
Owner

Oh, good catch. I'll have a look at the other actions.

And I think I'd prefer not to add a one-off environment variable for this one option, but (at the risk of vast overengineering) I'd be happy to look at making all borgmatic options available as environment variables in the same way that all options are now available as command-line flags. Maybe as a separate ticket?

I will say that if Borg exposes this as an environment variable, then borgmatic shouldn't have to do anything to support that.

Oh, good catch. I'll have a look at the other actions. And I think I'd prefer not to add a one-off environment variable for this one option, but (at the risk of vast overengineering) I'd be happy to look at making all borgmatic options available as environment variables in the same way that all options are now available as command-line flags. Maybe as a separate ticket? I will say that if *Borg* exposes this as an environment variable, then borgmatic shouldn't have to do anything to support that.
Author

@witten wrote in #1317 (comment):

And I think I'd prefer not to add a one-off environment variable for this one option, but (at the risk of vast overengineering) I'd be happy to look at making all borgmatic options available as environment variables in the same way that all options are now available as command-line flags. Maybe as a separate ticket?

(IMHO) definitely overkill and I don't think it is needed for now.

I will say that if Borg exposes this as an environment variable, then borgmatic shouldn't have to do anything to support that.

Agreed - that will allow borgmatic to be pretty much a full "pass through" wrapper for borg (probably eliminating the need to use the --archive-hostname via borgmatic for most scenarios if one goes with the environment variable option).

@witten wrote in https://projects.torsion.org/borgmatic-collective/borgmatic/issues/1317#issuecomment-14222: > And I think I'd prefer not to add a one-off environment variable for this one option, but (at the risk of vast overengineering) I'd be happy to look at making all borgmatic options available as environment variables in the same way that all options are now available as command-line flags. Maybe as a separate ticket? (IMHO) definitely overkill and I don't think it is needed for now. > I will say that if _Borg_ exposes this as an environment variable, then borgmatic shouldn't have to do anything to support that. Agreed - that will allow borgmatic to be pretty much a full "pass through" wrapper for borg (probably eliminating the need to use the `--archive-hostname` via borgmatic for most scenarios if one goes with the environment variable option).
Owner

Done! (Again.)

Done! (Again.)
Author

@witten - just a heads up that https://github.com/borgbackup/borg/issues/9651 is still work in progress but with an ongoing debate on whether to make the hostname override purely ENV VAR based or also support cmd line flags (for which you've added support).

I will update this issue once the final decision has been taken. Depending on the outcome there maybe changes and/or a rollback on the options required.

Sorry for the extra work in case that happens.

@witten - just a heads up that https://github.com/borgbackup/borg/issues/9651 is still work in progress but with an ongoing debate on whether to make the hostname override purely ENV VAR based or also support cmd line flags (for which you've added support). I will update this issue once the final decision has been taken. Depending on the outcome there maybe changes and/or a rollback on the options required. Sorry for the extra work in case that happens.
Owner

Thanks for the heads up. I'll reopen this ticket for now!

Thanks for the heads up. I'll reopen this ticket for now!
witten reopened this issue 2026-06-29 17:52:36 +00:00
Author

@witten - for now there is no plan to implement command line args/options for the hostname and username overrides in borg. As a result, the only way to override hostname/username is via environment variables.

I assume that for borgmatic most of these place holders are pass through to borg and so borgmatic shouldn't care. In case there are scenarios where borgmatic needs to use a custom hostname (maybe for a spotcheck?), it would be best if borgmatic also uses the the BORG_HOSTNAME (and BORG_USERNAME) override environment variables (rather than using/expecting a separate borgmatic specific variable).

@witten - for now there is no plan to implement command line args/options for the hostname and username overrides in borg. As a result, the only way to override hostname/username is via environment variables. I assume that for borgmatic most of these place holders are pass through to borg and so borgmatic shouldn't care. In case there are scenarios where borgmatic needs to use a custom hostname (maybe for a spotcheck?), it would be best if borgmatic also uses the the BORG_HOSTNAME (and BORG_USERNAME) override environment variables (rather than using/expecting a separate borgmatic specific variable).
Author

@witten - Borg 1.4.5 has been released. Only environment variable support to override hostname and username (via BORG_HOSTNAME and BORG_USERNAME).

Could you please make appropriate changes in borgmatic for this (if required)?

@witten - Borg 1.4.5 has been released. Only environment variable support to override hostname and username (via BORG_HOSTNAME and BORG_USERNAME). Could you please make appropriate changes in borgmatic for this (if required)?
Owner

Thanks for the update. This feature now passes the archive_hostname option (or --archive-hostname flag) to Borg via environment variable. It also passes BORG_HOSTNAME through to Borg if archive_hostname is not set and the environment variable is. It does not however read BORG_HOSTNAME as input to borgmatic's own config. I'd be happy to implement a general mechanism for making all borgmatic config settable via environment variable (in a separate ticket), but I'd rather not implement a one-off for that.

I also added a corresponding archive_username option / --archive-username flag for the {user} placeholder.

All of this will be part of the next release.

Thanks for the update. This feature now passes the `archive_hostname` option (or `--archive-hostname` flag) to Borg via environment variable. It also passes `BORG_HOSTNAME` through to Borg if `archive_hostname` is not set and the environment variable is. It does not however read `BORG_HOSTNAME` as input to borgmatic's own config. I'd be happy to implement a general mechanism for making all borgmatic config settable via environment variable (in a separate ticket), but I'd rather not implement a one-off for that. I also added a corresponding `archive_username` option / `--archive-username` flag for the `{user}` placeholder. All of this will be part of the next release.
Author

Couple of points:

  1. In borgmatic/config/schema.yaml it would be more appropriate to have example: borgmatic_user or example: backup_user instead of example: example.org for the archive_username section.
  2. Is it accurate to say that borgmatic itself doesn't use {username} or {hostname} internally - both are place holders purely for borg and as long as the BORG_HOSTNAME and BORG_USERNAME environment variables are set, borg should behave correctly in case of overrides?
Couple of points: 1. In `borgmatic/config/schema.yaml` it would be more appropriate to have `example: borgmatic_user` or `example: backup_user` instead of `example: example.org` for the `archive_username` section. 2. Is it accurate to say that borgmatic itself doesn't use `{username}` or `{hostname}` internally - both are place holders purely for borg and as long as the `BORG_HOSTNAME` and `BORG_USERNAME` environment variables are set, borg should behave correctly in case of overrides?
Owner

@quietquark wrote in #1317 (comment):

Couple of points:

1. In `borgmatic/config/schema.yaml` it would be more appropriate to have `example: borgmatic_user` or `example: backup_user` instead of `example: example.org` for the `archive_username` section.

Thanks, fixed!

2. Is it accurate to say that borgmatic itself doesn't use `{username}` or `{hostname}` internally - both are place holders purely for borg and as long as the `BORG_HOSTNAME` and `BORG_USERNAME` environment variables are set, borg should behave correctly in case of overrides?

Yes—unless you create constants with those names.

@quietquark wrote in https://projects.torsion.org/borgmatic-collective/borgmatic/issues/1317#issuecomment-14309: > Couple of points: > > 1. In `borgmatic/config/schema.yaml` it would be more appropriate to have `example: borgmatic_user` or `example: backup_user` instead of `example: example.org` for the `archive_username` section. Thanks, fixed! > 2. Is it accurate to say that borgmatic itself doesn't use `{username}` or `{hostname}` internally - both are place holders purely for borg and as long as the `BORG_HOSTNAME` and `BORG_USERNAME` environment variables are set, borg should behave correctly in case of overrides? Yes—unless you create [constants](https://torsion.org/borgmatic/reference/configuration/constants/) with those names.
Owner

Released in borgmatic 2.1.7!

Released in borgmatic 2.1.7!
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#1317
No description provided.