Skip to content

Commit 35f6080

Browse files
replace create --hostname/--username with BORG_HOSTNAME/BORG_USERNAME env vars, fixes #9651
The --hostname/--username options on borg create (added in 2.0.0b21, #9402) are removed in favor of the BORG_HOSTNAME / BORG_USERNAME environment variables. When set, these override the hostname/username stored in newly created archives and used by the {hostname}/{user} placeholders (so they also apply to archive names, prune --glob-archives, check, etc.). Useful to run borg on host A but impersonate host B. The env vars are read at the point of use; fqdn/hostid (see BORG_HOST_ID) and the auto-detection are intentionally left untouched. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent e57e22d commit 35f6080

7 files changed

Lines changed: 22 additions & 37 deletions

File tree

docs/changes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ New features:
201201
- prune: show total vs matching archives in output, #9262
202202
- prune: add --json option, #9222
203203
- archive: preserve cwd archive metadata, #9495
204+
- create: replace the --hostname/--username options (added in 2.0.0b21) with the
205+
BORG_HOSTNAME and BORG_USERNAME env vars. When set, they override the hostname/username
206+
stored in newly created archives and used by the {hostname}/{user} placeholders, #9651
204207

205208
Fixes:
206209

docs/usage/create.rst.inc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,6 @@ borg create
107107
+-------------------------------------------------------+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
108108
| | ``-C COMPRESSION``, ``--compression COMPRESSION`` | select compression algorithm, see the output of the "borg help compression" command for details. |
109109
+-------------------------------------------------------+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
110-
| | ``--hostname HOSTNAME`` | explicitly set hostname for the archive |
111-
+-------------------------------------------------------+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
112-
| | ``--username USERNAME`` | explicitly set username for the archive |
113-
+-------------------------------------------------------+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
114110
| | ``--tags TAG`` | add tags to archive (comma-separated or multiple arguments) |
115111
+-------------------------------------------------------+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
116112
@@ -181,8 +177,6 @@ borg create
181177
--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.
182178
--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
183179
-C COMPRESSION, --compression COMPRESSION select compression algorithm, see the output of the "borg help compression" command for details.
184-
--hostname HOSTNAME explicitly set hostname for the archive
185-
--username USERNAME explicitly set username for the archive
186180
--tags TAG add tags to archive (comma-separated or multiple arguments)
187181
188182

docs/usage/general/environment.rst.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ General:
4949
So, if you have an all-zero MAC address or other reasons to better control the host id externally, just set this
5050
environment variable to a unique value. If all your FQDNs are unique, you can just use the FQDN. If not,
5151
use FQDN@uniqueid.
52+
BORG_HOSTNAME
53+
When set, use this value as the hostname (instead of the auto-detected one), e.g. to run borg
54+
on one host, but impersonate another host. This affects the hostname stored in newly created
55+
archives as well as the ``{hostname}`` placeholder.
56+
BORG_USERNAME
57+
When set, use this value as the username (instead of the auto-detected one), e.g. to run borg
58+
as one user, but impersonate another user. This affects the username stored in newly created
59+
archives as well as the ``{user}`` placeholder.
5260
BORG_LOCK_WAIT
5361
You can set the default value for the ``--lock-wait`` option with this, so
5462
you do not need to give it as a command line option.

src/borg/archive.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,6 @@ def __init__(
475475
log_json=False,
476476
iec=False,
477477
deleted=False,
478-
hostname=None,
479-
username=None,
480478
):
481479
name_is_id = isinstance(name, bytes)
482480
if not name_is_id:
@@ -495,8 +493,6 @@ def __init__(
495493
self.name_in_manifest = name # can differ from .name later (if borg check fixed duplicate archive names)
496494
self.comment = None
497495
self.tags = None
498-
self.hostname = hostname if hostname is not None else platform.hostname
499-
self.username = username if username is not None else getuser()
500496
self.numeric_ids = numeric_ids
501497
self.noatime = noatime
502498
self.noctime = noctime
@@ -658,8 +654,8 @@ def save(self, name=None, comment=None, timestamp=None, stats=None, additional_m
658654
"item_ptrs": item_ptrs, # see #1473
659655
"command_line": join_cmd(sys.argv),
660656
"cwd": self.cwd,
661-
"hostname": self.hostname,
662-
"username": self.username,
657+
"hostname": os.environ.get("BORG_HOSTNAME") or platform.hostname,
658+
"username": os.environ.get("BORG_USERNAME") or getuser(),
663659
"time": nominal.isoformat(timespec="microseconds"),
664660
"start": start.isoformat(timespec="microseconds"),
665661
"end": end.isoformat(timespec="microseconds"),

src/borg/archiver/create_cmd.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,6 @@ def create_inner(archive, cache, fso):
252252
start=t0,
253253
log_json=args.log_json,
254254
iec=args.iec,
255-
hostname=args.hostname,
256-
username=args.username,
257255
)
258256
metadata_collector = MetadataCollector(
259257
noatime=not args.atime,
@@ -1013,24 +1011,6 @@ def build_parser_create(self, subparsers, common_parser, mid_common_parser):
10131011
action=Highlander,
10141012
help="select compression algorithm, see the output of the " '"borg help compression" command for details.',
10151013
)
1016-
archive_group.add_argument(
1017-
"--hostname",
1018-
metavar="HOSTNAME",
1019-
dest="hostname",
1020-
type=str,
1021-
default=None,
1022-
action=Highlander,
1023-
help="explicitly set hostname for the archive",
1024-
)
1025-
archive_group.add_argument(
1026-
"--username",
1027-
metavar="USERNAME",
1028-
dest="username",
1029-
type=str,
1030-
default=None,
1031-
action=Highlander,
1032-
help="explicitly set username for the archive",
1033-
)
10341014
archive_group.add_argument(
10351015
"--tags",
10361016
metavar="TAG",

src/borg/helpers/parseformat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,11 @@ def _replace_placeholders(text, overrides={}):
403403
"pid": os.getpid(),
404404
"fqdn": fqdn,
405405
"reverse-fqdn": ".".join(reversed(fqdn.split("."))),
406-
"hostname": hostname,
406+
"hostname": os.environ.get("BORG_HOSTNAME") or hostname,
407407
"now": DatetimeWrapper(current_time.astimezone()),
408408
"utcnow": DatetimeWrapper(current_time),
409409
"unixtime": int(current_time.timestamp()),
410-
"user": getosusername(),
410+
"user": os.environ.get("BORG_USERNAME") or getosusername(),
411411
"uuid4": str(uuid.uuid4()),
412412
"borgversion": borg_version,
413413
"borgmajor": "%d" % borg_version_tuple[:1],

src/borg/testsuite/archiver/create_cmd_test.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -887,13 +887,17 @@ def test_create_json(archivers, request):
887887
assert "stats" in archive
888888

889889

890-
def test_explicit_hostname_and_username(archivers, request):
890+
def test_hostname_and_username_override(archivers, request, monkeypatch):
891891
archiver = request.getfixturevalue(archivers)
892892
create_regular_file(archiver.input_path, "file1", size=1024 * 80)
893893
cmd(archiver, "repo-create", RK_ENCRYPTION)
894-
cmd(archiver, "create", "--hostname", "foo_host", "--username", "bar_user", "test", "input")
895-
info = json.loads(cmd(archiver, "info", "--json", "test"))
894+
monkeypatch.setenv("BORG_HOSTNAME", "foo_host")
895+
monkeypatch.setenv("BORG_USERNAME", "bar_user")
896+
# the override is also used to fill the {hostname}/{user} placeholders in the archive name:
897+
cmd(archiver, "create", "{hostname}-{user}", "input")
898+
info = json.loads(cmd(archiver, "info", "--json", "foo_host-bar_user"))
896899
archive = info["archives"][0]
900+
assert archive["name"] == "foo_host-bar_user"
897901
assert archive["hostname"] == "foo_host"
898902
assert archive["username"] == "bar_user"
899903

0 commit comments

Comments
 (0)