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