chiark / gitweb /
initial checkin; mostly complete
[distorted-backup] / bkpfunc.sh
1 ### -*-sh-*-
2 ###
3 ### Functions for backup clients.
4 ###
5 ### (c) 2011 Mark Wooding
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This program is free software; you can redistribute it and/or modify
11 ### it under the terms of the GNU General Public License as published by
12 ### the Free Software Foundation; either version 2 of the License, or
13 ### (at your option) any later version.
14 ###
15 ### This program is distributed in the hope that it will be useful,
16 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ### GNU General Public License for more details.
19 ###
20 ### You should have received a copy of the GNU General Public License
21 ### along with this program; if not, write to the Free Software Foundation,
22 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
24 set -e
25
26 ###--------------------------------------------------------------------------
27 ### Utilities.
28
29 QUIS=${0##*/}
30 fail () { echo >&2 "$QUIS: $*"; exit 1; }
31
32 preflight () {
33   for i in TMPDIR HOST RMT LEVEL LASTDATE ASSET TARGET; do
34     eval "p=\${BKP_$i+t}"
35     case "$p" in t) ;; *) fail "environment not correctly configured" ;; esac
36   done
37 }
38
39 run () {
40   echo "$*"
41   "$@"
42 }
43
44 bkpadmin () {
45   ## bkpadmin COMMAND ARGUMENT ...
46   ssh $BKP_HOST userv root bkpadmin "$@"
47 }
48
49 datefmt () {
50   fmt=$1 date=$2
51   ## Convert the DATE (in ISO8601-ish format, with an optional numeric
52   ## timezone offset) according to the strftime(3) format FMT.
53
54   ## We have Perl available, so you'd think this would be fairly easy.  Alas,
55   ## not.  The obvious thing to do would be use the Date::Format module, but
56   ## that's unusably broken.  Consider:
57   ##
58   ##   $t0 = 1319934600; $t1 = $t0 + 3600;      # obviously different
59   ##   for my $i ($t0, $t1)                     # print identically
60   ##     { print time2str "%a %b %e %H:%M:%S %Y %z\n", $i; }
61   ##
62   ## The Date::Parse module seems to work correctly, but isn't installed by
63   ## default on some of our target platforms.  So we end up doing a lot of
64   ## the work by hand.
65
66   perl -e '
67     use POSIX;
68
69     my ($fmt, $date) = @ARGV;
70
71     ## Parse the input date.
72     my ($yr, $mo, $dy, $hr, $mi, $s, $tz) = $date =~
73       /^\s*(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)(?:\s+)?([-+]\d+)?\s*$/
74       or die "bad input date `$date'\''";
75
76     ## Convert the input date into a time_t.  This is annoyingly fiddly.  If
77     ## an explicit timezone offset is supplied, do the conversion as UTC, and
78     ## then apply the timezone correction.  This means that we must hack
79     ## about with the awful BCD timezone offset.
80     my $t;
81     if (!defined $tz) {
82       $t = mktime $s, $mi, $hr, $dy, $mo - 1, $yr - 1900, undef, undef, -1;
83     } else {
84       use integer;
85       my ($tzsign, $tzabs) = $tz < 0 ? (-1, -$tz) : (+1, $tz);
86       my $tzoff = $tzsign*(60*($tzabs/100) + ($tzabs%100));
87       local $ENV{TZ} = "UTC0"; tzset;
88       $t = mktime $s, $mi, $hr, $dy, $mo - 1, $yr - 1900, undef, undef, 0;
89       $t -= 60*$tzoff;
90     }
91
92     ## Now format this as requested.
93     tzset; my @tm = localtime $t;
94     print strftime $fmt, @tm;
95     print "\n";
96   ' "$fmt" "$date"
97 }
98
99 ###----- That's all, folks --------------------------------------------------