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