Skip to content

Instantly share code, notes, and snippets.

@newhinton
Created April 9, 2021 08:19
Show Gist options
  • Save newhinton/e67a2f95e281e82f57a4784b6986ed27 to your computer and use it in GitHub Desktop.
Save newhinton/e67a2f95e281e82f57a4784b6986ed27 to your computer and use it in GitHub Desktop.
#!/bin/bash
#run as user for notifications
if [ "$(id -u)" -eq 0 ]; then
echo "Do not run as root!";
exit 1
fi
TIMEDIFF=82800 #23h, 24h=86400
TIMESTAMPFILE="~/.config/backintimecron/time.txt"
LOGFILE="~/.config/backintimecron/log.txt"
NOW=$(date +%s)
LAST=$(cat $TIMESTAMPFILE)
if [ -n "$LAST" ]; then
echo "$TIMESTAMPFILE was read!"
else
echo "$TIMESTAMPFILE was not readable!"
touch $TIMESTAMPFILE
fi
log() {
echo "$1"
echo "$1" >> $LOGFILE
}
show() {
log "$1"
# Required so that notify-send works from user's cron
XDG_RUNTIME_DIR=/run/user/$(id -u) notify-send "$1"
}
exec_window() {
#Additional Checks may be necessary, in my script, i check if the remote host is reachable
show "$1"
echo $NOW > $TIMESTAMPFILE
#Todo: check how that works with multiple profiles
/usr/bin/nice -n19 /usr/bin/ionice -c2 -n7 /usr/bin/backintime backup
# This shows prematurely, since backintime exits immediately because it only triggers a new process.
#show "Done."
}
DELTA=$((NOW - LAST))
log "$DELTA seconds since last backup started."
if [ "$DELTA" -ge $TIMEDIFF ];
then
exec_window "Make backup, because some windows have been missed!"
exit
fi
NOWHUMAN=$(date +%H:%M)
log $NOWHUMAN
if [[ "$NOWHUMAN" > "18:00" ]] && [[ "$NOWHUMAN" < "19:00" ]]; then
if [ "$DELTA" -ge 7200 ];
then
exec_window "Make backup, we are in schedule!"
else
show "Dont make scheduled backup, since the last one is not older than 2h"
fi
else
log "Not in schedule."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment