From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [PATCH 6/7] client: job: add battery-check example pre-run hook
Date: Wed, 14 Oct 2026 12:51:35 +0200 [thread overview]
Message-ID: <20261014105208.2747208-7-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20261014105208.2747208-1-t.lamprecht@proxmox.com>
Skip the run below 30% charge on battery, or below 10% on AC, where the
load of a backup would slow down charging a nearly empty battery. The
average over all batteries counts, as laptops with two batteries drain
one first, and every unclear case proceeds, since a hook that silently
skips forever is worse than one extra backup.
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
---
debian/proxmox-backup-client-static.examples | 1 +
debian/proxmox-backup-client.examples | 1 +
examples/pbs-check-battery.sh | 47 ++++++++++++++++++++
3 files changed, 49 insertions(+)
create mode 100644 debian/proxmox-backup-client-static.examples
create mode 100644 debian/proxmox-backup-client.examples
create mode 100755 examples/pbs-check-battery.sh
diff --git a/debian/proxmox-backup-client-static.examples b/debian/proxmox-backup-client-static.examples
new file mode 100644
index 000000000..b28044ebd
--- /dev/null
+++ b/debian/proxmox-backup-client-static.examples
@@ -0,0 +1 @@
+examples/pbs-check-battery.sh
diff --git a/debian/proxmox-backup-client.examples b/debian/proxmox-backup-client.examples
new file mode 100644
index 000000000..b28044ebd
--- /dev/null
+++ b/debian/proxmox-backup-client.examples
@@ -0,0 +1 @@
+examples/pbs-check-battery.sh
diff --git a/examples/pbs-check-battery.sh b/examples/pbs-check-battery.sh
new file mode 100755
index 000000000..d7779cb04
--- /dev/null
+++ b/examples/pbs-check-battery.sh
@@ -0,0 +1,47 @@
+#!/bin/bash
+# Pre-run hook for proxmox-backup-client scheduled backups. Skips the backup below 30% battery
+# while discharging, or below 10% on AC, where the load of a backup would slow down charging.
+#
+# Exit 0 proceeds with the backup, exit 1 skips this run, and anything else fails the job, so
+# proceed when in doubt, for example on a system without a battery.
+
+set -euo pipefail
+
+SUPPLY_PATH="/sys/class/power_supply"
+
+capacity_sum=0
+batteries=0
+status="Unknown"
+
+# batteries are found by their type, as their names depend on the driver
+for bat in "$SUPPLY_PATH"/*; do
+ [ "$(cat "$bat/type" 2>/dev/null)" = "Battery" ] || continue
+ # an empty bay is listed as well
+ [ "$(cat "$bat/present" 2>/dev/null || echo 1)" = 1 ] || continue
+ capacity=$(cat "$bat/capacity" 2>/dev/null) || continue
+ case "$capacity" in
+ ''|*[!0-9]*) continue ;;
+ esac
+
+ capacity_sum=$((capacity_sum + capacity))
+ batteries=$((batteries + 1))
+ if [ "$(cat "$bat/status" 2>/dev/null)" = "Discharging" ]; then
+ status="Discharging"
+ fi
+done
+
+# no battery, so a desktop or a server
+[ "$batteries" -gt 0 ] || exit 0
+
+# the average of all batteries, as laptops with two of them often drain one first
+charge=$((capacity_sum / batteries))
+if [ "$status" = "Discharging" ]; then
+ threshold=30
+else
+ threshold=10
+fi
+
+if [ "$charge" -lt "$threshold" ]; then
+ echo "battery at ${charge}%, below the ${threshold}% threshold, skipping backup" >&2
+ exit 1
+fi
--
2.47.3
next prev parent reply other threads:[~2026-10-21 20:55 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-14 10:51 [PATCH 0/7] client: add scheduled backup jobs Thomas Lamprecht
2026-10-14 10:51 ` [PATCH 1/7] client: backup specification: widen BackupDetectionMode derives Thomas Lamprecht
2026-10-14 10:51 ` [PATCH 2/7] client: job: add backup job config and state types Thomas Lamprecht
2026-10-14 10:51 ` [PATCH 3/7] client: job: add job runner and scheduled backup daemon Thomas Lamprecht
2026-10-14 10:51 ` [PATCH 4/7] client: job: add job management CLI subcommands Thomas Lamprecht
2026-10-14 10:51 ` [PATCH 5/7] client: job: ship systemd user unit for the scheduler Thomas Lamprecht
2026-10-14 10:51 ` Thomas Lamprecht [this message]
2026-10-14 10:51 ` [PATCH 7/7] docs: client: document scheduled backup jobs Thomas Lamprecht
2026-10-22 12:58 ` [PATCH 0/7] client: add " Christian Ebner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261014105208.2747208-7-t.lamprecht@proxmox.com \
--to=t.lamprecht@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.