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