]> arthur.barton.de Git - backup-script.git/blob - bin/backup-status
Add "[default_]exclude_dirs_add" options
[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 [-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 for f in $sys; do
137         [ -r "$f" -a -f "$f" ] || continue
138
139         fname=`basename $f`
140         case "$fname" in
141                 "backup-script.conf"|*.sh)
142                         continue
143                         ;;
144         esac
145
146         # Set global defaults
147         system="$fname"
148         target="$default_target"
149         generations="$default_generations"
150
151         # Read in system configuration file
152         source "$f"
153
154         target="$target/$(basename "$f")"
155
156         [ -d "$target" ] || continue
157
158         # System name
159         [ "$system" = "$fname" ] && echo "$fname" || echo "$fname [$system]"
160
161         # System target directory
162         echo "- Target: $target"
163
164         if [ $generations -gt 0 ]; then
165                 if [ "$ONLY_LATEST" = "0" ]; then
166                         for s in $target/[0-9]*-[0-9]* $target/current; do
167                                 [ -e "$s" ] || continue
168                                 Snapshot_Info "$s"
169                                 snapshots=$snapshots+1
170                         done
171                 elif [ -e "$target/latest" ]; then
172                         Snapshot_Info "$target/latest"
173                         snapshots=$snapshots+1
174                 fi
175         else
176                 # Timestamp and result code
177                 Check_Size "$target"
178                 Check_Stamp "$target/.stamp"
179                 snapshots=$snapshots+1
180         fi
181
182         count=$count+1
183         echo
184 done
185
186 if [ $count -lt 1 ]; then
187         echo "No backups found!"
188         exit 1
189 fi
190 [ $count -eq 1 ] && sc="" || sc="s"
191 [ $snapshots -eq 1 ] && ss="" || ss="s"
192 echo "$count system backup$sc found, $snapshots snapshot$ss."
193
194 # -eof-