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