]> arthur.barton.de Git - backup-script.git/blob - bin/backup-status
backup-status: Correctly show "current" snapshot as newest
[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
17 export LC_ALL=C
18
19 declare -i count=0
20 declare -i snapshots=0
21
22 # Default settings, can be overwritten in backup-script.conf:
23 [ -d "/usr/local/etc/backup-script.d" ] \
24         && conf_d="/usr/local/etc/backup-script.d" \
25         || conf_d="/etc/backup-script.d"
26 default_target=""
27 default_generations=0
28
29 # Search configuration file (last one is used as default!)
30 for conf in \
31         "/usr/local/etc/backup-script.conf" \
32         "/etc/backup-script.conf" \
33         "${conf_d}/backup-script.conf" \
34         "/usr/local/etc/backup-script.conf" \
35 ; do
36         if [ -r "$conf" ]; then
37                 source "$conf"
38                 break
39         fi
40 done
41
42 Check_Size() {
43         # $1: directory
44         # $2: padding
45
46         if [ "$QUICK" = "0" ]; then
47                 size=`du -Hhs "$1" | cut -f1`
48                 echo "$2  - Size:" $size
49         fi
50 }
51
52 Check_Stamp() {
53         # $1: stamp file
54         # $2: padding
55
56         if [ -f "$1" ]; then
57                 if [ "$(uname)" = "Linux" ]; then
58                         last=`LC_ALL=C stat "$1" | grep "^Modify: " \
59                          | cut -d':' -f2- | cut -d. -f1`
60                 else
61                         last=`LC_ALL=C stat -f "%Sc" "$1"`
62                 fi
63                 [ -n "$last" ] && echo "$2  - Date:" $last
64                 code=
65                 source "$1"
66                 case "$code" in
67                   0)    txt=", OK"; ;;
68                   24)   txt=", WARNING (some files vanished during backup)"; ;;
69                   *)    txt=", ERROR"
70                 esac
71                 [ -n "$code" ] && echo "$2  - Result code: $code$txt"
72         else
73                 echo "$2  - No timestamp recorded! Backup currently running or aborted?"
74         fi
75 }
76
77 if [ "$1" == "-r" -o "$1" == "--running" ]; then
78         pid="$(cat "$PIDFILE" 2>/dev/null)"
79         if [ -n "$pid" ]; then
80                 if kill -0 "$pid" >/dev/null 2>&1; then
81                         echo "Backup job running with PID $pid."
82                         echo
83                         pstree -ap "$pid" 2>/dev/null
84                         exit 0
85                 else
86                         echo "No backup running (invalid PID $pid in \"$PIDFILE\")."
87                         exit 1
88                 fi
89         fi
90         echo "No backup running (no PID file \"$PIDFILE\" found)."
91         exit 1
92 fi
93
94 if [ "$1" == "-q" ]; then
95         QUICK=1
96         shift
97 fi
98
99 case "$1" in
100     "-"*)
101         echo "Usage: $NAME [-q] [<system> [<system> [...]]]"
102         echo "       $NAME {-r|--running}"
103         exit 2
104         ;;
105 esac
106
107 if [ $# -ge 1 ]; then
108         for s in "$@"; do
109                 if [ ! -r "${conf_d}/$s" ]; then
110                         echo "$NAME: Can' read \"${conf_d}/$s\"!"
111                         exit 1
112                 fi
113                 sys="$sys ${conf_d}/$s"
114         done
115 else
116         sys="${conf_d}/"*
117 fi
118
119 [ -r "${conf_d}/backup-script.conf" ] && source "${conf_d}/backup-script.conf"
120
121 for f in $sys; do
122         [ -r "$f" -a -f "$f" ] || continue
123
124         fname=`basename $f`
125         case "$fname" in
126                 "backup-script.conf"|*.sh)
127                         continue
128                         ;;
129         esac
130
131         # Set global defaults
132         system="$fname"
133         target="$default_target"
134         generations="$default_generations"
135
136         # Read in system configuration file
137         source "$f"
138
139         target="$target/$system"
140
141         [ -d "$target" ] || continue
142
143         # System name
144         [ "$system" = "$fname" ] && echo "$fname" || echo "$fname [$system]"
145
146         # System target directory
147         echo "- Target: $target"
148
149         if [ $generations -gt 0 ]; then
150                 for s in $target/[0-9]*-[0-9]* $target/current; do
151                         [ -e "$s" ] || continue
152                         echo "  - Snapshot: $s"
153                         Check_Size "$s" "  "
154                         Check_Stamp "$s/.stamp" "  "
155                         snapshots=$snapshots+1
156                 done
157         else
158                 # Timestamp and result code
159                 Check_Size "$target"
160                 Check_Stamp "$target/.stamp"
161                 snapshots=$snapshots+1
162         fi
163
164         count=$count+1
165         echo
166 done
167
168 if [ $count -lt 1 ]; then
169         echo "No backups found!"
170         exit 1
171 fi
172 [ $count -eq 1 ] && sc="" || sc="s"
173 [ $snapshots -eq 1 ] && ss="" || ss="s"
174 echo "$count system backup$sc found, $snapshots snapshot$ss."
175
176 # -eof-