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