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