summaryrefslogtreecommitdiff
path: root/bin/workstation-duplicity
blob: 2b2aacc6606e796514a53a7c4745c831860530c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/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 </dev/null; then
	msg="Looks like SSH to backup host is possible."
    else
	msg="We're online, but couldn't SSH to the backup host."
    fi
    notify-send --urgency=critical --icon=dialog-warning \
	    "duplicity could not connect" "$msg" ||:
fi