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