chiark / gitweb /
rfreezefs.c: Minor formatting tweak.
[rsync-backup] / check-bkp-status.in
CommitLineData
9f0350f9
MW
1#! @BASH@
2###
3### Check that dumps have been made as expected.
4###
5### (c) 2013 Mark Wooding
6###
7
8###----- Licensing notice ---------------------------------------------------
9###
10### This file is part of the `rsync-backup' program.
11###
12### rsync-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### rsync-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 rsync-backup; if not, write to the Free Software Foundation,
24### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26set -e
27
28quis=${0##*/}
29. @pkgdatadir@/lib.sh
30
31###--------------------------------------------------------------------------
32### Stubs for the configuration file.
33
34defhook () { :; }
35addhook () { :; }
36
37retain () { :; }
38snaptype () { :; }
39like () { :; }
40retry () { :; }
41user () { :; }
42
43###--------------------------------------------------------------------------
44### Output format switch.
45
46formats=:
47defformat () { formats=$formats$1:; }
48fmtstate=begin
49
50fmt () {
51 state=$1; shift
52
53 while :; do
54 ostate=$fmtstate
55 case $fmtstate in $state) break ;; esac
56 case "$fmtstate,$state" in
57 begin,end)
58 fmtstate=end
59 ;;
60 begin,*)
61 ${fmt}_header "$@"
62 fmtstate=hosttbl_begin
63 ;;
64 hosttbl_begin,hosttbl_*)
65 ${fmt}_hosttbl_begin "$@"
66 fmtstate=hosttbl_host
67 ;;
68 hosttbl_host,hosttbl_fs)
69 fmtstate=hosttbl_fs
70 ;;
71 hosttbl_fs,hosttbl_host)
72 ${fmt}_hosttbl_sep "$@"
73 fmtstate=hosttbl_host
74 ;;
75 hosttbl_begin,* | hosttbl_end,*)
76 fmtstate=logdump_begin
77 ;;
78 hosttbl_*,*)
79 ${fmt}_hosttbl_end "$@"
80 fmtstate=hosttbl_end
81 ;;
82 logdump_begin,logdump_*)
83 fmtstate=logdump_out
84 ;;
85 logdump_out,logdump_info | logdump_out,logdump_file)
86 fmtstate=$state
87 ;;
88 logdump_*,logdump_begin)
89 ${fmt}_logdump_end "$@"
90 fmtstate=logdump_begin
91 ;;
92 logdump_end,*)
93 fmtstate=footer
94 ;;
95 logdump_*,*)
96 fmtstate=logdump_end
97 ;;
98 footer,end)
99 ${fmt}_footer "$@"
100 fmtstate=end
101 ;;
102 esac
103 case $fmtstate in
104 "$ostate")
105 echo >&2 "$quis: FATAL! NO PROGRESS IN FMT STATE MACHINE"
106 exit 9
107 ;;
108 esac
109 done
110
111 ${fmt}_$fmtstate "$@"
112}
113
114###--------------------------------------------------------------------------
115### Plain text output.
116
117defformat txt
118
119txt_header () { :; }
120txt_hosttbl_begin () { :; }
121txt_hosttbl_host () { echo "HOST $1"; }
122txt_hosttbl_fs () { printf " %-23s %s\n" "$2" "$4"; }
123txt_hosttbl_sep () { echo; }
124txt_hosttbl_end () { :; }
125
126txt_logdump_begin () {
127 cat <<EOF
128
129-----------------------------------------------------------------------------
130Log tail for $1:$2
131
132EOF
133}
134
135txt_logdump_info () { echo "!!! $3"; }
136txt_logdump_file () { cat "$3"; }
137txt_logdump_end () { :; }
138
139txt_footer () { :; }
140txt_end () { :; }
141
142fmt=txt
143
144###--------------------------------------------------------------------------
145### HTML output.
146
147defformat html
148
149html_header () {
150 cat <<EOF
151<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
152 "http://www.w3c.org/TR/html4/strict.dtd">
153<html>
154<head>
155 <title>$now rsync-backup report</title>
156 <style type='text/css'><!--
157 body {
158 background: white;
159 color: black;
160 margin: 1ex 2em;
161 }
162
163 h1, h2 { font-family: sans-serif; font-weight: bold; }
164
165 h1 {
166 padding-bottom: 1ex;
167 border-bottom: solid medium black;
168 margin-bottom: 3ex;
169 font-size: 200%;
170 }
171
172 h2 {
173 border-top: solid thin black;
174 padding-top: 1ex;
175 margin-top: 3ex;
176 font-size: x-large;
177 }
178
179 h1 + h2 {
180 border-top: none;
181 padding-top: 0;
182 }
183
184 div.footer {
185 border-top: solid medium black;
186 margin-top: 3ex;
187 padding-top: 1ex;
188 font-size: small;
189 clear: both;
190 text-align: right;
191 font-style: italic;
192 }
193
194 table.hosttbl {
195 border-collapse: collapse;
196 display: inline-table;
197 margin: 1ex 1em;
198 }
199 table.hosttbl tr.fs td {
200 border: solid thin black;
201 }
202 table.hosttbl td { padding: 0.5ex 0.5em; }
203 table.hosttbl tr.host td {
204 padding-top: 2ex;
205 text-align: center;
206 font-weight: bold;
207 font-family: monospace;
208 }
209 table.hosttbl td.fs {
210 font-family: monospace;
211 }
212
213 table.hosttbl td.winning { background: #2f4; }
214 table.hosttbl td.failed { background: #f80; }
215 table.hosttbl td.broken { background: #f11; }
216 table.hosttbl td.never { background: #66f; }
217
218 pre {
219 clear: both;
220 margin: 3ex 2em;
221 padding: 1ex;
222 background: #eee;
223 border: solid thin black;
1093ae15 224 overflow: auto;
9f0350f9
MW
225 }
226
1093ae15
MW
227 pre.logdump { max-height: 120ex; }
228
9e224eb1
MW
229 .hide { display: none; }
230 a.expand-button {
231 float: right;
232 font-size: medium;
233 font-weight: initial;
234 padding-top: 1ex;
235 }
236
9f0350f9
MW
237 div.logdump-info {
238 margin: 3ex 2em;
239 padding: 1ex;
240 background: #f11;
241 border: solid thin black;
242 }
243 --></style>
9e224eb1
MW
244 <script type='text/javascript'><!--
245 var LAST_EXPAND = null;
246 function elt(id) { return document.getElementById(id); }
cc950b1c
MW
247 function elt_class_p(elt, cls) {
248 return elt.className.match('\\\\b' + cls + '\\\\b');
249 }
9e224eb1 250 function add_elt_class(elt, cls) {
cc950b1c 251 if (!elt_class_p(elt, cls)) elt.className += ' ' + cls;
9e224eb1
MW
252 }
253 function rm_elt_class(elt, cls) {
254 elt.className = elt.className.replace(
255 new RegExp('\\\\s*\\\\b' + cls + '\\\\b\\\\s*'), ' ');
256 }
257 function toggle_expand(ev, tag) {
258 var d = elt('logdump-' + tag);
259 var b = elt('expand-' + tag);
cc950b1c 260 if (elt_class_p(d, 'hide')) do_show(d, b);
9e224eb1
MW
261 else do_hide(d, b);
262 ev.preventDefault();
263 }
264 function do_show(d, b) {
265 rm_elt_class(d, 'hide');
266 b.textContent = '[hide]';
267 }
268 function do_hide(d, b) {
269 add_elt_class(d, 'hide');
270 b.textContent = '[show]';
afd4eb4b
MW
271 if (LAST_EXPAND !== null && d === LAST_EXPAND[0])
272 LAST_EXPAND = null;
9e224eb1
MW
273 }
274 function expand_log(tag) {
afd4eb4b 275 if (LAST_EXPAND !== null) do_hide(LAST_EXPAND[0], LAST_EXPAND[1]);
9e224eb1
MW
276 var d = elt('logdump-' + tag);
277 var b = elt('expand-' + tag);
b0c99c59
MW
278 if (elt_class_p(d, 'hide')) {
279 LAST_EXPAND = [d, b];
280 do_show(d, b);
281 }
9e224eb1
MW
282 }
283 function make_toggle_button(tag) {
284 document.write(
285 "<a class=expand-button id='expand-" + tag + "' href='#' " +
286 "onclick='toggle_expand(event, \"" + tag + "\")'>" +
287 "[show]" +
288 "</a>");
289 }
290 function hide_logdump(tag) {
291 add_elt_class(elt('logdump-' + tag), 'hide');
292 }
293 --></script>
9f0350f9
MW
294</head>
295<body>
296
4064bd38 297<h1><tt>rsync-backup</tt> report: $now_dow $now</tt></h1>
9f0350f9
MW
298EOF
299}
300
301html_hosttbl_begin () {
302 cat <<EOF
303
304<h2>Overview</h2>
305EOF
306}
307
308html_hosttbl_host () {
309 cat <<EOF
310<table class=hosttbl>
311 <tr class=host><td colspan=2>$1</tc>
312EOF
313}
314
315html_hosttbl_fs () {
316 case $3 in
317 winning) link="" knil="" ;;
9e224eb1
MW
318 *)
319 link="<a href='#log-$host:$fs' onclick='expand_log(\"$host:$fs\")'>"
320 knil="</a>"
321 ;;
9f0350f9
MW
322 esac
323 cat <<EOF
324 <tr class=fs>
325 <td class=fs>$2</td>
326 <td class=$3>$link$4$knil</td>
327EOF
328}
329
330html_hosttbl_sep () {
331 cat <<EOF
332</table>
333EOF
334}
335
336html_hosttbl_end () {
337 cat <<EOF
338</table>
339EOF
340}
341
342html_logdump_begin () {
343 cat <<EOF
344
9e224eb1
MW
345<h2 id='log-$1:$2'><a name='log-$1:$2'>Log tail for <tt>$1:$2</tt></a>
346<script type='text/javascript'><!--
347 make_toggle_button('$1:$2');
348--></script></h2>
9f0350f9
MW
349EOF
350}
351
352html_logdump_info () {
353 cat <<EOF
9e224eb1
MW
354<div id='logdump-$1:$2' class=logdump-info><script type='text/javascript'><!--
355 hide_logdump('$1:$2');
356--></script>$3</div>
9f0350f9
MW
357EOF
358}
359
360html_logdump_file () {
361 cat <<EOF
9e224eb1
MW
362<pre id='logdump-$1:$2' class=logdump>
363<script type='text/javascript'><!--
364 hide_logdump('$1:$2');
365--></script>$3</div>
9f0350f9
MW
366EOF
367 cat "$3"
368 cat <<EOF
369</pre>
370EOF
371}
372
373html_logdump_end () { :; }
374
375html_footer () {
376 cat <<EOF
377
378<div class=footer>
3d7eec09
MW
379 Checked at $now $now_time<br>
380 <tt>rsync-backup</tt> $VERSION; &copy; 2014 Mark Wooding
9f0350f9
MW
381</div>
382
383</body>
384</html>
385EOF
386}
387
388html_end () { :; }
389
390###--------------------------------------------------------------------------
391### Main checking.
392
393INDEXDB=@pkglocalstatedir@/index.db
394
395host () { host=$1; }
396
397patterns=
398showhost=
399failures=
400
401backup () {
402 for fs in "$@"; do
403 case $fs in *:*) fs=${fs%%:*} ;; esac
404 matchp=nil
405 for p in "${patterns[@]}"; do
406 case $host:$fs in $p) matchp=t; break ;; esac
407 done
408 case $matchp in nil) return ;; esac
409 when=$(sqlite3 $INDEXDB \
410 "SELECT MAX(date) FROM idx WHERE host = '$host' AND fs = '$fs';")
411 case $when in
412 "")
413 class=never
414 info="NEVER"
415 show=t
416 win=nil
417 ;;
418 "$now")
419 class=winning
420 info="today"
421 show=$verbose
422 win=t
423 ;;
424 *)
425 jdn=$(julian "$when")
426 ago=$(( $now_jdn - $jdn ))
427 case $ago in 1 | -1) days=day ;; *) days=days ;; esac
428 case $ago in
429 -*) class=future; info="${ago#-} $days in the FUTURE" ;;
430 1 | 2) class=failed; info="$ago $days ago" ;;
431 *) class=broken; info="$ago $days ago" ;;
432 esac
433 show=t
434 win=nil
435 ;;
436 esac
437 case $show in
438 t)
439 case $showhost in
440 "$host") ;;
441 *)
442 fmt hosttbl_host $host
443 showhost=$host
444 ;;
445 esac
446 fmt hosttbl_fs $host $fs $class "$info"
447 case $win in
448 nil) failures="$failures $host:$fs" ;;
449 esac
450 ;;
451 esac
452 done
453}
454
455failure_logs () {
456 for fail in $failures; do
457 host=${fail%:*} fs=${fail##*:}
458 any=nil
459 for i in "$logdir/$host/$fs.$now#"*; do
460 if [ -f "$i" ]; then log=$i; any=t; fi
461 done
462 fmt logdump_begin $host $fs
463 case $any in
464 t) fmt logdump_file $host $fs "$log" ;;
465 nil) fmt logdump_info $host $fs "No log! No backup attempted." ;;
466 esac
467 done
468}
469
470###--------------------------------------------------------------------------
471### Read the configuration and we're done.
472
473usage () {
474 echo "usage: $quis [-v] [-c CONF] [-f FORMAT] [PATTERNS...]"
475}
476
477version () {
478 echo "$quis, rsync-backup version $VERSION"
479}
480
481verbose=nil
482
483while getopts "hVc:f:v" opt; do
484 case "$opt" in
485 h) usage; exit 0 ;;
486 V) version; config; exit 0 ;;
487 c) conf=$OPTARG ;;
488 f)
489 case $formats in
490 *":$OPTARG:"*) ;;
491 *) echo >&2 "$0: unknown format \`$OPTARG'"; exit 1 ;;
492 esac
493 fmt=$OPTARG
494 ;;
495 v) verbose=t ;;
496 *) exit 1 ;;
497 esac
498done
499shift $((OPTIND - 1))
500case $# in
501 0) declare -a patterns=("*") ;;
502 *) declare -a patterns=("$@") ;;
503esac
504
505now=$(date +"%Y-%m-%d")
506now_time=$(date +"%H:%M:%S %z")
4064bd38 507now_dow=$(date +"%A")
9f0350f9
MW
508now_jdn=$(julian $now)
509. "$conf"
510failure_logs
511fmt end
512
513###----- That's all, folks --------------------------------------------------