]> arthur.barton.de Git - backup-script.git/blob - bin/backup-audit
Don't set "pipefail", it changes behaviour!
[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 # Set shell options.
31 shopt -s nullglob
32
33 # Search configuration file (last one is used as default!)
34 for conf in \
35         "/usr/local/etc/backup-script.conf" \
36         "/etc/backup-script.conf" \
37         "${conf_d}/backup-script.conf" \
38         "/usr/local/etc/backup-script.conf" \
39 ; do
40         if [ -r "$conf" ]; then
41                 # shellcheck source=/dev/null
42                 source "$conf"
43                 break
44         fi
45 done
46
47 Usage() {
48         echo "Usage: $NAME [-q|--quiet] [-v|--verbose] [<job> [<job> [...]]]"
49         echo "       $NAME <-d|--dirs> <dir1> <dir2>"
50         echo
51         echo "  -d, --dirs      Compare two backup directories (not jobs)."
52         echo "  -q, --quiet     Quite mode, only list jobs with changes or errors."
53         echo "  -v, --verbose   Verbose mode, show all checks that are run."
54         echo
55         echo "When no <job> is given, all defined jobs are checked."
56         echo
57         exit 2
58 }
59
60 BeginDiff() {
61         echo "Differences in $*:"
62 }
63
64 PipeDiff() {
65         local line
66         IFS=
67         while read -r line; do
68                 echo -e "     | $line"
69         done
70 }
71
72 EndDiff() {
73         :
74 }
75
76 ListDirectory() {
77         local base_dir="$1"
78         local dir_name="$2"
79
80         local exclude
81
82         exclude='total '
83         if [[ "$dir_name" == "/" ]]; then
84                 exclude="$exclude"'| \.stamp$| dev$| etc$| proc$| root$| run$| sys$| tmp$'
85                 exclude="$exclude"'| data$| net$| srv$'
86                 exclude="$exclude"'| [[:alnum:]_-]+\.log(\.[[:alnum:]]+|)$'
87         fi
88
89         # shellcheck disable=SC2012
90         ls -Al "$base_dir$dir_name" 2>/dev/null \
91                 | egrep -v "($exclude)" | awk '!($2="")' | column -t
92 }
93
94 HandleSystem() {
95         local fname="$1"
96
97         # Set global defaults
98         local backup_type="$default_backup_type"
99         local files="$default_files"
100         local generations="$default_generations"
101         local local=0
102         local system="$fname"
103         local target="$default_target"
104
105         # Read in system configuration file
106         # shellcheck source=/dev/null
107         source "$f"
108
109         target="$target/$(basename "$f")"
110
111         [[ -d "$target" ]] || return 0
112
113         # System name
114         [[ "$system" == "$fname" ]] \
115                 && systxt="\"$system\"" \
116                 || systxt="\"$fname\" [\"$system\"]"
117         [[ "$local" -eq 0 ]] \
118                 && echo "Checking $systxt ..." \
119                 || echo "Checking $systxt (local system) ..."
120
121         # Check if job is disabled
122         if [[ "$backup_type" == "disabled" ]]; then
123                 echo "Job is DISABLED and will be skipped."
124                 echo; return 0
125         fi
126
127         if [ $generations -lt 1 ]; then
128                 echo "No generations configured, nothing to compare, skipping system!"
129                 echo; return 1
130         fi
131
132         local latest_d="$target/latest"
133         if [[ ! -d "$latest_d" || ! -r "$latest_d/.stamp" ]]; then
134                 echo "Failed to access latest backup generation in \"$latest_d\", skipping system!"
135                 echo; return 1
136         fi
137         echo "Found latest generation in \"$latest_d\"."
138
139         declare -i code=-1
140         # shellcheck source=/dev/null
141         source "$latest_d/.stamp"
142
143         if [[ $code -ne 0 && $code -ne 24 ]]; then
144                 echo "Last backup generation has errors, skipping system!"
145                 echo; return 1
146         fi
147
148         # Search previous generation without errors
149         local previous_d=""
150         # shellcheck disable=SC2045
151         for d in $(ls -1dt "$target/"[0-9]*-[0-9]* 2>/dev/null); do
152                 [[ -d "$d" && -r "$d/.stamp" ]] || return 0
153
154                 declare -i code=-1
155                 # shellcheck source=/dev/null
156                 source "$d/.stamp"
157
158                 if [[ $code -eq 0 || $code -eq 24 ]]; then
159                         previous_d="$d"
160                         break
161                 fi
162         done
163         if [[ -z "$previous_d" || ! -d "$previous_d" || ! -r "$previous_d/.stamp" ]]; then
164                 echo "Failed to find previous successfull backup generation, skipping system!"
165                 echo; return 1
166         fi
167         echo "Comparing with generation in $previous_d ..."
168
169         DiffGenerations "$backup_type" "$previous_d" "$latest_d" "$files"
170         return_code=$?
171
172         echo
173         return $return_code
174 }
175
176 DiffGenerations() {
177         local backup_type="$1"
178         local gen1_d="$2"
179         local gen2_d="$3"
180         local files="$4"
181
182         local return_code=0
183
184         if [[ "$backup_type" == "rsync" ]]; then
185                 # rsync Backup Type
186
187                 for file in \
188                         /etc/passwd \
189                         /etc/shadow \
190                         /etc/group \
191                         /etc/gshadow \
192                         \
193                         /boot/grub/grub.cfg \
194                         /etc/aliases \
195                         /etc/bash.bashrc \
196                         /etc/crontab \
197                         /etc/environment \
198                         /etc/fstab \
199                         /etc/hostname \
200                         /etc/hosts \
201                         /etc/hosts.allow \
202                         /etc/hosts.deny \
203                         /etc/inittab \
204                         /etc/ld.so.conf \
205                         /etc/login.defs \
206                         /etc/machine-id \
207                         /etc/modules \
208                         /etc/network/interfaces \
209                         /etc/networks \
210                         /etc/nsswitch.conf \
211                         /etc/profile \
212                         /etc/rc.local \
213                         /etc/resolv.conf \
214                         /etc/services \
215                         /etc/shells \
216                         /etc/ssh/sshd_config \
217                         /etc/sshd_config \
218                         /etc/sudoers \
219                         /etc/sysctl.conf \
220                 ; do
221                         [[ -r "${gen1_d}${file}" ]] || continue
222
223                         [[ $VERBOSE -ne 0 ]] && echo "Checking \"$file\" ..."
224                         if ! diff -U 3 "${gen1_d}${file}" "${gen2_d}${file}" >"$tmp_diff"; then
225                                 BeginDiff "\"$file\""
226                                 tail -n +3 "$tmp_diff" | PipeDiff
227                                 EndDiff
228                                 return_code=1
229                         fi
230                 done
231
232                 for dir in \
233                         / \
234                         /etc/cron.d/ \
235                         /etc/cron.daily/ \
236                         /etc/cron.hourly/ \
237                         /etc/cron.monthly/ \
238                         /etc/cron.weekly/ \
239                         /etc/sudoers.d/ \
240                         /var/log/dumps/ \
241                 ; do
242                         [[ ! -d "${gen1_d}${dir}" ]] && continue
243                         [[ ! -d "${gen2_d}${dir}" ]] && continue
244
245                         # Make sure that this is a system root; comparing other
246                         # root folders results in misleading output ...
247                         [[ "$dir" == "/" && ! -d "${gen1_d}${dir}/etc" ]] && continue
248
249                         [[ $VERBOSE -ne 0 ]] && echo "Checking \"$dir\" ..."
250                         ListDirectory "${gen1_d}" "${dir}" >"$tmp_1"
251                         ListDirectory "${gen2_d}" "${dir}" >"$tmp_2"
252                         if ! diff -U 0 "$tmp_1" "$tmp_2" >"$tmp_diff"; then
253                                 BeginDiff "\"$dir\" directory"
254                                 tail -n +3 "$tmp_diff" | egrep -v '^@@ ' | PipeDiff
255                                 EndDiff
256                                 return_code=1
257                         fi
258                 done
259
260                 if [[ -d "${gen1_d}/var/lib/dpkg/info" && -d "${gen2_d}/var/lib/dpkg/info" ]]; then
261                         [[ $VERBOSE -ne 0 ]] && echo "Checking list of installed packages ..."
262                         chroot "${gen1_d}" dpkg --get-selections >"$tmp_1" || return 2
263                         chroot "${gen2_d}" dpkg --get-selections >"$tmp_2" || return 2
264                         if ! diff -U 0 "$tmp_1" "$tmp_2" >"$tmp_diff"; then
265                                 BeginDiff "list of installed packages"
266                                 tail -n +3 "$tmp_diff" | grep -v '^@@ ' | PipeDiff
267                                 EndDiff
268                                 return_code=1
269                         fi
270                 fi
271         elif [[ "$backup_type" == "scp" ]]; then
272                 # scp Backup type
273                 file=$(basename "$files")
274                 [[ $VERBOSE -ne 0 ]] && echo "Checking \"$file\" ..."
275                 if ! diff -U 3 "${gen1_d}/${file}" "${gen2_d}/${file}" >"$tmp_diff"; then
276                         BeginDiff "\"$file\""
277                         tail -n +3 "$tmp_diff" | PipeDiff
278                         EndDiff
279                         return_code=1
280                 fi
281         else
282                 echo "Backup type \"$backup_type\" undefined, \"$system\" skipped!"
283                 echo; return 2
284         fi
285
286         return $return_code
287 }
288
289 MkTempFiles() {
290         tmp_1=$(mktemp "/tmp/$NAME.XXXXXX") || exit 1
291         tmp_2=$(mktemp "/tmp/$NAME.XXXXXX") || exit 1
292         tmp_diff=$(mktemp "/tmp/$NAME.XXXXXX") || exit 1
293         tmp_out=$(mktemp "/tmp/$NAME.XXXXXX") || exit 1
294 }
295
296 CleanUp() {
297         rm -f "$tmp_1" "$tmp_2" "$tmp_diff" "$tmp_out"
298 }
299
300 while [[ $# -gt 0 ]]; do
301         case "$1" in
302           "-d"|"--dirs")
303                 shift
304                 [[ $# -eq 2 ]] || Usage
305                 MkTempFiles
306                 DiffGenerations "$default_backup_type" "$1" "$2" "$default_files"
307                 return_code=$?
308                 CleanUp
309                 exit $return_code
310                 ;;
311           "-q"|"--quiet")
312                 QUIET=1; shift
313                 ;;
314           "-v"|"--verbose")
315                 VERBOSE=1; shift
316                 ;;
317           "-"*)
318                 Usage
319                 ;;
320           *)
321                 break
322         esac
323 done
324
325 if [[ $# -ge 1 ]]; then
326         for s in "$@"; do
327                 if [ ! -r "${conf_d}/$s" ]; then
328                         echo "$NAME: Can' read \"${conf_d}/$s\"!"
329                         exit 1
330                 fi
331                 sys+=("${conf_d}/$s")
332         done
333 else
334         sys=("${conf_d}/"*)
335 fi
336
337 MkTempFiles
338 for f in "${sys[@]}"; do
339         [[ -r "$f" && -f "$f" ]] || continue
340
341         fname=$(basename "$f")
342         case "$fname" in
343                 "backup-script.conf"|*.sh)
344                         continue
345                         ;;
346         esac
347
348         HandleSystem "$fname" >"$tmp_out" 2>&1; result=$?
349         [[ $QUIET -eq 0 || $result -ne 0 ]] && cat "$tmp_out"
350 done
351 CleanUp
352
353 # -eof-