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