#!/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="$icon ?"
elif (( days >= overdue )); then
text="$icon ${days}d"
else
text="$icon ${days}d"
fi
echo "$text | "