From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 8778D1FF0C0 for ; Wed, 21 Oct 2026 22:55:48 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id E36B5214F0; Wed, 21 Oct 2026 22:55:46 +0200 (CEST) From: Thomas Lamprecht 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 Message-ID: <20261014105208.2747208-7-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261014105208.2747208-1-t.lamprecht@proxmox.com> References: <20261014105208.2747208-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1792616142307 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.554 Adjusted score from AWL reputation of From: address DATE_IN_PAST_96_XX 2.07 Date: is 96 hours or more before Received: date DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 6PH3G7U5ZFRZNRWPCDWWE5GHRH2SMPFC X-Message-ID-Hash: 6PH3G7U5ZFRZNRWPCDWWE5GHRH2SMPFC X-MailFrom: t.lamprecht@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox Backup Server development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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