]> arthur.barton.de Git - backup-script.git/blob - bin/backup-script
Output warning and error messages to stderr
[backup-script.git] / bin / backup-script
1 #!/bin/bash
2 #
3 # backup-script system for cloning systems using rsync
4 # Copyright (c)2008-2017 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 PIDFILE="/var/run/$NAME.pid"
15
16 DRYRUN=0
17 VERBOSE=0
18 TAG=""
19 PREPOSTEXEC=1
20
21 export LC_ALL=C
22
23 declare -i count_all=0
24 declare -i count_started=0
25 declare -i count_ok=0
26 declare -i count_ok_vanished=0
27 declare -i count_enabled=0
28
29 destinations=""
30
31 # Default settings, can be overwritten in backup-script.conf:
32 [ -d "/usr/local/etc/backup-script.d" ] \
33         && conf_d="/usr/local/etc/backup-script.d" \
34         || conf_d="/etc/backup-script.d"
35 setup_exec=""
36 pre_exec=""
37 post_exec=""
38 default_backup_type="rsync"
39 default_source_root="/"
40 default_files="running-config"
41 default_target="/var/backups"
42 default_user="root"
43 default_ssh_args_add=""
44 default_rsync_args_add=""
45 default_exclude_args_add=""
46 default_exclude_dirs_add=""
47 default_compress=1
48 default_ping=1
49 default_local=0
50 default_generations=0
51 default_io_timeout="1800"
52 default_job_pre_exec=""
53 default_job_post_exec=""
54 default_tags=""
55
56 Usage() {
57         {
58                 echo "Usage: $NAME [<options>] [<job> [<job> [...]]]"
59                 echo
60                 echo "  -n, --dry-run       Test run only, don't copy any data."
61                 echo "  -p, --progress      Show progress, see rsync(1)."
62                 echo "  -t TAG, --tag TAG   Only run jobs with tag TAG."
63                 echo "  -x, --no-exec       Don't run global pre-/post-exec commands."
64                 echo
65                 echo "When no <job> is given, all defined systems are used."
66                 echo
67                 # shellcheck disable=SC2086
68                 echo -e $config_info
69                 echo
70         } >&2
71         exit 2
72 }
73
74 ErrorMsg () {
75         printf "%s\n" "$@" >&2
76 }
77
78 CleanUp() {
79         if [[ -n "$pre_exec" && $PREPOSTEXEC -ne 0 ]]; then
80                 echo "Executing \"$post_exec\" ..."
81                 if ! sh -c $post_exec; then
82                         ErrorMsg "Warning: post-exec command failed!"
83                 fi
84                 echo
85         fi
86         rm -f "$PIDFILE"
87 }
88
89 GotSignal() {
90         echo
91         ErrorMsg "--> Got signal, cleaning up & aborting ..."
92         echo
93         CleanUp
94         ErrorMsg -n "Aborted: " >&2; date
95         echo
96         sleep 3
97         exit 9
98 }
99
100 ExecJob() {
101         local what="$1"
102         local cmd="$2"
103
104         echo "Running job ${what}-exec command ..."
105         [ "$local" -eq 0 ] \
106                 && cmd="$ssh_cmd ${user}@${system} $cmd"
107         echo -n "Start date (${what}-exec): "; date
108         echo "$cmd"
109         if [ "$DRYRUN" -eq 0 ]; then
110                 $SHELL -c "$cmd"; local ret=$?
111         else
112                 echo " *** Trial run, not executing ${what}-exec command!"
113                 ret=0
114         fi
115         if [ $ret -eq 0 ]; then
116                 echo "The ${what}-exec command completed with status 0, OK."
117         else
118                 ErrorMsg "The ${what}-exec command completed with ERRORS, code $ret!"
119         fi
120         return $ret
121 }
122
123 GetFS() {
124         local dir="$1"
125
126         while [ -n "$dir" ]; do
127                 findmnt -fn -o FSTYPE --raw "$dir" 2>/dev/null; local r=$?
128                 if [ $r -eq 0 ]; then
129                         return 0
130                 elif [ $r -eq 127 ]; then
131                         echo "UNKNOWN"
132                         return 1
133                 fi
134                 dir=$(dirname "$dir") || return 1
135         done
136 }
137
138 CreateSubvolume() {
139         local volume="$1"
140         local fs
141         local dir
142
143         dir=$(dirname "$volume")
144         fs=$(GetFS "$dir")
145         case "$fs" in
146           "btrfs")
147                 btrfs subvolume create "$volume"  >/dev/null || return 1
148                 ;;
149           "zfs")
150                 zfs create "$(echo "$volume" | cut -c2-)" || return 1
151                 ;;
152           *)
153                 ErrorMsg "CreateSubvolume: Incompatible FS type \"$fs\" on \"$dir\"!"
154                 return 9
155         esac
156         return 0
157 }
158
159 CloneSubvolume() {
160         local source="$1"
161         local volume="$2"
162         local snapshot="$3"
163         local dir
164         local fs
165         local link_name
166
167         dir=$(dirname "source")
168         fs=$(GetFS "$source")
169         case "$fs" in
170           "btrfs")
171                 btrfs subvolume snapshot "$source" "$snapshot"  >/dev/null || return 1
172                 ;;
173           "zfs")
174                 zfs snapshot "$snapshot" || return 1
175                 link_name="$(echo "$snapshot" | cut -d@ -f2-)"
176                 ln -s \
177                         "current/.zfs/snapshot/$link_name" \
178                         "$(dirname "$volume")/$link_name"
179                 ;;
180           *)
181                 ErrorMsg "CloneSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
182                 return 9
183         esac
184         return 0
185 }
186
187 RenameSubvolume() {
188         local source="$1"
189         local target="$2"
190         local fs
191
192         fs=$(GetFS "$source")
193         case "$fs" in
194           "btrfs")
195                 mv "$source" "$target" || return 1
196                 ;;
197           "zfs")
198                 zfs rename \
199                   "$(echo "$source" | cut -c2-)" \
200                   "$(echo "$target" | cut -c2-)" \
201                         || return 1
202                 ;;
203           *)
204                 ErrorMsg "RenameSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
205                 return 9
206         esac
207         return 0
208 }
209
210 DeleteSubvolume() {
211         local volume="$1"
212         local fs
213         local id
214         local snapshot
215
216         fs=$(GetFS "$volume")
217         case "$fs" in
218           "btrfs")
219                 btrfs subvolume delete "$volume" >/dev/null || return 1
220                 ;;
221           "zfs")
222                 id="$(basename "$volume")"
223                 if [ -h "$volume" ]; then
224                         snapshot="$(dirname "$volume")/current@$id"
225                 else
226                         snapshot="$volume"
227                 fi
228                 zfs destroy -r "$(echo "$snapshot" | cut -c2-)" >/dev/null || return 1
229                 [ -h "$volume" ] && rm "$volume"
230                 ;;
231           *)
232                 ErrorMsg "DeleteSubvolume: Incompatible FS type \"$fs\" on \"$volume\"!"
233                 return 9
234         esac
235         return 0
236 }
237
238 Initialize_Last_SysTarget_Snapshot() {
239         sys_target="$1"
240         unset last
241         unset snapshot
242
243         fs=$(GetFS "$sys_target")
244         case "$fs" in
245           "btrfs")
246                 # Search directory of last generation, if any
247                 # shellcheck disable=SC2012
248                 last=$(ls -1d "$sys_target"/[0-9]* 2>/dev/null | sort -r | head -n1)
249                 if [ -n "$last" ]; then
250                         if [ ! -d "$last" ]; then
251                                 ErrorMsg "Last snapshot \"$last\" seems not to be a directory!? \"$system\" skipped!"
252                                 echo
253                                 return 1
254                         fi
255                 fi
256                 sys_target="$sys_target/$(date +%Y%m%d-%H%M%S)"
257                 snapshot="$sys_target"
258                 ;;
259           "zfs")
260                 # On ZFS, the last generation is always named "current"
261                 if [ -e "$sys_target/current" ]; then
262                         last="$sys_target/current"
263                         if [ "$(uname)" = "Linux" ]; then
264                                 date=$(LC_ALL=C stat "$1" | grep "^Modify: " \
265                                  | cut -d':' -f2- | cut -d. -f1)
266                         else
267                                 date=$(LC_ALL=C stat -f "%Sc" "$1")
268                         fi
269                         date=$(echo "$date" | sed -e's/^ //g' -e 's/[-:]//g' -e 's/ /-/g')
270
271                 else
272                         last=""
273                         date="$(date +%Y%m%d-%H%M%S)"
274                 fi
275                 snapshot="$(echo "$sys_target/current" | cut -c2-)@$date"
276                 sys_target="$sys_target/current"
277                 ;;
278           *)
279                 ErrorMsg "Initialize_Last_SysTarget_Snapshot: Incompatible FS type \"$fs\" on \"$sys_target\"!"
280                 return 1
281         esac
282         return 0
283 }
284
285 # Search configuration file (last one is used as default!)
286 for conf in \
287         "/usr/local/etc/backup-script.conf" \
288         "/etc/backup-script.conf" \
289         "${conf_d}/backup-script.conf" \
290         "/usr/local/etc/backup-script.conf" \
291 ; do
292         [ -r "$conf" ] && break
293 done
294
295 # Read in configuration file
296 config_info="Configuration file is \"$conf\""
297 if [ -r "$conf" ]; then
298         # shellcheck source=/dev/null
299         source "$conf"
300 else
301         config_info="${config_info} (not readable, using defaults)"
302 fi
303 config_info="${config_info},\nusing \"$conf_d\" as configuration directory."
304
305 while [ $# -gt 0 ]; do
306         case "$1" in
307           "-n"|"--dry-run")
308                 DRYRUN=1; shift
309                 ;;
310           "-p"|"--progress")
311                 VERBOSE=1; shift
312                 ;;
313           "-t"|"--tag")
314                 shift; TAG="$1"; shift
315                 [ -n "$TAG" ] || Usage
316                 ;;
317           "-x"|"--no-exec")
318                 PREPOSTEXEC=0; shift
319                 ;;
320           "-"*)
321                 Usage
322                 ;;
323           *)
324                 break
325         esac
326 done
327
328 echo -n "Started: "; date
329 echo -e "$config_info"
330
331 # Check rsync and its protocol version
332 if ! rsync=$(which "rsync" 2>/dev/null); then
333         ErrorMsg "Failed to detect rsync(1)! Is it installed in your \$PATH?"
334         exit 1
335 fi
336 if ! rsync_proto=$($rsync --version 2>/dev/null | head -n 1 | sed 's/.*  protocol version \([0-9]*\)$/\1/'); then
337         ErrorMsg "Failed to detect protocol version of $rsync!"
338         exit 1
339 fi
340 echo "Rsync command is $rsync, protocol version $rsync_proto."
341
342 [[ -n "$TAG" ]] && echo "Running jobs tagged with \"$TAG\"."
343 echo
344
345 if [ $# -ge 1 ]; then
346         for s in "$@"; do
347                 if [ ! -r "${conf_d}/$s" ]; then
348                         ErrorMsg "$NAME: Can' read \"${conf_d}/$s\"!"
349                         exit 3
350                 fi
351                 sys+=("${conf_d}/$s")
352         done
353 else
354         sys=("${conf_d}/"*)
355 fi
356
357 if [[ -n "$setup_exec" && $PREPOSTEXEC -ne 0 ]]; then
358         echo "Executing \"$setup_exec\" ..."
359         if ! sh -c $setup_exec; then
360                 ErrorMsg "Error: setup command failed!"; echo
361                 ErrorMsg "Aborting backup."; echo
362                 exit 5
363         fi
364         sleep 2
365         echo
366 fi
367
368 trap GotSignal SIGINT SIGTERM
369
370 # check and create PID file
371 if [ -e "$PIDFILE" ]; then
372         ErrorMsg "Lockfile \"$PIDFILE\" already exists."
373         ErrorMsg "Is an other instance still running?"
374         echo
375         ErrorMsg -n "Aborted: " >&2; date
376         echo
377         exit 4
378 fi
379 if ! touch "$PIDFILE" 2>/dev/null; then
380         ErrorMsg "Warning: can't create PID file \"$PIDFILE\"!"
381         echo
382 else
383         echo "$$" >>"$PIDFILE"
384 fi
385
386 if [[ -n "$pre_exec" && $PREPOSTEXEC -ne 0 ]]; then
387         echo "Executing \"$pre_exec\" ..."
388         if ! sh -c $pre_exec; then
389                 ErrorMsg "Error: pre-exec command failed!"; echo
390                 CleanUp
391                 ErrorMsg "Aborting backup."; echo
392                 exit 5
393         fi
394         sleep 2
395         echo
396 fi
397
398 for f in "${sys[@]}"; do
399         [[ -r "$f" && -f "$f" ]] || continue
400
401         fname=$(basename "$f")
402         case "$fname" in
403                 "backup-script.conf"|*.sh)
404                         continue
405                         ;;
406         esac
407
408         # Set global defaults
409         system="$fname"
410         backup_type="$default_backup_type"
411         user="$default_user"
412         source_root="$default_source_root"
413         files="$default_files"
414         target="$default_target"
415         ssh_args_add="$default_ssh_args_add"
416         rsync_args_add="$default_rsync_args_add"
417         exclude_args_add="$default_exclude_args_add"
418         exclude_dirs_add="$default_exclude_dirs_add"
419         compress="$default_compress"
420         ping="$default_ping"
421         local="$default_local"
422         generations="$default_generations"
423         job_pre_exec="$default_job_pre_exec"
424         job_post_exec="$default_job_post_exec"
425         tags="$default_tags"
426         io_timeout="$default_io_timeout"
427
428         # Compatibility with backup-pull(1) script: Save global values ...
429         pre_exec_saved="$pre_exec"
430         post_exec_saved="$post_exec"
431
432         # Compatibility with backup-pull(1) script: Set defaults
433         host=""
434         unset source
435         unset pre_exec
436         unset post_exec
437
438         # Read in system configuration file
439         # shellcheck source=/dev/null
440         source "$f"
441
442         # Compatibility with backup-pull(1) script: Fix up configuration
443         [[ "$system" = "$fname" && -n "$host" ]] \
444                 && system="$host"
445         [[ "$source_root" = "$default_source_root" && -n "$source" ]] \
446                 && source_root="$source"
447         [[ -z "$job_pre_exec" && -n "$pre_exec" ]] \
448                 && job_pre_exec="$pre_exec"
449         [[ -z "$job_post_exec" && -n "$post_exec" ]] \
450                 && job_post_exec="$post_exec"
451
452         # Compatibility with backup-pull(1) script: Restore global values ...
453         pre_exec="$pre_exec_saved"
454         post_exec="$post_exec_saved"
455
456         # Validate configuration
457         if [[ "$system" = "localhost" || "$system" = "127.0.0.1" ]]; then
458                 # Local system
459                 local=1
460                 compress=0
461         fi
462
463         # Add "NONE" tag when no tags are given in the config file:
464         [[ -z "$tags" ]] && tags="NONE"
465         # Add "auto-tags":
466         [[ "$local" -eq 1 ]] && tags="$tags,LOCAL"
467         # Check tags
468         if [[ -n "$TAG" && "$TAG" != "ALL" ]]; then
469                 if ! echo "$tags" | grep -E "(^|,)$TAG(,|$)" >/dev/null 2>&1; then
470                         if [ "$DRYRUN" -ne 0 ]; then
471                                 echo "Tags of system \"$system\" don't match \"$TAG\": \"$tags\". Skipped."
472                                 echo
473                         fi
474                         continue
475                 fi
476         fi
477
478         # Make sure "source_root" ends with a slash ("/")
479         case "$source_root" in
480           *"/")
481                 ;;
482           *)
483                 source_root="$source_root/"
484         esac
485
486         # Make sure "target" DOESN'T end with a slash ("/")
487         case "$target" in
488           "*/")
489                 target=$( echo "$target" | sed -e 's/\/$//g' )
490                 ;;
491         esac
492
493         [ "$system" = "$fname" ] \
494                 && systxt="\"$system\"" \
495                 || systxt="\"$fname\" [\"$system\"]"
496         [ "$local" -eq 0 ] \
497                 && echo "Working on $systxt ..." \
498                 || echo "Working on $systxt (local system) ..."
499
500         count_all=$count_all+1
501
502         # Check if job is disabled
503         if [ "$backup_type" = "disabled" ]; then
504                 echo "Job is DISABLED and will be skipped."
505                 echo; continue
506         fi
507
508         count_enabled=$count_enabled+1
509
510         # Check target directory
511         if [ -z "$target" ]; then
512                 ErrorMsg "No target directory specified for \"$system\"!? Skipped!"
513                 echo; continue
514         fi
515         if [ ! -d "$target" ]; then
516                 ErrorMsg "Target \"$target\" is not a directory!? \"$system\" skipped!"
517                 echo; continue
518         fi
519
520         sys_target="$target/$fname"
521         sys_root="$sys_target"
522         if [[ "$DRYRUN" -eq 0 && ! -e "$sys_target" ]]; then
523                 if [ $generations -gt 0 ]; then
524                         CreateSubvolume "$sys_target"; r=$?
525                 else
526                         mkdir -p "$sys_target"; r=$?
527                 fi
528                 if [ $r -ne 0 ]; then
529                         ErrorMsg "Can't create \"$sys_target\"!? \"$system\" skipped!"
530                         echo; continue
531                 fi
532         fi
533
534         if [[ "$local" -eq 0 && "$ping" -ne 0 ]]; then
535                 # Check if system is alive
536                 if ! ping -c 1 "$system" >/dev/null 2>&1; then
537                         ErrorMsg "Host \"$system\" seems not to be alive!? Skipped."
538                         echo; continue
539                 fi
540                 echo "OK, host \"$system\" seems to be alive."
541         fi
542
543         if [ $generations -gt 0 ]; then
544                 # Make sure no old backup is stored in system directory
545                 if [ -e "$sys_target/.stamp" ]; then
546                         # There seems to be a genearation-less backup in the
547                         # target directory!
548                         ErrorMsg "Target directory \"$sys_target\" seems to be unclean!? \"$system\" skipped!"
549                         echo; continue
550                 fi
551
552                 Initialize_Last_SysTarget_Snapshot "$sys_target" || continue
553
554                 if [[ -n "$last" && ! -e "$last/.stamp" ]]; then
555                         # Old backup directory without "stamp file", continue
556                         echo "Found incomplete snapshot in \"$last\", reusing and renaming it ..."
557                         if [ "$DRYRUN" -eq 0 ]; then
558                                 if ! RenameSubvolume "$last" "$sys_target"; then
559                                         ErrorMsg "Failed to rename last snapshot \"$last\" to \"$sys_target\"!? \"$system\" skipped!"
560                                         echo; continue
561                                 fi
562                         else
563                                 echo " *** Trial run, not renaming snapshot \"$last\" to \"$sys_target\"!"
564                         fi
565                 elif [ -n "$last" ]; then
566                         # Old backup directory found, create new snapshot
567                         echo "Found last snapshot in \"$last\"."
568                         if [ "$DRYRUN" -eq 0 ]; then
569                                 CloneSubvolume "$last" "$sys_target" "$snapshot"; r=$?
570                                 if [ $r -ne 0 ]; then
571                                         ErrorMsg "Can't create snapshot \"$snapshot\" of \"$last\", code $r!? \"$system\" skipped!"
572                                         echo; continue
573                                 fi
574                                 echo "Created new snapshot in \"$snapshot\"."
575                         else
576                                 echo " *** Trial run, not creating new snapshot in \"$snapshot\"!"
577                         fi
578                 else
579                         # No old backup found, create new subvolume
580                         if [ "$DRYRUN" -eq 0 ]; then
581                                 CreateSubvolume "$sys_target"; r=$?
582                                 if [ $r -ne 0 ]; then
583                                         ErrorMsg "Can't create subvolume \"$sys_target\", code $r!? \"$system\" skipped!"
584                                         echo; continue
585                                 fi
586                                 echo "Created new subvolume in \"$sys_target\"."
587                         else
588                                 echo " *** Trial run, not creating new subvolume \"$sys_target\"!"
589                         fi
590                 fi
591         fi
592
593         ssh_cmd="ssh"
594         [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
595
596         # execute job "pre-exec" command, if any
597         if [ -n "$job_pre_exec" ]; then
598                 ExecJob pre "$job_pre_exec" ; ret=$?
599                 if [ $ret -ne 0 ]; then
600                         [ $ret -ne 99 ] && count_started=$count_started+1
601                         ErrorMsg "Pre-exec command failed, \"$system\" skipped!"
602                         echo; continue
603                 fi
604         fi
605
606         # prepare (remote) command ...
607         if [[ "$backup_type" == "rsync" ]]; then
608                 cmd="$rsync --archive --timeout=$io_timeout"
609                 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
610                 [ "$local" -eq 0 ] && cmd="$cmd --rsh=\"$ssh_cmd\""
611                 cmd="$cmd --delete-during --delete-excluded --sparse"
612                 if [ "$VERBOSE" -gt 0 ]; then
613                         [ "$rsync_proto" -ge 31 ] \
614                                 && cmd="$cmd --info=progress2" \
615                                 || cmd="$cmd --progress"
616                 fi
617                 set -f
618                 if [ "$source_root" = "$default_source_root" ]; then
619                         for dir in \
620                                 "/dev/**" \
621                                 "/media/**" \
622                                 "/mnt/**" \
623                                 "/net/**" \
624                                 "/proc/**" \
625                                 "/run/**" \
626                                 "/sys/**" \
627                                 "/tmp/**" \
628                                 "/var/cache/apt/**" \
629                                 "/var/log/**" \
630                                 "/var/tmp/**" \
631                         ; do
632                                 cmd="$cmd --exclude=$dir"
633                         done
634                 fi
635                 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
636                 for dir in $exclude_dirs_add; do
637                         cmd="$cmd --exclude=$dir"
638                 done
639                 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
640                 set +f
641
642                 [ "$local" -eq 0 ] \
643                         && cmd="$cmd ${user}@${system}:$source_root $sys_target/" \
644                         || cmd="$cmd $source_root $sys_target/"
645         elif [[ "$backup_type" == "scp" ]]; then
646                 cmd="scp"
647                 [ "$VERBOSE" -eq 0 ] && cmd="$cmd -q"
648                 for file in $files; do
649                         cmd="$cmd ${user}@${system}:$file $sys_target/"
650                 done
651         else
652                 ErrorMsg "Backup type \"$backup_type\" undefined, \"$system\" skipped!"
653                 echo; continue
654         fi
655
656         echo "Backing up to \"$sys_target\" ..."
657         echo -n "Start date: "; date
658         echo "$cmd"
659         count_started=$count_started+1
660         ok=0
661
662         if [ "$DRYRUN" -eq 0 ]; then
663                 stamp_file="$sys_target/.stamp"
664                 rm -f "$stamp_file"
665
666                 # Execute backup command:
667                 start_t=$(date "+%s")
668                 $SHELL -c "$cmd"; ret=$?
669                 end_t=$(date "+%s")
670
671                 {
672                         echo "code=$ret"
673                         echo "start_t=$start_t"
674                         echo "end_t=$end_t"
675                         echo "cmd='$cmd'"
676                         echo "backup_host='$(hostname -f)'"
677                         echo "backup_user='$(id -un)'"
678                 } >"$stamp_file"
679         else
680                 echo " *** Trial run, not executing save command!"
681                 ret=0
682         fi
683
684         if [ $ret -eq 20 ]; then
685                 ErrorMsg "Backup of \"$system\" interrupted. Aborting ..."
686                 GotSignal
687         fi
688
689         echo -n "End date: "; date
690         if [[ $ret -eq 0 || $ret -eq 24 ]]; then
691                 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
692
693                 echo "System \"$system\" completed with status $ret, OK."
694                 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
695                 ok=1
696         else
697                 ErrorMsg "System \"$system\" completed with ERRORS, code $ret!"
698         fi
699
700         # execute job "post-exec" command, if any
701         if [ -n "$job_post_exec" ]; then
702                 ExecJob post "$job_post_exec"
703         fi
704
705         if [ $generations -gt 0 ]; then
706                 # Update "latest" symlink
707                 if [ "$DRYRUN" -eq 0 ]; then
708                         rm -f "$sys_root/latest"
709                         ln -s "$sys_target" "$sys_root/latest"
710                 fi
711                 # Clean up old generations
712                 declare -i gen_count=$generations+2
713                 # shellcheck disable=SC2012
714                 to_delete=$(ls -1t "$sys_root" 2>/dev/null | tail -n+$gen_count | sort)
715                 if [[ -n "$to_delete" && $ok -eq 1 ]]; then
716                         [ "$DRYRUN" -eq 0 ] \
717                                 && echo "Deleting old backup generations (keep $generations) ..." \
718                                 || echo " *** Trial run, not deleting old generations:"
719                         for delete in $to_delete; do
720                                 dir="$sys_root/$delete"
721                                 if [ ! -e "$dir/.stamp" ]; then
722                                         ErrorMsg "Not deleting \"$dir\" of \"$system\", not a backup directory!?"
723                                         continue
724                                 fi
725                                 last=$(stat "$dir/.stamp" 2>/dev/null | grep "^Modify: " \
726                                  | cut -d':' -f2- | cut -d. -f1)
727                                 # shellcheck disable=SC2086
728                                 echo "Removing backup from" $last "..."
729                                 if [ "$DRYRUN" -eq 0 ]; then
730                                         DeleteSubvolume "$dir" \
731                                                 || ErrorMsg "Failed to delete \"$dir\" of \"$system\"!"
732                                 fi
733                         done
734                         echo -n "Clean up finished: "; date
735                 elif [ -n "$to_delete" ]; then
736                         ErrorMsg "There have been errors for \"$system\", not cleaning up old generations!"
737                 else
738                         echo "Nothing to clean up (keep up to $generations generations)."
739                 fi
740         fi
741
742         destinations="$destinations $target"
743         echo
744 done
745
746 sync
747
748 if [ "$DRYRUN" -eq 0 ]; then
749         paths=""
750         paths_zfs=""
751         # shellcheck disable=SC2086
752         for dest in $(echo $destinations | sed -e 's/ /\n/g' | sort | uniq); do
753                 fs=$(GetFS "$dest")
754                 case $fs in
755                   "zfs" )
756                         paths_zfs="$paths_zfs $dest"
757                         ;;
758                   *)
759                         paths="$paths $dest"
760                 esac
761         done
762         if [ -n "$paths" ]; then
763                 # shellcheck disable=SC2086
764                 df -h $paths
765                 echo
766         fi
767         if [ -n "$paths_zfs" ]; then
768                 # shellcheck disable=SC2086
769                 zfs list $paths_zfs
770                 echo
771         fi
772 fi
773
774 CleanUp
775
776 echo -n "Done: "; date
777 echo
778 [ $count_all -eq 1 ] && s="" || s="s"
779 [ $count_enabled -eq $count_all ] \
780         && echo " - $count_all job$s defined (all enabled)," \
781         || echo " - $count_all job$s defined ($count_enabled enabled),"
782 [ $count_started -eq 1 ] && s="" || s="s"
783 echo " - $count_started job$s started,"
784 echo " - $count_ok done without errors."
785 echo
786
787 if [ $count_started -ne $count_ok ]; then
788         echo "----->  THERE HAVE BEEN ERRORS!  <-----"
789         echo
790         exit 6
791 elif [ $count_enabled -ne $count_started ]; then
792         exit 7
793 fi
794
795 # -eof-