3 # backup-script system for cloning systems using rsync
4 # Copyright (c)2008-2016 Alexander Barton, alex@barton.de
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.
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"
25 default_backup_type="rsync"
26 default_files="running-config"
28 default_target="/var/backups"
34 # Search configuration file (last one is used as default!)
36 "/usr/local/etc/backup-script.conf" \
37 "/etc/backup-script.conf" \
38 "${conf_d}/backup-script.conf" \
39 "/usr/local/etc/backup-script.conf" \
41 if [ -r "$conf" ]; then
42 # shellcheck source=/dev/null
49 echo "Usage: $NAME [-q|--quiet] [-v|--verbose] [<job> [<job> [...]]]"
50 echo " $NAME <-d|--dirs> <dir1> <dir2>"
52 echo " -d, --dirs Compare two backup directories (not jobs)."
53 echo " -q, --quiet Quite mode, only list jobs with changes or errors."
54 echo " -v, --verbose Verbose mode, show all checks that are run."
56 echo "When no <job> is given, all defined jobs are checked."
62 echo "Differences in $*:"
68 while read -r line; do
84 if [[ "$dir_name" == "/" ]]; then
85 exclude="$exclude"'| \.stamp$| dev$| etc$| proc$| root$| run$| sys$| tmp$'
86 exclude="$exclude"'| data$| net$| srv$'
87 exclude="$exclude"'| [[:alnum:]_-]+\.log(\.[[:alnum:]]+|)$'
90 # shellcheck disable=SC2012
91 ls -Al "$base_dir$dir_name" 2>/dev/null \
92 | egrep -v "($exclude)" | awk '!($2="")' | column -t
99 local backup_type="$default_backup_type"
100 local files="$default_files"
101 local generations="$default_generations"
103 local system="$fname"
104 local target="$default_target"
106 # Read in system configuration file
107 # shellcheck source=/dev/null
110 target="$target/$(basename "$f")"
112 [[ -d "$target" ]] || return 0
115 [[ "$system" == "$fname" ]] \
116 && systxt="\"$system\"" \
117 || systxt="\"$fname\" [\"$system\"]"
118 [[ "$local" -eq 0 ]] \
119 && echo "Checking $systxt ..." \
120 || echo "Checking $systxt (local system) ..."
122 # Check if job is disabled
123 if [[ "$backup_type" == "disabled" ]]; then
124 echo "Job is DISABLED and will be skipped."
128 if [ $generations -lt 1 ]; then
129 echo "No generations configured, nothing to compare, skipping system!"
133 local latest_d="$target/latest"
134 if [[ ! -d "$latest_d" || ! -r "$latest_d/.stamp" ]]; then
135 echo "Failed to access latest backup generation in \"$latest_d\", skipping system!"
138 echo "Found latest generation in \"$latest_d\"."
141 # shellcheck source=/dev/null
142 source "$latest_d/.stamp"
144 if [[ $code -ne 0 && $code -ne 24 ]]; then
145 echo "Last backup generation has errors, skipping system!"
149 # Search previous generation without errors
151 # shellcheck disable=SC2045
152 for d in $(ls -1dt "$target/"[0-9]*-[0-9]* 2>/dev/null); do
153 [[ -d "$d" && -r "$d/.stamp" ]] || return 0
156 # shellcheck source=/dev/null
159 if [[ $code -eq 0 || $code -eq 24 ]]; then
164 if [[ -z "$previous_d" || ! -d "$previous_d" || ! -r "$previous_d/.stamp" ]]; then
165 echo "Failed to find previous successfull backup generation, skipping system!"
168 echo "Comparing with generation in $previous_d ..."
170 DiffGenerations "$backup_type" "$previous_d" "$latest_d" "$files"
178 local backup_type="$1"
185 if [[ "$backup_type" == "rsync" ]]; then
194 /boot/grub/grub.cfg \
209 /etc/network/interfaces \
217 /etc/ssh/sshd_config \
222 [[ -r "${gen1_d}${file}" ]] || continue
224 [[ $VERBOSE -ne 0 ]] && echo "Checking \"$file\" ..."
225 if ! diff -U 3 "${gen1_d}${file}" "${gen2_d}${file}" >"$tmp_diff"; then
226 BeginDiff "\"$file\""
227 tail -n +3 "$tmp_diff" | PipeDiff
243 [[ ! -d "${gen1_d}${dir}" ]] && continue
244 [[ ! -d "${gen2_d}${dir}" ]] && continue
246 # Make sure that this is a system root; comparing other
247 # root folders results in misleading output ...
248 [[ "$dir" == "/" && ! -d "${gen1_d}${dir}/etc" ]] && continue
250 [[ $VERBOSE -ne 0 ]] && echo "Checking \"$dir\" ..."
251 ListDirectory "${gen1_d}" "${dir}" >"$tmp_1"
252 ListDirectory "${gen2_d}" "${dir}" >"$tmp_2"
253 if ! diff -U 0 "$tmp_1" "$tmp_2" >"$tmp_diff"; then
254 BeginDiff "\"$dir\" directory"
255 tail -n +3 "$tmp_diff" | egrep -v '^@@ ' | PipeDiff
261 if [[ -d "${gen1_d}/var/lib/dpkg/info" && -d "${gen2_d}/var/lib/dpkg/info" ]]; then
262 [[ $VERBOSE -ne 0 ]] && echo "Checking list of installed packages ..."
263 chroot "${gen1_d}" dpkg --get-selections >"$tmp_1" || return 2
264 chroot "${gen2_d}" dpkg --get-selections >"$tmp_2" || return 2
265 if ! diff -U 0 "$tmp_1" "$tmp_2" >"$tmp_diff"; then
266 BeginDiff "list of installed packages"
267 tail -n +3 "$tmp_diff" | grep -v '^@@ ' | PipeDiff
272 elif [[ "$backup_type" == "scp" ]]; then
274 file=$(basename "$files")
275 [[ $VERBOSE -ne 0 ]] && echo "Checking \"$file\" ..."
276 if ! diff -U 3 "${gen1_d}/${file}" "${gen2_d}/${file}" >"$tmp_diff"; then
277 BeginDiff "\"$file\""
278 tail -n +3 "$tmp_diff" | PipeDiff
283 echo "Backup type \"$backup_type\" undefined, \"$system\" skipped!"
291 tmp_1=$(mktemp "/tmp/$NAME.XXXXXX") || exit 1
292 tmp_2=$(mktemp "/tmp/$NAME.XXXXXX") || exit 1
293 tmp_diff=$(mktemp "/tmp/$NAME.XXXXXX") || exit 1
294 tmp_out=$(mktemp "/tmp/$NAME.XXXXXX") || exit 1
298 rm -f "$tmp_1" "$tmp_2" "$tmp_diff" "$tmp_out"
301 while [[ $# -gt 0 ]]; do
305 [[ $# -eq 2 ]] || Usage
307 DiffGenerations "$default_backup_type" "$1" "$2" "$default_files"
326 if [[ $# -ge 1 ]]; then
328 if [ ! -r "${conf_d}/$s" ]; then
329 echo "$NAME: Can' read \"${conf_d}/$s\"!"
332 sys+=("${conf_d}/$s")
339 for f in "${sys[@]}"; do
340 [[ -r "$f" && -f "$f" ]] || continue
342 fname=$(basename "$f")
344 "backup-script.conf"|*.sh)
349 HandleSystem "$fname" >"$tmp_out" 2>&1; result=$?
350 [[ $QUIET -eq 0 || $result -ne 0 ]] && cat "$tmp_out"