]> arthur.barton.de Git - backup-script.git/blob - bin/backup-audit
146a9559c6be32ea5bf6f1b456697c00935af10a
[backup-script.git] / bin / backup-audit
1 #!/bin/bash
2 #
3 # backup-script system for cloning systems using rsync
4 # Copyright (c)2008-2019 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=' \.$'
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         find "$base_dir$dir_name". -maxdepth 1 -printf '%M %10u:%-10g %t %12s  %f\n' 2>/dev/null \
91                 | LC_ALL=C sort -k 9 | grep -Ev "($exclude)"
92 }
93
94 ListFilesRecursive() {
95         local base_dir="$1"
96         local dir_name="$2"
97
98         (
99                 cd "$base_dir" || return 1
100                 find ".$dir_name" -type f -o -type l | cut -d'/' -f2-
101         )
102 }
103
104 HandleSystem() {
105         local fname="$1"
106
107         # Set global defaults
108         local backup_type="$default_backup_type"
109         local files="$default_files"
110         local generations="$default_generations"
111         local local=0
112         local system="$fname"
113         local target="$default_target"
114
115         # Read in system configuration file
116         # shellcheck source=/dev/null
117         source "$f"
118
119         target="$target/$(basename "$f")"
120
121         [[ -d "$target" ]] || return 0
122
123         # System name
124         [[ "$system" == "$fname" ]] \
125                 && systxt="\"$system\"" \
126                 || systxt="\"$fname\" [\"$system\"]"
127         [[ "$local" -eq 0 ]] \
128                 && echo "Checking $systxt ..." \
129                 || echo "Checking $systxt (local system) ..."
130
131         # Check if job is disabled
132         if [[ "$backup_type" == "disabled" ]]; then
133                 echo "Job is DISABLED and will be skipped."
134                 echo; return 0
135         fi
136
137         if [ $generations -lt 1 ]; then
138                 echo "No generations configured, nothing to compare, skipping system!"
139                 echo; return 1
140         fi
141
142         local latest_d="$target/latest"
143         if [[ ! -d "$latest_d" || ! -r "$latest_d/.stamp" ]]; then
144                 echo "Failed to access latest backup generation in \"$latest_d\", skipping system!"
145                 echo; return 1
146         fi
147         echo "Found latest generation in \"$latest_d\"."
148
149         declare -i code=-1
150         # shellcheck source=/dev/null
151         source "$latest_d/.stamp"
152
153         if [[ $code -ne 0 ]]; then
154                 echo "Warning: Last backup generation had errors, code $code!"
155         fi
156
157         # Search previous generation without errors
158         local previous_d=""
159         # shellcheck disable=SC2045
160         for d in $(ls -1dt "$target/"[0-9]*-[0-9]* 2>/dev/null); do
161                 [[ -d "$d" && -r "$d/.stamp" ]] || return 0
162
163                 declare -i code=-1
164                 # shellcheck source=/dev/null
165                 source "$d/.stamp"
166
167                 if [[ $code -eq 0 || $code -eq 24 ]]; then
168                         previous_d="$d"
169                         break
170                 fi
171         done
172         if [[ -z "$previous_d" || ! -d "$previous_d" || ! -r "$previous_d/.stamp" ]]; then
173                 echo "Failed to find previous successfull backup generation, skipping system!"
174                 echo; return 1
175         fi
176         echo "Comparing with generation in $previous_d ..."
177
178         DiffGenerations "$backup_type" "$previous_d" "$latest_d" "$files"
179         return_code=$?
180
181         echo
182         return $return_code
183 }
184
185 DiffGenerations() {
186         local backup_type="$1"
187         local gen1_d="$2"
188         local gen2_d="$3"
189         local files="$4"
190
191         local return_code=0
192
193         if [[ "$backup_type" == "rsync" ]]; then
194                 # rsync Backup Type
195
196                 for file in \
197                         /etc/passwd \
198                         /etc/shadow \
199                         /etc/group \
200                         /etc/gshadow \
201                         \
202                         /boot/grub/grub.cfg \
203                         /etc/aliases \
204                         /etc/bash.bashrc \
205                         /etc/crontab \
206                         /etc/debian_version \
207                         /etc/environment \
208                         /etc/fstab \
209                         /etc/hostname \
210                         /etc/hosts \
211                         /etc/hosts.allow \
212                         /etc/hosts.deny \
213                         /etc/inittab \
214                         /etc/ld.so.conf \
215                         /etc/login.defs \
216                         /etc/machine-id \
217                         /etc/modules \
218                         /etc/network/interfaces \
219                         /etc/networks \
220                         /etc/nsswitch.conf \
221                         /etc/profile \
222                         /etc/rc.local \
223                         /etc/resolv.conf \
224                         /etc/services \
225                         /etc/shells \
226                         /etc/ssh/sshd_config \
227                         /etc/sshd_config \
228                         /etc/sudoers \
229                         /etc/sysctl.conf \
230                 ; do
231                         [[ -r "${gen1_d}${file}" ]] || continue
232
233                         [[ $VERBOSE -ne 0 ]] && echo "Checking \"$file\" ..."
234                         if ! diff -U 3 "${gen1_d}${file}" "${gen2_d}${file}" >"$tmp_diff"; then
235                                 BeginDiff "\"$file\""
236                                 tail -n +3 "$tmp_diff" | PipeDiff
237                                 EndDiff
238                                 return_code=1
239                         fi
240                 done
241
242                 for dir in \
243                         / \
244                         /etc/cron.d/ \
245                         /etc/cron.daily/ \
246                         /etc/cron.hourly/ \
247                         /etc/cron.monthly/ \
248                         /etc/cron.weekly/ \
249                         /etc/init.d/ \
250                         /etc/sudoers.d/ \
251                         /var/log/dumps/ \
252                 ; do
253                         [[ ! -d "${gen1_d}${dir}" ]] && continue
254                         [[ ! -d "${gen2_d}${dir}" ]] && continue
255
256                         # Make sure that this is a system root; comparing other
257                         # root folders results in misleading output ...
258                         [[ "$dir" == "/" && ! -d "${gen1_d}${dir}/etc" ]] && continue
259
260                         [[ $VERBOSE -ne 0 ]] && echo "Checking \"$dir\" ..."
261                         ListDirectory "${gen1_d}" "${dir}" >"$tmp_1"
262                         ListDirectory "${gen2_d}" "${dir}" >"$tmp_2"
263                         if ! diff -U 0 "$tmp_1" "$tmp_2" >"$tmp_diff"; then
264                                 BeginDiff "\"$dir\" directory"
265                                 tail -n +3 "$tmp_diff" | grep -Ev '^@@ ' | PipeDiff
266                                 EndDiff
267                                 return_code=1
268                         fi
269                 done
270
271                 for dir in \
272                         /etc/systemd/network/ \
273                         /etc/systemd/system/ \
274                         /etc/systemd/user/ \
275                         /lib/systemd/network/ \
276                         /lib/systemd/system/ \
277                         /lib/systemd/user/ \
278                         /run/systemd/system/ \
279                         /usr/lib/systemd/network/ \
280                         /usr/lib/systemd/system/ \
281                         /usr/lib/systemd/user/ \
282                 ; do
283                         [[ ! -d "${gen1_d}${dir}" ]] && continue
284                         [[ ! -d "${gen2_d}${dir}" ]] && continue
285
286                         # Make sure that this is a system root; comparing other
287                         # root folders results in misleading output ...
288                         [[ "$dir" == "/" && ! -d "${gen1_d}${dir}/etc" ]] && continue
289
290                         [[ $VERBOSE -ne 0 ]] && echo "Checking systemd hierarchy \"$dir\" ..."
291                         ListFilesRecursive "${gen1_d}" "${dir}" >"$tmp_1"
292                         ListFilesRecursive "${gen2_d}" "${dir}" >"$tmp_2"
293                         if ! diff -U 0 "$tmp_1" "$tmp_2" >"$tmp_diff"; then
294                                 BeginDiff "\"$dir\" directory"
295                                 tail -n +3 "$tmp_diff" | grep -Ev '^@@ ' | PipeDiff
296                                 EndDiff
297                                 return_code=1
298                         fi
299                 done
300
301                 if [[ -d "${gen1_d}/var/lib/dpkg/info" && -d "${gen2_d}/var/lib/dpkg/info" ]]; then
302                         [[ $VERBOSE -ne 0 ]] && echo "Checking list of installed packages ..."
303                         chroot "${gen1_d}" dpkg --get-selections >"$tmp_1" || return 2
304                         chroot "${gen2_d}" dpkg --get-selections >"$tmp_2" || return 2
305                         if ! diff -U 0 "$tmp_1" "$tmp_2" >"$tmp_diff"; then
306                                 BeginDiff "list of installed packages"
307                                 tail -n +3 "$tmp_diff" | grep -v '^@@ ' | PipeDiff
308                                 EndDiff
309                                 return_code=1
310                         fi
311                 fi
312         elif [[ "$backup_type" == "scp" ]]; then
313                 # scp Backup type
314                 file=$(basename "$files")
315                 [[ $VERBOSE -ne 0 ]] && echo "Checking \"$file\" ..."
316                 if ! diff -U 3 "${gen1_d}/${file}" "${gen2_d}/${file}" >"$tmp_diff"; then
317                         BeginDiff "\"$file\""
318                         tail -n +3 "$tmp_diff" | PipeDiff
319                         EndDiff
320                         return_code=1
321                 fi
322         else
323                 echo "Backup type \"$backup_type\" undefined, \"$system\" skipped!"
324                 echo; return 2
325         fi
326
327         return $return_code
328 }
329
330 MkTempFiles() {
331         tmp_1=$(mktemp "/tmp/$NAME.XXXXXX") || exit 1
332         tmp_2=$(mktemp "/tmp/$NAME.XXXXXX") || exit 1
333         tmp_diff=$(mktemp "/tmp/$NAME.XXXXXX") || exit 1
334         tmp_out=$(mktemp "/tmp/$NAME.XXXXXX") || exit 1
335 }
336
337 CleanUp() {
338         rm -f "$tmp_1" "$tmp_2" "$tmp_diff" "$tmp_out"
339 }
340
341 while [[ $# -gt 0 ]]; do
342         case "$1" in
343           "-d"|"--dirs")
344                 shift
345                 [[ $# -eq 2 ]] || Usage
346                 MkTempFiles
347                 DiffGenerations "$default_backup_type" "$1" "$2" "$default_files"
348                 return_code=$?
349                 CleanUp
350                 exit $return_code
351                 ;;
352           "-q"|"--quiet")
353                 QUIET=1; shift
354                 ;;
355           "-v"|"--verbose")
356                 VERBOSE=1; shift
357                 ;;
358           "-"*)
359                 Usage
360                 ;;
361           *)
362                 break
363         esac
364 done
365
366 if [[ $# -ge 1 ]]; then
367         for s in "$@"; do
368                 if [ ! -r "${conf_d}/$s" ]; then
369                         echo "$NAME: Can' read \"${conf_d}/$s\"!"
370                         exit 1
371                 fi
372                 sys+=("${conf_d}/$s")
373         done
374 else
375         sys=("${conf_d}/"*)
376 fi
377
378 MkTempFiles
379 for f in "${sys[@]}"; do
380         [[ -r "$f" && -f "$f" ]] || continue
381
382         fname=$(basename "$f")
383         case "$fname" in
384                 "backup-script.conf"|*.sh)
385                         continue
386                         ;;
387         esac
388
389         HandleSystem "$fname" >"$tmp_out" 2>&1; result=$?
390         [[ $QUIET -eq 0 || $result -ne 0 ]] && cat "$tmp_out"
391 done
392 CleanUp
393
394 # -eof-