]> arthur.barton.de Git - backup-script.git/blob - bin/backup-status
backup-status: "quick" mode is "-q", not "-p"!
[backup-script.git] / bin / backup-status
1 #!/bin/bash
2 #
3 # backup-script system for cloning systems using rsync
4 # Copyright (c)2008-2013 Alexander Barton, alex@barton.de
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 # Please read the file COPYING, README and AUTHORS for more information.
11 #
12
13 NAME=`basename $0`
14 CONF_D="/etc/backup-script.d"
15 QUICK=0
16
17 export LC_ALL=C
18
19 declare -i count=0
20 declare -i snapshots=0
21
22 # Default settings, can be overwritten in backup-script.conf:
23 default_target=""
24 default_generations=0
25
26 Check_Size() {
27         # $1: directory
28         # $2: padding
29
30         if [ "$QUICK" = "0" ]; then
31                 size=`du -sh "$1" | cut -f1`
32                 echo "$2  - Size:" $size
33         fi
34 }
35
36 Check_Stamp() {
37         # $1: stamp file
38         # $2: padding
39
40         if [ -f "$1" ]; then
41                 last=`stat "$1" | grep "^Modify: " \
42                   | cut -d':' -f2- | cut -d. -f1`
43                 [ -n "$last" ] && echo "$2  - Date:" $last
44                 unset code
45                 source "$1"
46                 case "$code" in
47                   0)    txt=", OK"; ;;
48                   24)   txt=", WARNING (some files vanished during backup)"; ;;
49                   *)    txt=", ERROR"
50                 esac
51                 [ -n "$code" ] && echo "$2  - Result code: $code$txt"
52         else
53                 echo "$2  - No timestamp recorded!? Backup aborted?"
54         fi
55 }
56
57 if [ "$1" == "-q" ]; then
58         QUICK=1
59         shift
60 fi
61
62 case "$1" in
63     "-"*)
64         echo "Usage: $NAME [-q] [<system> [<system> [...]]]"
65         exit 1
66         ;;
67 esac
68
69 if [ $# -ge 1 ]; then
70         for s in $@; do
71                 if [ ! -r "${CONF_D}/$s" ]; then
72                         echo "$NAME: Can' read \"${CONF_D}/$s\"!"
73                         exit 1
74                 fi
75                 sys="$sys ${CONF_D}/$s"
76         done
77 else
78         sys=${CONF_D}/*
79 fi
80
81 [ -r "${CONF_D}/backup-script.conf" ] && source "${CONF_D}/backup-script.conf"
82
83 for f in $sys; do
84         [ -r "$f" -a -f "$f" ] || continue
85
86         fname=`basename $f`
87         case "$fname" in
88                 "backup-script.conf"|*.sh)
89                         continue
90                         ;;
91         esac
92
93         # Set global defaults
94         system="$fname"
95         target="$default_target"
96         generations="$default_generations"
97
98         # Read in system configuration file
99         source "$f"
100
101         # Validate configuration
102         [ "$system" = "localhost" -o "$system" = "127.0.0.1" ] && local=1
103
104         destdir="$target"
105         target="$target/$fname"
106
107         [ -d "$target" ] || continue
108
109         # System name
110         [ "$system" = "$fname" ] && echo "$fname" || echo "$fname [$system]"
111
112         # System target directory
113         echo "- Target: $target"
114
115         if [ $generations -gt 0 ]; then
116                 for s in $target/[0-9]*-[0-9]*; do
117                         echo "  - Snapshot: $s"
118                         Check_Size "$s" "  "
119                         Check_Stamp "$s/.stamp" "  "
120                         snapshots=$snapshots+1
121                 done
122         else
123                 # Timestamp and result code
124                 Check_Size "$s"
125                 Check_Stamp "$target/.stamp"
126                 snapshots=$snapshots+1
127         fi
128
129         count=$count+1
130         echo
131 done
132
133 if [ $count -lt 1 ]; then
134         echo "No backups found!"
135         exit 1
136 fi
137 [ $count -eq 1 ] && sc="" || sc="s"
138 [ $snapshots -eq 1 ] && ss="" || ss="s"
139 echo "$count system backup$sc found, $snapshots snapshot$ss."
140
141 # -eof-