Merge pull request #9811 from ThomasWaldmann/env-hostname-username-master

Replace create --hostname/--username with BORG_HOSTNAME/BORG_USERNAME env vars, fixes #9651
This commit is contained in:
TW 2026-07-05 14:56:52 +02:00 committed by GitHub
commit fc8c8e0fe9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 22 additions and 37 deletions

View file

@ -207,6 +207,9 @@ New features:
- add --json option, #9222
- add blake3 (super-fast hash/keyed MAC), replacing blake2b for new repos
- archive: preserve cwd archive metadata, #9495
- create: replace the --hostname/--username options (added in 2.0.0b21) with the
BORG_HOSTNAME and BORG_USERNAME env vars. When set, they override the hostname/username
stored in newly created archives and used by the {hostname}/{user} placeholders, #9651
Fixes:

View file

@ -107,10 +107,6 @@ borg create
+-------------------------------------------------------+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| | ``-C COMPRESSION``, ``--compression COMPRESSION`` | select compression algorithm, see the output of the "borg help compression" command for details. |
+-------------------------------------------------------+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| | ``--hostname HOSTNAME`` | explicitly set hostname for the archive |
+-------------------------------------------------------+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| | ``--username USERNAME`` | explicitly set username for the archive |
+-------------------------------------------------------+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| | ``--tags TAG`` | add tags to archive (comma-separated or multiple arguments) |
+-------------------------------------------------------+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
@ -181,8 +177,6 @@ borg create
--timestamp TIMESTAMP manually specify the archive creation date/time (yyyy-mm-ddThh:mm:ss[(+|-)HH:MM] format, (+|-)HH:MM is the UTC offset, default: local time zone). Alternatively, give a reference file/directory.
--chunker-params PARAMS specify the chunker parameters (ALGO, CHUNK_MIN_EXP, CHUNK_MAX_EXP, HASH_MASK_BITS, HASH_WINDOW_SIZE). default: buzhash,19,23,21,4095
-C COMPRESSION, --compression COMPRESSION select compression algorithm, see the output of the "borg help compression" command for details.
--hostname HOSTNAME explicitly set hostname for the archive
--username USERNAME explicitly set username for the archive
--tags TAG add tags to archive (comma-separated or multiple arguments)

View file

@ -49,6 +49,14 @@ General:
So, if you have an all-zero MAC address or other reasons to better control the host id externally, just set this
environment variable to a unique value. If all your FQDNs are unique, you can just use the FQDN. If not,
use FQDN@uniqueid.
BORG_HOSTNAME
When set, use this value as the hostname (instead of the auto-detected one), e.g. to run borg
on one host, but impersonate another host. This affects the hostname stored in newly created
archives as well as the ``{hostname}`` placeholder.
BORG_USERNAME
When set, use this value as the username (instead of the auto-detected one), e.g. to run borg
as one user, but impersonate another user. This affects the username stored in newly created
archives as well as the ``{user}`` placeholder.
BORG_LOCK_WAIT
You can set the default value for the ``--lock-wait`` option with this, so
you do not need to give it as a command line option.

View file

@ -474,8 +474,6 @@ class Archive:
log_json=False,
iec=False,
deleted=False,
hostname=None,
username=None,
):
name_is_id = isinstance(name, bytes)
if not name_is_id:
@ -494,8 +492,6 @@ class Archive:
self.name_in_manifest = name # can differ from .name later (if borg check fixed duplicate archive names)
self.comment = None
self.tags = None
self.hostname = hostname if hostname is not None else platform.hostname
self.username = username if username is not None else getuser()
self.numeric_ids = numeric_ids
self.noatime = noatime
self.noctime = noctime
@ -657,8 +653,8 @@ Duration: {0.duration}
"item_ptrs": item_ptrs, # see #1473
"command_line": join_cmd(sys.argv),
"cwd": self.cwd,
"hostname": self.hostname,
"username": self.username,
"hostname": os.environ.get("BORG_HOSTNAME") or platform.hostname,
"username": os.environ.get("BORG_USERNAME") or getuser(),
"time": nominal.isoformat(timespec="microseconds"),
"start": start.isoformat(timespec="microseconds"),
"end": end.isoformat(timespec="microseconds"),

View file

@ -252,8 +252,6 @@ class CreateMixIn:
start=t0,
log_json=args.log_json,
iec=args.iec,
hostname=args.hostname,
username=args.username,
)
metadata_collector = MetadataCollector(
noatime=not args.atime,
@ -1013,24 +1011,6 @@ class CreateMixIn:
action=Highlander,
help="select compression algorithm, see the output of the " '"borg help compression" command for details.',
)
archive_group.add_argument(
"--hostname",
metavar="HOSTNAME",
dest="hostname",
type=str,
default=None,
action=Highlander,
help="explicitly set hostname for the archive",
)
archive_group.add_argument(
"--username",
metavar="USERNAME",
dest="username",
type=str,
default=None,
action=Highlander,
help="explicitly set username for the archive",
)
archive_group.add_argument(
"--tags",
metavar="TAG",

View file

@ -447,11 +447,11 @@ def _replace_placeholders(text, overrides={}):
"pid": os.getpid(),
"fqdn": fqdn,
"reverse-fqdn": ".".join(reversed(fqdn.split("."))),
"hostname": hostname,
"hostname": os.environ.get("BORG_HOSTNAME") or hostname,
"now": DatetimeWrapper(current_time.astimezone()),
"utcnow": DatetimeWrapper(current_time),
"unixtime": int(current_time.timestamp()),
"user": getosusername(),
"user": os.environ.get("BORG_USERNAME") or getosusername(),
"uuid4": str(uuid.uuid4()),
"borgversion": borg_version,
"borgmajor": "%d" % borg_version_tuple[:1],

View file

@ -887,13 +887,17 @@ def test_create_json(archivers, request):
assert "stats" in archive
def test_explicit_hostname_and_username(archivers, request):
def test_hostname_and_username_override(archivers, request, monkeypatch):
archiver = request.getfixturevalue(archivers)
create_regular_file(archiver.input_path, "file1", size=1024 * 80)
cmd(archiver, "repo-create", RK_ENCRYPTION)
cmd(archiver, "create", "--hostname", "foo_host", "--username", "bar_user", "test", "input")
info = json.loads(cmd(archiver, "info", "--json", "test"))
monkeypatch.setenv("BORG_HOSTNAME", "foo_host")
monkeypatch.setenv("BORG_USERNAME", "bar_user")
# the override is also used to fill the {hostname}/{user} placeholders in the archive name:
cmd(archiver, "create", "{hostname}-{user}", "input")
info = json.loads(cmd(archiver, "info", "--json", "foo_host-bar_user"))
archive = info["archives"][0]
assert archive["name"] == "foo_host-bar_user"
assert archive["hostname"] == "foo_host"
assert archive["username"] == "bar_user"