]> arthur.barton.de Git - backup-script.git/blob - bin/backup-audit
Add "backup-audit" script
[backup-script.git] / bin / backup-audit
1 #!/bin/bash
2 #
3 # backup-script system for cloning systems using rsync
4 # Copyright (c)2008-2016 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
15 VERBOSE=0
16 QUIET=0
17
18 export LC_ALL=C
19
20 # Default settings, can be overwritten in backup-script.conf:
21 [ -d "/usr/local/etc/backup-script.d" ] \
22         && conf_d="/usr/local/etc/backup-script.d" \
23         || conf_d="/etc/backup-script.d"
24
25 default_backup_type="rsync"
26 default_files="running-config"
27 default_generations=0
28 default_target="/var/backups"
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                 # shellcheck source=/dev/null
39                 source "$conf"
40                 break
41         fi
42 done
43
44 Usage() {
45         echo "Usage: $NAME [-q|--quiet] [-v|--verbose] [<system> [<system> [...]]]"
46         echo "       $NAME <-d|--dirs> <dir1> <dir2>"
47         echo
48         exit 2
49 }
50
51 BeginDiff() {
52         echo "Differences in $*:"
53 }
54
55 PipeDiff() {
56         local line
57         IFS=
58         while read line; do
59                 echo -e "     | $line"
60         done
61 }
62
63 EndDiff() {
64         :
65 }
66
67 HandleSystem() {
68         local fname="$1"
69
70         # Set global defaults
71         local backup_type="$default_backup_type"
72         local files="$default_files"
73         local generations="$default_generations"
74         local local=0
75         local system="$fname"
76         local target="$default_target"
77
78         # Read in system configuration file
79         # shellcheck source=/dev/null
80         source "$f"
81
82         target="$target/$(basename "$f")"
83
84         [[ -d "$target" ]] || return 0
85
86         # System name
87         [[ "$system" == "$fname" ]] \
88                 && systxt="\"$system\"" \
89                 || systxt="\"$fname\" [\"$system\"]"
90         [[ "$local" -eq 0 ]] \
91                 && echo "Checking $systxt ..." \
92                 || echo "Checking $systxt (local system) ..."
93
94         # Check if job is disabled
95         if [[ "$backup_type" == "disabled" ]]; then
96                 echo "Job is DISABLED and will be skipped."
97                 echo; return 0
98         fi
99
100         if [ $generations -lt 1 ]; then
101                 echo "No generations configured, nothing to compare, skipping system!"
102                 echo; return 1
103         fi
104
105         local latest_d="$target/latest"
106         if [[ ! -d "$latest_d" || ! -r "$latest_d/.stamp" ]]; then
107                 echo "Failed to access latest backup generation in \"$latest_d\", skipping system!"
108                 echo; return 1
109         fi
110         echo "Found latest generation in \"$latest_d\"."
111
112         declare -i code=-1
113         source "$latest_d/.stamp"
114
115         if [[ $code -ne 0 && $code -ne 24 ]]; then
116                 echo "Last backup generation has errors, skipping system!"
117                 echo; return 1
118         fi
119
120         # Search previous generation without errors
121         local previous_d=""
122         for d in $(ls -1dt $target/[0-9]*-[0-9]*); do
123                 [[ -d "$d" && -r "$d/.stamp" ]] || return 0
124
125                 declare -i code=-1
126                 source "$d/.stamp"
127
128                 if [[ $code -eq 0 || $code -eq 24 ]]; then
129                         previous_d="$d"
130                         break
131                 fi
132         done
133         if [[ -z "$previous_d" || ! -d "$previous_d" || ! -r "$previous_d/.stamp" ]]; then
134                 echo "Failed to find previous successfull backup generation, skipping system!"
135                 echo; return 1
136         fi
137         echo "Comparing with generation in $previous_d ..."
138
139         DiffGenerations "$backup_type" "$previous_d" "$latest_d" "$files"
140         return_code=$?
141
142         echo
143         return $return_code
144 }
145
146 DiffGenerations() {
147         local backup_type="$1"
148         local gen1_d="$2"
149         local gen2_d="$3"
150         local files="$4"
151
152         local return_code=0
153
154         if [[ "$backup_type" == "rsync" ]]; then
155                 # rsync Backup Type
156
157                 for file in \
158                         /etc/passwd \
159                         /etc/shadow \
160                         /etc/group \
161                         /etc/gshadow \
162                         \
163                         /etc/fstab \
164                         /etc/hostname \
165                         /etc/hosts \
166                         /etc/machine-id \
167                         /etc/modules \
168                         /etc/network/interfaces \
169                         /etc/networks \
170                 ; do
171                         [[ -r "${gen1_d}${file}" ]] || continue
172
173                         [[ $VERBOSE -ne 0 ]] && echo "Checking \"$file\" ..."
174                         diff -U 3 "${gen1_d}${file}" "${gen2_d}${file}" >"$tmp_diff"
175                         if [[ $? -ne 0 ]]; then
176                                 BeginDiff "\"$file\""
177                                 tail -n +3 "$tmp_diff" | PipeDiff
178                                 EndDiff
179                                 return_code=1
180                         fi
181                 done
182
183                 if [[ -d "${gen1_d}/var/lib/dpkg/info" && -d "${gen2_d}/var/lib/dpkg/info" ]]; then
184                         [[ $VERBOSE -ne 0 ]] && echo "Checking list of installed packages ..."
185                         chroot "${gen1_d}" dpkg --get-selections >"$tmp_1" || return 2
186                         chroot "${gen2_d}" dpkg --get-selections >"$tmp_2" || return 2
187                         diff -U 0 "$tmp_1" "$tmp_2" >"$tmp_diff"
188                         if [[ $? -ne 0 ]]; then
189                                 BeginDiff "list of installed packages"
190                                 tail -n +3 "$tmp_diff" | grep -v '^@@ ' | PipeDiff
191                                 EndDiff
192                                 return_code=1
193                         fi
194                 fi
195         elif [[ "$backup_type" == "scp" ]]; then
196                 # scp Backup type
197                 file=$(basename "$files")
198                 [[ $VERBOSE -ne 0 ]] && echo "Checking \"$file\" ..."
199                 diff -U 3 "${gen1_d}/${file}" "${gen2_d}/${file}" >"$tmp_diff"
200                 if [[ $? -ne 0 ]]; then
201                         BeginDiff "\"$file\""
202                         tail -n +3 "$tmp_diff" | PipeDiff
203                         EndDiff
204                         return_code=1
205                 fi
206         else
207                 echo "Backup type \"$backup_type\" undefined, \"$system\" skipped!"
208                 echo; return 2
209         fi
210
211         return $return_code
212 }
213
214 MkTempFiles() {
215         tmp_1=$(mktemp /tmp/$NAME.XXXXXX) || exit 1
216         tmp_2=$(mktemp /tmp/$NAME.XXXXXX) || exit 1
217         tmp_diff=$(mktemp /tmp/$NAME.XXXXXX) || exit 1
218         tmp_out=$(mktemp /tmp/$NAME.XXXXXX) || exit 1
219 }
220
221 CleanUp() {
222         rm -f "$tmp_1" "$tmp_2" "$tmp_diff" "$tmp_out"
223 }
224
225 while [[ $# -gt 0 ]]; do
226         case "$1" in
227           "-d"|"--dirs")
228                 shift
229                 [[ $# -eq 2 ]] || Usage
230                 MkTempFiles
231                 DiffGenerations "$default_backup_type" "$1/" "$2/" "$default_files"
232                 return_code=$?
233                 CleanUp
234                 exit $return_code
235                 ;;
236           "-q"|"--quiet")
237                 QUIET=1; shift
238                 ;;
239           "-v"|"--verbose")
240                 VERBOSE=1; shift
241                 ;;
242           "-"*)
243                 Usage
244                 ;;
245           *)
246                 break
247         esac
248 done
249
250 if [[ $# -ge 1 ]]; then
251         for s in "$@"; do
252                 if [ ! -r "${conf_d}/$s" ]; then
253                         echo "$NAME: Can' read \"${conf_d}/$s\"!"
254                         exit 1
255                 fi
256                 sys+=("${conf_d}/$s")
257         done
258 else
259         sys=("${conf_d}/"*)
260 fi
261
262 MkTempFiles
263 for f in "${sys[@]}"; do
264         [[ -r "$f" && -f "$f" ]] || continue
265
266         fname=`basename $f`
267         case "$fname" in
268                 "backup-script.conf"|*.sh)
269                         continue
270                         ;;
271         esac
272
273         HandleSystem "$fname" >"$tmp_out" 2>&1
274         [[ $QUIET -eq 0 || $? -ne 0 ]] && cat "$tmp_out"
275 done
276 CleanUp
277
278 # -eof-