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