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