Ticket #1124: ds-backup.sh

File ds-backup.sh, 4.2 KB (added by hamiltonchua, 12 years ago)
Line 
1#!/bin/bash
2#
3# Wrapper around ds-backup - will be called in 2 situations
4#
5#  - On cron, every 30 minutes during waking/school hours
6#    If you are calling this from cron, pass 'cron' as
7#    the first parameter.
8#
9#  - from a NetworkManager event when we
10#    associate to the school network. In that case, we get
11#    2 parameters - if, action
12#
13# Note: this wrapper _must_ be cheap to execute to avoid burning
14# battery.
15#
16# Author: Martin Langhoff <martin@laptop.org>
17#
18
19##
20## Are we on a School server network?
21## skip if we aren't!
22##
23## Note: this is simplistic on purpose - as we may be
24##       in one of many network topologies.
25##
26function skip_noschoolnet {
27
28    # no DNS, no XS
29    # grep -c '^nameserver ' /etc/resolv.conf 1>&/dev/null || exit
30
31    # can't resolve & ping? outta here
32    # verify if we are inside an soas
33
34    # SoaS : use the backup_url saved from registration when in an SoaS
35
36    if [ -e ~/.sugar/soas/backup_url ]
37    then
38      BACKUP_URL=`cat ~/.sugar/soas/backup_url`
39    else
40      BACKUP_URL='schoolserver'
41    fi
42
43    ping -c1 $BACKUP_URL 1>&/dev/null || exit
44
45    # TODO: if we are on a mesh, count the hops to
46    # the MPP - as the MPP will be the XS _or_ will provide
47    # access to it. Only continue to backup if the hopcount
48    # is low...
49
50}
51
52# If we have backed up recently, leave it for later. Use
53# -mtime 0 for "today"
54# -mtime -1 for "since yesterday"
55# -mtime -10 for in the last 10 days
56#
57# Using -daystart means that the script is more eager to backup
58# from early each day. Without -daystart, backups tend to happen
59# later and later everyday, as they only start trying after 24hs...
60#
61# Another tack could be to try -mmin -1200 (20hs) -
62#
63function skip_ifrecent {
64    RECENT_CHECK='-daystart -mtime 0'
65    if [ `find ~/.sugar/default/ds-backup-done $RECENT_CHECK 2>/dev/null` ]
66    then
67        exit 0
68    fi
69}
70
71
72# Will skip if we are on low batt
73function skip_onlowbatt {
74
75    if [ -e /sys/class/power_supply/olpc-battery/capacity \
76        -a -e /sys/class/power_supply/olpc-ac/online ]
77    then
78        # OLPC HW
79        B_LEVEL=`cat /sys/class/power_supply/olpc-battery/capacity`
80        AC_STAT=`cat /sys/class/power_supply/olpc-ac/online`
81    else
82        # Portable, but 100ms slower on XO-1
83        # Note - we read the 1st battery, and the 1st AC
84        # TODO: Smarter support for >1 battery
85        B_HAL=`hal-find-by-capability --capability battery | head -n1`
86        AC_HAL=`hal-find-by-capability --capability ac_adapter`
87        if [ -z $B_HAL -o -z $AC_HAL ]
88        then
89            # We do expect a battery & AC
90            exit 1;
91        fi
92
93        B_LEVEL=`hal-get-property --udi $B_HAL --key battery.charge_level.percentage`
94        AC_STAT=`hal-get-property --udi $AC_HAL --key ac_adapter.present`
95
96        # hal reports ac adapter presence as 'true'
97        # ... translate...
98        if [ "$AC_STAT" = 'true' ]
99        then
100            AC_STAT=1
101        else
102            AC_STAT=0
103        fi
104    fi
105
106    # If we are on battery, and below 30%, leave it for later
107    if [ $AC_STAT == "0" -a $B_LEVEL -lt 30 ]
108    then
109        exit 0
110    fi
111}
112##
113## TODO:
114## - Handle being called from NM
115
116# change to homedir
117# rsync does not like being called from elsewhere
118cd ~
119
120## These checks are ordered cheapest first
121skip_ifrecent;
122## SoaS : do not check battery when we are on SoaS
123if [ ! -e ~/.sugar/soas/backup_url ]
124then
125  skip_onlowbatt;
126fi
127skip_noschoolnet;
128
129### Ok, we are going to attempt a backup
130
131# make the lock dir if needed
132# we will keep the (empty) file around
133if [ ! -d ~/.sugar/default/lock ]
134then
135    mkdir ~/.sugar/default/lock || exit 1;
136fi
137
138#
139#  Sleep a random amount, not greater than 20 minutes
140#  We use this to stagger client machines in the 30 minute
141#  slots between cron invocations...
142#  (yes we need all the parenthesys)
143(sleep $(($RANDOM % 1200)));
144
145# After the sleep, check again. Perhaps something triggered
146# another invokation that got the job done while we slept
147skip_ifrecent;
148
149# Execute ds-backup.py from the same
150# directory where we are. Use a flock
151# to prevent concurrent runs. If the
152# flock does not succeed immediately,
153# we quit.
154LOCKFILE=~/.sugar/default/lock/ds-backup.run
155flock -n $LOCKFILE `dirname $0 `/ds-backup.py
156EXITCODE=$?
157
158# Note: we keep the lockfile around to save
159# NAND cycles.
160
161# Propagate the exit code of the flock/ds-backup invocation
162exit $EXITCODE
163