LibrePortal/scripts/crontab/app/crontab_backup_scheduler.sh
librelad 6bb04533fa fix(desudo): manager->self sudo drops -> runAsManager (scoped-sudoers safe)
The scoped sudoers grants the manager (root) and (dockerinstall) but NOT
(itself), so the many 'sudo -u $sudo_user_name <cmd>' calls (crontab,
git/update, reinstall, swapfile, …) failed with 'a password is required'
once per CLI command. runAsManager runs the command plainly when already
the manager (the runtime case) and only sudo -u's when root (install
time), so it's correct in both contexts and needs no sudoers self-grant.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Signed-off-by: librelad <librelad@digitalangels.vip>
2026-05-24 18:40:19 +01:00

32 lines
1.4 KiB
Bash

#!/bin/bash
# Install (or refresh) the single daily crontab entry that drives application
# backups. The entry runs `libreportal backup scheduled`, which enqueues a
# backup task per enabled app for the processor to drain serially.
crontabSetupBackupScheduler()
{
local ISCRON=$( (runAsManager crontab -l) 2>/dev/null )
if [[ "$ISCRON" == *"command not found"* ]]; then
isNotice "Crontab is not found. Unable to set up the backup scheduler."
return 0
fi
if ! runAsManager crontab -l 2>/dev/null | grep -q "cron is set up for $sudo_user_name"; then
isNotice "Crontab is not set up, skipping backup scheduler until it's found."
return 0
fi
local marker="# CRONTAB BACKUP SCHEDULER"
local scheduler_entry="$CFG_BACKUP_CRONTAB_APP libreportal backup scheduled $marker"
# Drop any previous scheduler entry, then re-add the current one so a
# changed schedule (CFG_BACKUP_CRONTAB_APP) always takes effect.
local result=$(runAsManager crontab -l 2>/dev/null | grep -v "$marker" | runAsManager crontab -)
local result=$( (runAsManager crontab -l 2>/dev/null; echo "$scheduler_entry") | runAsManager crontab - )
checkSuccess "Installing the daily backup scheduler entry"
local schedule_time=$(echo "$CFG_BACKUP_CRONTAB_APP" | cut -d' ' -f2)
isSuccessful "Enabled apps will be queued for backup daily at ${schedule_time}:00"
}