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