]> arthur.barton.de Git - backup-script.git/blob - bin/backup-status
Unify and enhance usage information and script descriptions
[backup-script.git] / bin / backup-status
1 #!/bin/bash
2 #
3 # backup-script system for cloning systems using rsync
4 # Copyright (c)2008-2016 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 PIDFILE="/var/run/backup-script.pid"
15 QUICK=0
16 ONLY_ERRORS=0
17 ONLY_LATEST=0
18
19 export LC_ALL=C
20
21 declare -i count=0
22 declare -i snapshots=0
23
24 # Default settings, can be overwritten in backup-script.conf:
25 [ -d "/usr/local/etc/backup-script.d" ] \
26         && conf_d="/usr/local/etc/backup-script.d" \
27         || conf_d="/etc/backup-script.d"
28 default_target="/var/backups"
29 default_generations=0
30
31 # Search configuration file (last one is used as default!)
32 for conf in \
33         "/usr/local/etc/backup-script.conf" \
34         "/etc/backup-script.conf" \
35         "${conf_d}/backup-script.conf" \
36         "/usr/local/etc/backup-script.conf" \
37 ; do
38         if [ -r "$conf" ]; then
39                 # shellcheck source=/dev/null
40                 source "$conf"
41                 break
42         fi
43 done
44
45 Usage() {
46         echo "Usage: $NAME [--errors|--latest] [--quick] [<job> [<job> [...]]]"
47         echo "       $NAME --running"
48         echo
49         echo "  -e, --errors    Only show current backups with errors (implies \"--latest\")."
50         echo "  -l, --latest    Only show latest backup generations."
51         echo "  -q, --quick     Don't calculate backup sizes."
52         echo "  -r, --running   Check if an \"backup-script\" task is currently running."
53         echo
54         echo "When no <job> is given, all defined jobs are listed."
55         echo
56         exit 2
57 }
58
59 Check_Size() {
60         # $1: directory
61         # $2: padding
62
63         if [ "$QUICK" = "0" ]; then
64                 size=$(du -Hhs "$1" | cut -f1)
65                 # shellcheck disable=SC2086
66                 echo "$2  - Size:" $size
67         fi
68 }
69
70 Check_Stamp() {
71         # $1: stamp file
72         # $2: padding
73
74         if [ -f "$1" ]; then
75                 declare -i code=-1
76                 declare -i start_t=-1
77                 start=""
78                 declare -i end_t=-1
79                 end=""
80                 declare -i duration_t=-1
81
82                 # Read in "stamp file"
83                 # shellcheck source=/dev/null
84                 source "$1"
85
86                 if [ $start_t -gt 0 ] && [ $end_t -gt 0 ]; then
87                         if [ "$(uname)" = "Linux" ]; then
88                                 start=$(date -d @"$start_t")
89                                 end=$(date -d @"$end_t")
90                         else
91                                 start=$(date -r "$start_t")
92                                 end=$(date -r "$end_t")
93                         fi
94                         duration_t=$end_t-$start_t
95                 else
96                         if [ "$(uname)" = "Linux" ]; then
97                                 end=$(LC_ALL=C stat "$1" | grep "^Modify: " \
98                                  | cut -d':' -f2- | cut -d. -f1)
99                         else
100                                 end=$(LC_ALL=C stat -f "%Sc" "$1")
101                         fi
102                 fi
103                 # shellcheck disable=SC2086
104                 [ -n "$start" ] && echo "$2  - Start date:" $start
105                 # shellcheck disable=SC2086
106                 [ -n "$end" ] && echo "$2  - End date:" $end
107                 if [ $duration_t -gt -1 ]; then
108                         declare -i s=$duration_t
109                         if [ $s -ge 60 ]; then
110                                 declare -i m=$((s / 60))
111                                 declare -i s=$((s % 60))
112                                 if [ $m -ge 60 ]; then
113                                         declare -i h=$((m / 60))
114                                         declare -i m=$((m % 60))
115                                         if [ $h -ge 24 ]; then
116                                                 declare -i d=$((h / 24))
117                                                 declare -i h=$((h % 24))
118                                                 duration="${d}d${h}h${m}m${s}s"
119                                         else
120                                                 duration="${h}h${m}m${s}s"
121                                         fi
122                                 else
123                                         duration="${m}m${s}s"
124                                 fi
125                         else
126                                 duration="${s}s"
127                         fi
128                         echo "$2  - Duration:" $duration
129                 fi
130
131                 case "$code" in
132                   0)    txt=", OK"; ;;
133                   24)   txt=", WARNING (some files vanished during backup)"; ;;
134                   *)    txt=", ERROR"
135                 esac
136                 [ $code -ge 0 ] && echo "$2  - Result code: ${code}${txt}"
137         else
138                 echo "$2  - No timestamp recorded! Backup currently running or aborted?"
139         fi
140 }
141
142 Snapshot_Info() {
143         echo "  - Snapshot: $1"
144         Check_Size "$1" "  "
145         Check_Stamp "$1/.stamp" "  "
146 }
147
148 Get_Result_Code() {
149         code=1
150         # shellcheck source=/dev/null
151         [ -r "$1" ] && source "$1"
152         [ -z "$code" ] && code=1
153         echo $code
154 }
155
156 if [[ "$1" == "-r" || "$1" == "--running" ]]; then
157         pid="$(cat "$PIDFILE" 2>/dev/null)"
158         if [ -n "$pid" ]; then
159                 if kill -0 "$pid" >/dev/null 2>&1; then
160                         echo "Backup job running with PID $pid."
161                         echo
162                         pstree -ap "$pid" 2>/dev/null
163                         exit 0
164                 else
165                         echo "No backup running (invalid PID $pid in \"$PIDFILE\")."
166                         exit 1
167                 fi
168         fi
169         echo "No backup running (no PID file \"$PIDFILE\" found)."
170         exit 1
171 fi
172
173 while [ $# -gt 0 ]; do
174         case "$1" in
175                 "--errors"|"-e")
176                         ONLY_ERRORS=1
177                         ONLY_LATEST=1
178                         ;;
179                 "--latest"|"-l")
180                         ONLY_LATEST=1
181                         ;;
182                 "--quick"|"-q")
183                         QUICK=1
184                         ;;
185                 "-"*)
186                         Usage
187                         ;;
188                 *)
189                         break
190         esac
191         shift
192 done
193
194 if [ $# -ge 1 ]; then
195         for s in "$@"; do
196                 if [ ! -r "${conf_d}/$s" ]; then
197                         echo "$NAME: Can' read \"${conf_d}/$s\"!"
198                         exit 1
199                 fi
200                 sys+=("${conf_d}/$s")
201         done
202 else
203         sys=("${conf_d}/"*)
204 fi
205
206 for f in "${sys[@]}"; do
207         [[ -r "$f" && -f "$f" ]] || continue
208
209         fname=$(basename "$f")
210         case "$fname" in
211                 "backup-script.conf"|*.sh)
212                         continue
213                         ;;
214         esac
215
216         # Set global defaults
217         system="$fname"
218         target="$default_target"
219         generations="$default_generations"
220
221         # Read in system configuration file
222         # shellcheck source=/dev/null
223         source "$f"
224
225         target="$target/$(basename "$f")"
226
227         [ -d "$target" ] || continue
228
229         if [ "$ONLY_ERRORS" != "0" ]; then
230                 [ $generations -gt 0 ] \
231                         && result=$(Get_Result_Code "$target/latest/.stamp") \
232                         || result=$(Get_Result_Code "$target/.stamp")
233                 [[ $result -eq 0 || $result -eq 24 ]] && continue
234         fi
235
236         # System name
237         [ "$system" = "$fname" ] && echo "$fname" || echo "$fname [$system]"
238
239         # System target directory
240         echo "- Target: $target"
241
242         if [ $generations -gt 0 ]; then
243                 if [ "$ONLY_LATEST" = "0" ]; then
244                         for s in $target/[0-9]*-[0-9]* $target/current; do
245                                 [ -e "$s" ] || continue
246                                 Snapshot_Info "$s"
247                                 snapshots=$snapshots+1
248                         done
249                 elif [ -e "$target/latest" ]; then
250                         Snapshot_Info "$target/latest"
251                         snapshots=$snapshots+1
252                 fi
253         else
254                 # Timestamp and result code
255                 Check_Size "$target"
256                 Check_Stamp "$target/.stamp"
257                 snapshots=$snapshots+1
258         fi
259
260         count=$count+1
261         echo
262 done
263
264 if [ "$ONLY_ERRORS" != "0" ]; then
265         status="failed "; p0="."; pN="!"
266 else
267         status=""; p0="!"; pN="."
268 fi
269 if [ $count -lt 1 ]; then
270         echo "No ${status}backups found${p0}"
271         exit 1
272 fi
273 [ $count -eq 1 ] && sc="" || sc="s"
274 [ $snapshots -eq 1 ] && ss="" || ss="s"
275 echo "$count ${status}system backup$sc found, $snapshots snapshot$ss${pN}"
276
277 # -eof-