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