Skip to content

Instantly share code, notes, and snippets.

@zinntikumugai
Created February 18, 2026 14:24
Show Gist options
  • Select an option

  • Save zinntikumugai/7942c86918d39ce6ca2d8afd5d03d6ad to your computer and use it in GitHub Desktop.

Select an option

Save zinntikumugai/7942c86918d39ce6ca2d8afd5d03d6ad to your computer and use it in GitHub Desktop.
#!/bin/bash
# バックアップ中の unattended-upgrade 抑止制御
# Usage: bareos-apt-inhibit.sh lock|unlock
LOCKDIR="/run/bareos-apt-inhibit"
LOCKFILE="${LOCKDIR}/lock"
COUNTFILE="${LOCKDIR}/count"
mkdir -p "$LOCKDIR"
case "$1" in
lock)
(
flock -w 10 200 || exit 1
count=$(cat "$COUNTFILE" 2>/dev/null || echo 0)
if [ "$count" -eq 0 ]; then
echo "$(date): Inhibiting apt-daily-upgrade (JobId=${2:-unknown})" >> /var/log/bareos/apt-inhibit.log
# 実行中のunattended-upgradeを待つ(最大5分)
timeout 300 bash -c 'while systemctl is-active --quiet apt-daily-upgrade.service; do sleep 5; done' 2>/dev/null
systemctl mask --now apt-daily-upgrade.service apt-daily-upgrade.timer
fi
echo $((count + 1)) > "$COUNTFILE"
) 200>"$LOCKFILE"
;;
unlock)
(
flock -w 10 200 || exit 1
count=$(cat "$COUNTFILE" 2>/dev/null || echo 0)
count=$((count - 1))
[ "$count" -lt 0 ] && count=0
echo "$count" > "$COUNTFILE"
if [ "$count" -eq 0 ]; then
echo "$(date): Releasing apt-daily-upgrade (JobId=${2:-unknown})" >> /var/log/bareos/apt-inhibit.log
systemctl unmask apt-daily-upgrade.service apt-daily-upgrade.timer
systemctl start apt-daily-upgrade.timer
fi
) 200>"$LOCKFILE"
;;
status)
count=$(cat "$COUNTFILE" 2>/dev/null || echo 0)
echo "Active locks: $count"
systemctl is-enabled apt-daily-upgrade.timer 2>/dev/null && echo "Timer: enabled" || echo "Timer: masked"
;;
*)
echo "Usage: $0 {lock|unlock|status} [JobId]"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment