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