#!/usr/bin/env bash set -e # this should ensure that notify-send works when script is run by crond export XDG_RUNTIME_DIR=/run/user/$(id -u) FLAG_FILE="$HOME/local/duplicity-flag-file" DUPLY_PROFILE="$(hostname -s)-$(whoami)-home" # ensure only one copy of this script is ever running at once [ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en "$0" "$0" "$@" ||: # ensure that the duply profile we want to use exists -- it's an error # if it doesn't because this script shouldn't be in our crontab if it # doesn't if ! [ -e "$HOME/.duply/$DUPLY_PROFILE/conf" ]; then echo >&2 "workstation-duplicity: duply configuration missing" exit 1 fi offline () { test "$(wget -q -O- --tries=10 -T20 http://www.msftncsi.com/ncsi.txt)" \ != "Microsoft NCSI" } # fewer checks if we're running at a terminal (this is a bashism) if ! [ -t 1 ]; then # check when we last ran; don't run if it was less than four hours ago if [ -e "$FLAG_FILE" ] \ && [ -n "$(find $FLAG_FILE -newermt '4 hours ago')" ]; then echo "flag file too new; not backing up" exit 0 fi # don't bother if we're not online if offline; then echo "not online; not backing up" exit 0 fi # don't try to backup immediately; we might not be online for long sleep 300 # confirm we're still online if offline; then echo "not online; not backing up" exit 0 fi fi set +e if [ -e "$FLAG_FILE" ]; then # attempt an incremental backup -- always incremental since we # might be running from a cronjob on weak coffee shop WiFi duply $DUPLY_PROFILE incr --force else # interpret lack of flag file to mean this host's OS has been # reinstalled. Do a full backup, as doing an incremental backup # against the old host's filesystem is not useful. This full # backup won't be purged immediately (MAX_FULL_BACKUPS is 2), in # case files need to be restored duply $DUPLY_PROFILE full fi exit=$? set -e if [ $exit = 0 ]; then if [ -e "$FLAG_FILE" ]; then type="incr" else type="full" fi notify-send --urgency=low --expire-time=10000 --icon=package_network \ "duplicity $type backup @ $(date '+%-l:%M%#p %Z')" ||: # update flag file so cronjob doesn't do this again for four hours touch $FLAG_FILE else if nc -w1 -q0 ch-s012.rsync.net 22