[PATCH v3 5/5] fix #5340: client: repository: add PBS_NAMESPACE environment variable

Thomas Lamprecht
2026-04-01T22:53:04Z

Add PBS_NAMESPACE as a fallback when --ns is not given on the CLI,
addressing the request in bug #5340.

The env var is supported uniformly across all client tools
(proxmox-backup-client, proxmox-file-restore, proxmox-backup-debug).
Like the other PBS_* atom env vars, it provides a default that can be
overridden by the corresponding CLI option.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
---

no real changes since v2, only adapt to context changes.

docs/backup-client.rst | 3 +++
pbs-client/src/tools/mod.rs | 2 ++
proxmox-backup-client/src/main.rs | 13 +++++++++----
proxmox-file-restore/src/main.rs | 26 ++++++++++++++++----------
src/bin/proxmox_backup_debug/diff.rs | 13 ++++++++-----
5 files changed, 38 insertions(+), 19 deletions(-)

diff --git a/docs/backup-client.rst b/docs/backup-client.rst
index 1d464a007..e2a05d4fc 100644
--- a/docs/backup-client.rst
+++ b/docs/backup-client.rst
@@ -131,6 +131,9 @@ Environment Variables
Authentication identity (``user@realm`` or ``user@realm!tokenname``).
Defaults to ``root@pam`` if unset.

+``PBS_NAMESPACE``
+ Backup namespace. Used as a fallback when ``--ns`` is not given.
+
``PBS_PASSWORD``
When set, this value is used as the password for the backup server.
You can also set this to an API token secret.
diff --git a/pbs-client/src/tools/mod.rs b/pbs-client/src/tools/mod.rs
index 32c55ee1b..634a0d4e5 100644
--- a/pbs-client/src/tools/mod.rs
+++ b/pbs-client/src/tools/mod.rs
@@ -36,6 +36,7 @@ const ENV_VAR_PBS_SERVER: &str = "PBS_SERVER";
const ENV_VAR_PBS_PORT: &str = "PBS_PORT";
const ENV_VAR_PBS_DATASTORE: &str = "PBS_DATASTORE";
const ENV_VAR_PBS_AUTH_ID: &str = "PBS_AUTH_ID";
+pub const ENV_VAR_PBS_NAMESPACE: &str = "PBS_NAMESPACE";

/// Directory with system [credential]s. See systemd-creds(1).
///
@@ -841,6 +842,7 @@ mod tests {
ENV_VAR_PBS_PORT,
ENV_VAR_PBS_DATASTORE,
ENV_VAR_PBS_AUTH_ID,
+ ENV_VAR_PBS_NAMESPACE,
ENV_VAR_CREDENTIALS_DIRECTORY,
];

diff --git a/proxmox-backup-client/src/main.rs b/proxmox-backup-client/src/main.rs
index b7376e58c..cd7291428 100644
--- a/proxmox-backup-client/src/main.rs
+++ b/proxmox-backup-client/src/main.rs
@@ -330,11 +330,16 @@ async fn backup_image<P: AsRef<Path>>(
}

pub fn optional_ns_param(param: &Value) -> Result<BackupNamespace, Error> {
- Ok(match param.get("ns") {
- Some(Value::String(ns)) => ns.parse()?,
+ match param.get("ns") {
+ Some(Value::String(ns)) => return ns.parse().map_err(Error::from),
Some(_) => bail!("invalid namespace parameter"),
- None => BackupNamespace::root(),
- })
+ None => {}
+ }
+ // Fall back to PBS_NAMESPACE environment variable.
+ if let Ok(ns) = std::env::var(pbs_client::tools::ENV_VAR_PBS_NAMESPACE) {
+ return ns.parse().map_err(Error::from);
+ }
+ Ok(BackupNamespace::root())
}

#[api(
diff --git a/proxmox-file-restore/src/main.rs b/proxmox-file-restore/src/main.rs
index 0de56a7d6..f7885c874 100644
--- a/proxmox-file-restore/src/main.rs
+++ b/proxmox-file-restore/src/main.rs
@@ -274,11 +274,14 @@ async fn list(
param: Value,
) -> Result<(), Error> {
let repo = extract_repository_from_value(&param)?;
- let ns: BackupNamespace = param["ns"]
- .as_str()
- .map(|s| s.parse())
- .transpose()?
- .unwrap_or_default();
+ let ns: BackupNamespace = match param["ns"].as_str() {
+ Some(s) => s.parse()?,
+ None => std::env::var(pbs_client::tools::ENV_VAR_PBS_NAMESPACE)
+ .ok()
+ .map(|s| s.parse())
+ .transpose()?
+ .unwrap_or_default(),
+ };
let snapshot: BackupDir = snapshot.parse()?;
let path = parse_path(path, base64)?;

@@ -429,11 +432,14 @@ async fn extract(
param: Value,
) -> Result<(), Error> {
let repo = extract_repository_from_value(&param)?;
- let namespace: BackupNamespace = param["ns"]
- .as_str()
- .map(|s| s.parse())
- .transpose()?
- .unwrap_or_default();
+ let namespace: BackupNamespace = match param["ns"].as_str() {
+ Some(s) => s.parse()?,
+ None => std::env::var(pbs_client::tools::ENV_VAR_PBS_NAMESPACE)
+ .ok()
+ .map(|s| s.parse())
+ .transpose()?
+ .unwrap_or_default(),
+ };
let snapshot: BackupDir = snapshot.parse()?;
let orig_path = path;
let path = parse_path(orig_path.clone(), base64)?;
diff --git a/src/bin/proxmox_backup_debug/diff.rs b/src/bin/proxmox_backup_debug/diff.rs
index 88b4199f7..d42768aa7 100644
--- a/src/bin/proxmox_backup_debug/diff.rs
+++ b/src/bin/proxmox_backup_debug/diff.rs
@@ -108,11 +108,14 @@ async fn diff_archive_cmd(
let repo = extract_repository_from_value(&param)?;

let color = color.unwrap_or_default();
- let namespace: BackupNamespace = param["ns"]
- .as_str()
- .map(|s| s.parse())
- .transpose()?
- .unwrap_or_default();
+ let namespace: BackupNamespace = match param["ns"].as_str() {
+ Some(s) => s.parse()?,
+ None => std::env::var(pbs_client::tools::ENV_VAR_PBS_NAMESPACE)
+ .ok()
+ .map(|s| s.parse())
+ .transpose()?
+ .unwrap_or_default(),
+ };

let crypto = crypto_parameters(&param)?;

--
2.47.3