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