backup.sh shows the days since the last simple_backup (orange from 7, red from 14); backup-reminder.timer notifies daily once it is overdue.
64 lines
2.0 KiB
Bash
Executable File
64 lines
2.0 KiB
Bash
Executable File
#!/bin/bash
|
|
# Days since the last simple_backup for xmobar, e.g. " 9d | ": hidden under 7 days, orange from 7, red from 14
|
|
# Reads simple_backup's state file and the journal (versions before the state file only log there), whichever is newer.
|
|
# With "list": show the date in a notification (for a click on the bar)
|
|
# With "remind": notify only when a backup is overdue (run daily by backup-reminder.timer)
|
|
|
|
warn=7 overdue=14
|
|
state="${XDG_STATE_HOME:-$HOME/.local/state}/simple_backup/last_backup"
|
|
|
|
last=0 dir=""
|
|
if [[ -r $state ]]; then
|
|
{ read -r time; read -r dir; } < "$state"
|
|
last=$(date -d "$time" +%s 2>/dev/null) || last=0
|
|
fi
|
|
|
|
# The journal drops old entries, so after a long gap there may be nothing left to find
|
|
line=$(journalctl -q -t /usr/bin/simple_backup -r -o short-unix 2>/dev/null | grep -m1 'Backup completed')
|
|
jlast=${line%%.*}
|
|
if [[ $jlast =~ ^[0-9]+$ ]] && (( jlast > last )); then
|
|
last=$jlast dir=""
|
|
fi
|
|
|
|
days=$(( ($(date +%s) - last) / 86400 ))
|
|
|
|
age() {
|
|
case $days in
|
|
0) echo "today" ;;
|
|
1) echo "1 day ago" ;;
|
|
*) echo "$days days ago" ;;
|
|
esac
|
|
}
|
|
|
|
case ${1:-} in
|
|
list)
|
|
if (( last )); then
|
|
body="$(date -d "@$last" '+%Y-%m-%d %H:%M') ($(age))"
|
|
[[ -n $dir ]] && body+=$'\n'"$dir"
|
|
else
|
|
body="No backup found"
|
|
fi
|
|
notify-send "Last backup" "$body"
|
|
exit ;;
|
|
remind)
|
|
(( last && days < overdue )) && exit 0
|
|
if (( last )); then
|
|
body="Last backup $(age) ($(date -d "@$last" '+%Y-%m-%d'))"
|
|
else
|
|
body="No backup found"
|
|
fi
|
|
# A failure (e.g. no notification daemon yet) makes the service retry
|
|
exec notify-send "Backup overdue" "$body. Plug in the backup disk." ;;
|
|
esac
|
|
|
|
(( last && days < warn )) && exit 0
|
|
icon=$'\xef\x82\xa0' # nf-fa-hdd_o, as UTF-8 bytes so the locale doesn't matter
|
|
if (( ! last )); then
|
|
text="<fc=#f7768e>$icon ?</fc>"
|
|
elif (( days >= overdue )); then
|
|
text="<fc=#f7768e>$icon ${days}d</fc>"
|
|
else
|
|
text="<fc=#ffb86c>$icon ${days}d</fc>"
|
|
fi
|
|
echo "$text | "
|