]> arthur.barton.de Git - backup-script.git/blob - bin/backup-status
68a7d1f8334feab9b4c6d02fb44fff56eca7edc5
[backup-script.git] / bin / backup-status
1 #!/bin/bash
2 #
3 # backup-script system for cloning systems using rsync
4 # Copyright (c)2008-2015 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                 if [ "$(uname)" = "Linux" ]; then
72                         last=`LC_ALL=C stat "$1" | grep "^Modify: " \
73                          | cut -d':' -f2- | cut -d. -f1`
74                 else
75                         last=`LC_ALL=C stat -f "%Sc" "$1"`
76                 fi
77                 [ -n "$last" ] && echo "$2  - Date:" $last
78                 code=
79                 source "$1"
80                 case "$code" in
81                   0)    txt=", OK"; ;;
82                   24)   txt=", WARNING (some files vanished during backup)"; ;;
83                   *)    txt=", ERROR"
84                 esac
85                 [ -n "$code" ] && echo "$2  - Result code: $code$txt"
86         else
87                 echo "$2  - No timestamp recorded! Backup currently running or aborted?"
88         fi
89 }
90
91 Snapshot_Info() {
92         echo "  - Snapshot: $1"
93         Check_Size "$1" "  "
94         Check_Stamp "$1/.stamp" "  "
95 }
96
97 Get_Result_Code() {
98         code=1
99         [ -r "$1" ] && source "$1"
100         [ -z "$code" ] && code=1
101         echo $code
102 }
103
104 if [ "$1" == "-r" -o "$1" == "--running" ]; then
105         pid="$(cat "$PIDFILE" 2>/dev/null)"
106         if [ -n "$pid" ]; then
107                 if kill -0 "$pid" >/dev/null 2>&1; then
108                         echo "Backup job running with PID $pid."
109                         echo
110                         pstree -ap "$pid" 2>/dev/null
111                         exit 0
112                 else
113                         echo "No backup running (invalid PID $pid in \"$PIDFILE\")."
114                         exit 1
115                 fi
116         fi
117         echo "No backup running (no PID file \"$PIDFILE\" found)."
118         exit 1
119 fi
120
121 while [ $# -gt 0 ]; do
122         case "$1" in
123                 "--errors"|"-e")
124                         ONLY_ERRORS=1
125                         ONLY_LATEST=1
126                         ;;
127                 "--latest"|"-l")
128                         ONLY_LATEST=1
129                         ;;
130                 "--quick"|"-q")
131                         QUICK=1
132                         ;;
133                 "-"*)
134                         Usage
135                         ;;
136                 *)
137                         break
138         esac
139         shift
140 done
141
142 if [ $# -ge 1 ]; then
143         for s in "$@"; do
144                 if [ ! -r "${conf_d}/$s" ]; then
145                         echo "$NAME: Can' read \"${conf_d}/$s\"!"
146                         exit 1
147                 fi
148                 sys="$sys ${conf_d}/$s"
149         done
150 else
151         sys="${conf_d}/"*
152 fi
153
154 for f in $sys; do
155         [ -r "$f" -a -f "$f" ] || continue
156
157         fname=`basename $f`
158         case "$fname" in
159                 "backup-script.conf"|*.sh)
160                         continue
161                         ;;
162         esac
163
164         # Set global defaults
165         system="$fname"
166         target="$default_target"
167         generations="$default_generations"
168
169         # Read in system configuration file
170         source "$f"
171
172         target="$target/$(basename "$f")"
173
174         [ -d "$target" ] || continue
175
176         if [ "$ONLY_ERRORS" != "0" ]; then
177                 [ $generations -gt 0 ] \
178                         && result=$(Get_Result_Code "$target/latest/.stamp") \
179                         || result=$(Get_Result_Code "$target/.stamp")
180                 [ $result -eq 0 -o $result -eq 24 ] && continue
181         fi
182
183         # System name
184         [ "$system" = "$fname" ] && echo "$fname" || echo "$fname [$system]"
185
186         # System target directory
187         echo "- Target: $target"
188
189         if [ $generations -gt 0 ]; then
190                 if [ "$ONLY_LATEST" = "0" ]; then
191                         for s in $target/[0-9]*-[0-9]* $target/current; do
192                                 [ -e "$s" ] || continue
193                                 Snapshot_Info "$s"
194                                 snapshots=$snapshots+1
195                         done
196                 elif [ -e "$target/latest" ]; then
197                         Snapshot_Info "$target/latest"
198                         snapshots=$snapshots+1
199                 fi
200         else
201                 # Timestamp and result code
202                 Check_Size "$target"
203                 Check_Stamp "$target/.stamp"
204                 snapshots=$snapshots+1
205         fi
206
207         count=$count+1
208         echo
209 done
210
211 if [ $count -lt 1 ]; then
212         echo "No backups found!"
213         exit 1
214 fi
215 [ $count -eq 1 ] && sc="" || sc="s"
216 [ $snapshots -eq 1 ] && ss="" || ss="s"
217 echo "$count system backup$sc found, $snapshots snapshot$ss."
218
219 # -eof-