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