]> arthur.barton.de Git - backup-script.git/blob - bin/backup-script
04572682d78832f7acde423f8b8013f41e7deedd
[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 trap GotSignal SIGINT
334
335 if [ $# -ge 1 ]; then
336         for s in "$@"; do
337                 if [ ! -r "${conf_d}/$s" ]; then
338                         echo "$NAME: Can' read \"${conf_d}/$s\"!"
339                         exit 3
340                 fi
341                 sys+=("${conf_d}/$s")
342         done
343 else
344         sys=("${conf_d}/"*)
345 fi
346
347 if [ -n "$setup_exec" ]; then
348         echo "Executing \"$setup_exec\" ..."
349         sh -c $setup_exec
350         if [ $? -ne 0 ]; then
351                 echo "Error: setup command failed!"; echo
352                 CleanUp
353                 echo "Aborting backup."; echo
354                 exit 5
355         fi
356         sleep 2
357         echo
358 fi
359
360 # check and create PID file
361 if [ -e "$PIDFILE" ]; then
362         echo "Lockfile \"$PIDFILE\" already exists."
363         echo "Is an other instance still running?"
364         echo
365         echo -n "Aborted: "; date
366         echo
367         exit 4
368 fi
369 touch "$PIDFILE" 2>/dev/null
370 if [ $? -ne 0 ]; then
371         echo "Warning: can't create PID file \"$PIDFILE\"!"
372         echo
373 else
374         echo "$$" >>"$PIDFILE"
375 fi
376
377 if [ -n "$pre_exec" ]; then
378         echo "Executing \"$pre_exec\" ..."
379         sh -c $pre_exec
380         if [ $? -ne 0 ]; then
381                 echo "Error: pre-exec command failed!"; echo
382                 CleanUp
383                 echo "Aborting backup."; echo
384                 exit 5
385         fi
386         sleep 2
387         echo
388 fi
389
390 for f in "${sys[@]}"; do
391         [[ -r "$f" && -f "$f" ]] || continue
392
393         fname=$(basename "$f")
394         case "$fname" in
395                 "backup-script.conf"|*.sh)
396                         continue
397                         ;;
398         esac
399
400         # Set global defaults
401         system="$fname"
402         backup_type="$default_backup_type"
403         user="$default_user"
404         source_root="$default_source_root"
405         files="$default_files"
406         target="$default_target"
407         ssh_args_add="$default_ssh_args_add"
408         rsync_args_add="$default_rsync_args_add"
409         exclude_args_add="$default_exclude_args_add"
410         exclude_dirs_add="$default_exclude_dirs_add"
411         compress="$default_compress"
412         ping="$default_ping"
413         local="$default_local"
414         generations="$default_generations"
415         job_pre_exec="$default_job_pre_exec"
416         job_post_exec="$default_job_post_exec"
417         tags="$default_tags"
418         io_timeout="$default_io_timeout"
419
420         # Compatibility with backup-pull(1) script: Save global values ...
421         pre_exec_saved="$pre_exec"
422         post_exec_saved="$post_exec"
423
424         # Compatibility with backup-pull(1) script: Set defaults
425         host=""
426         unset source
427         unset pre_exec
428         unset post_exec
429
430         # Read in system configuration file
431         # shellcheck source=/dev/null
432         source "$f"
433
434         # Compatibility with backup-pull(1) script: Fix up configuration
435         [[ "$system" = "$fname" && -n "$host" ]] \
436                 && system="$host"
437         [[ "$source_root" = "$default_source_root" && -n "$source" ]] \
438                 && source_root="$source"
439         [[ -z "$job_pre_exec" && -n "$pre_exec" ]] \
440                 && job_pre_exec="$pre_exec"
441         [[ -z "$job_post_exec" && -n "$post_exec" ]] \
442                 && job_post_exec="$post_exec"
443
444         # Compatibility with backup-pull(1) script: Restore global values ...
445         pre_exec="$pre_exec_saved"
446         post_exec="$post_exec_saved"
447
448         # Validate configuration
449         if [[ "$system" = "localhost" || "$system" = "127.0.0.1" ]]; then
450                 # Local system
451                 local=1
452                 compress=0
453         fi
454
455         # Add "NONE" tag when no tags are given in the config file:
456         [[ -z "$tags" ]] && tags="NONE"
457         # Add "auto-tags":
458         [[ "$local" -eq 1 ]] && tags="$tags,LOCAL"
459         # Check tags
460         if [[ -n "$TAG" && "$TAG" != "ALL" ]]; then
461                 echo "$tags" | grep -E "(^|,)$TAG(,|$)" >/dev/null 2>&1
462                 if [ $? -ne 0 ]; then
463                         if [ "$DRYRUN" -ne 0 ]; then
464                                 echo "Tags of system \"$system\" don't match \"$TAG\": \"$tags\". Skipped."
465                                 echo
466                         fi
467                         continue
468                 fi
469         fi
470
471         # Make sure "source_root" ends with a slash ("/")
472         case "$source_root" in
473           *"/")
474                 ;;
475           *)
476                 source_root="$source_root/"
477         esac
478
479         # Make sure "target" DOESN'T end with a slash ("/")
480         case "$target" in
481           "*/")
482                 target=$( echo "$target" | sed -e 's/\/$//g' )
483                 ;;
484         esac
485
486         [ "$system" = "$fname" ] \
487                 && systxt="\"$system\"" \
488                 || systxt="\"$fname\" [\"$system\"]"
489         [ "$local" -eq 0 ] \
490                 && echo "Working on $systxt ..." \
491                 || echo "Working on $systxt (local system) ..."
492
493         count_all=$count_all+1
494
495         # Check if job is disabled
496         if [ "$backup_type" = "disabled" ]; then
497                 echo "Job is DISABLED and will be skipped."
498                 echo; continue
499         fi
500
501         count_enabled=$count_enabled+1
502
503         # Check target directory
504         if [ -z "$target" ]; then
505                 echo "No target directory specified for \"$system\"!? Skipped!"
506                 echo; continue
507         fi
508         if [ ! -d "$target" ]; then
509                 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
510                 echo; continue
511         fi
512
513         sys_target="$target/$fname"
514         sys_root="$sys_target"
515         if [[ "$DRYRUN" -eq 0 && ! -e "$sys_target" ]]; then
516                 if [ $generations -gt 0 ]; then
517                         CreateSubvolume "$sys_target"
518                 else
519                         mkdir -p "$sys_target"
520                 fi
521                 if [ $? -ne 0 ]; then
522                         echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
523                         echo; continue
524                 fi
525         fi
526
527         if [[ "$local" -eq 0 && "$ping" -ne 0 ]]; then
528                 # Check if system is alive
529                 ping -c 1 "$system" >/dev/null 2>&1
530                 if [ $? -ne 0 ]; then
531                         echo "Host \"$system\" seems not to be alive!? Skipped."
532                         echo; continue
533                 fi
534                 echo "OK, host \"$system\" seems to be alive."
535         fi
536
537         if [ $generations -gt 0 ]; then
538                 # Make sure no old backup is stored in system directory
539                 if [ -e "$sys_target/.stamp" ]; then
540                         # There seems to be a genearation-less backup in the
541                         # target directory!
542                         echo "Target directory \"$sys_target\" seems to be unclean!? \"$system\" skipped!"
543                         echo; continue
544                 fi
545
546                 Initialize_Last_SysTarget_Snapshot "$sys_target" || continue
547
548                 if [[ -n "$last" && ! -e "$last/.stamp" ]]; then
549                         # Old backup directory without "stamp file", continue
550                         echo "Found incomplete snapshot in \"$last\", reusing and renaming it ..."
551                         RenameSubvolume "$last" "$sys_target"
552                         if [ $? -ne 0 ]; then
553                                 echo "Failed to rename last snapshot \"$last\" to \"$sys_target\"!? \"$system\" skipped!"
554                                 echo; continue
555                         fi
556                 elif [ -n "$last" ]; then
557                         # Old backup directory found, create new snapshot
558                         echo "Found last snapshot in \"$last\"."
559                         if [ "$DRYRUN" -eq 0 ]; then
560                                 CloneSubvolume "$last" "$sys_target" "$snapshot"; r=$?
561                                 if [ $r -ne 0 ]; then
562                                         echo "Can't create snapshot \"$snapshot\" of \"$last\", code $r!? \"$system\" skipped!"
563                                         echo; continue
564                                 fi
565                                 echo "Created new snapshot in \"$snapshot\"."
566                         else
567                                 echo " *** Trial run, not creating new snapshot in \"$snapshot\"!"
568                         fi
569                 else
570                         # No old backup found, create new subvolume
571                         if [ "$DRYRUN" -eq 0 ]; then
572                                 CreateSubvolume "$sys_target"; r=$?
573                                 if [ $r -ne 0 ]; then
574                                         echo "Can't create subvolume \"$sys_target\", code $r!? \"$system\" skipped!"
575                                         echo; continue
576                                 fi
577                                 echo "Created new subvolume in \"$sys_target\"."
578                         else
579                                 echo " *** Trial run, not creating new subvolume \"$sys_target\"!"
580                         fi
581                 fi
582         fi
583
584         ssh_cmd="ssh"
585         [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
586
587         # execute job "pre-exec" command, if any
588         if [ -n "$job_pre_exec" ]; then
589                 ExecJob pre "$job_pre_exec" ; ret=$?
590                 if [ $ret -ne 0 ]; then
591                         [ $ret -ne 99 ] && count_started=$count_started+1
592                         echo "Pre-exec command failed, \"$system\" skipped!"
593                         echo; continue
594                 fi
595         fi
596
597         # prepare (remote) command ...
598         if [[ "$backup_type" == "rsync" ]]; then
599                 cmd="$rsync --archive --timeout=$io_timeout"
600                 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
601                 [ "$local" -eq 0 ] && cmd="$cmd --rsh=\"$ssh_cmd\""
602                 cmd="$cmd --delete-during --delete-excluded --sparse"
603                 if [ "$VERBOSE" -gt 0 ]; then
604                         [ "$rsync_proto" -ge 31 ] \
605                                 && cmd="$cmd --info=progress2" \
606                                 || cmd="$cmd --progress"
607                 fi
608                 set -f
609                 if [ "$source_root" = "$default_source_root" ]; then
610                         for dir in \
611                                 "/dev/**" \
612                                 "/media/**" \
613                                 "/mnt/**" \
614                                 "/net/**" \
615                                 "/proc/**" \
616                                 "/run/**" \
617                                 "/sys/**" \
618                                 "/tmp/**" \
619                                 "/var/cache/apt/**" \
620                                 "/var/log/**" \
621                                 "/var/tmp/**" \
622                         ; do
623                                 cmd="$cmd --exclude=$dir"
624                         done
625                 fi
626                 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
627                 for dir in $exclude_dirs_add; do
628                         cmd="$cmd --exclude=$dir"
629                 done
630                 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
631                 set +f
632
633                 [ "$local" -eq 0 ] \
634                         && cmd="$cmd ${user}@${system}:$source_root $sys_target/" \
635                         || cmd="$cmd $source_root $sys_target/"
636         elif [[ "$backup_type" == "scp" ]]; then
637                 cmd="scp"
638                 [ "$VERBOSE" -eq 0 ] && cmd="$cmd -q"
639                 for file in $files; do
640                         cmd="$cmd ${user}@${system}:$file $sys_target/"
641                 done
642         else
643                 echo "Backup type \"$backup_type\" undefined, \"$system\" skipped!"
644                 echo; continue
645         fi
646
647         echo "Backing up to \"$sys_target\" ..."
648         echo -n "Start date: "; date
649         echo "$cmd"
650         count_started=$count_started+1
651         ok=0
652
653         if [ "$DRYRUN" -eq 0 ]; then
654                 stamp_file="$sys_target/.stamp"
655                 rm -f "$stamp_file"
656
657                 # Execute backup command:
658                 start_t=$(date "+%s")
659                 $SHELL -c "$cmd"; ret=$?
660                 end_t=$(date "+%s")
661
662                 echo "code=$ret" >"$stamp_file"
663                 echo "start_t=$start_t" >>"$stamp_file"
664                 echo "end_t=$end_t" >>"$stamp_file"
665                 echo "cmd='$cmd'" >>"$stamp_file"
666                 echo "backup_host='`hostname -f`'" >>"$stamp_file"
667                 echo "backup_user='`id -un`'" >>"$stamp_file"
668         else
669                 echo " *** Trial run, not executing save command!"
670                 ret=0
671         fi
672
673         if [ $ret -eq 20 ]; then
674                 echo "Backup of \"$system\" interrupted. Aborting ..."
675                 GotSignal
676         fi
677
678         echo -n "End date: "; date
679         if [[ $ret -eq 0 || $ret -eq 24 ]]; then
680                 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
681
682                 echo "System \"$system\" completed with status $ret, OK."
683                 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
684                 ok=1
685         else
686                 echo "System \"$system\" completed with ERRORS, code $ret!"
687         fi
688
689         # execute job "post-exec" command, if any
690         if [ -n "$job_post_exec" ]; then
691                 ExecJob post "$job_post_exec"
692         fi
693
694         if [ $generations -gt 0 ]; then
695                 # Update "latest" symlink
696                 if [ "$DRYRUN" -eq 0 ]; then
697                         rm -f "$sys_root/latest"
698                         ln -s "$sys_target" "$sys_root/latest"
699                 fi
700                 # Clean up old generations
701                 declare -i gen_count=$generations+2
702                 to_delete=$(ls -1t "$sys_root" 2>/dev/null | tail -n+$gen_count | sort)
703                 if [[ -n "$to_delete" && $ok -eq 1 ]]; then
704                         [ "$DRYRUN" -eq 0 ] \
705                                 && echo "Deleting old backup generations (keep $generations) ..." \
706                                 || echo " *** Trial run, not deleting old generations:"
707                         for delete in $to_delete; do
708                                 dir="$sys_root/$delete"
709                                 if [ ! -e "$dir/.stamp" ]; then
710                                         echo "Not deleting \"$dir\", not a backup directory!?"
711                                         continue
712                                 fi
713                                 last=$(stat "$dir/.stamp" 2>/dev/null | grep "^Modify: " \
714                                  | cut -d':' -f2- | cut -d. -f1)
715                                 echo "Removing backup from" $last "..."
716                                 if [ "$DRYRUN" -eq 0 ]; then
717                                         DeleteSubvolume "$dir"
718                                         [ $? -eq 0 ] || \
719                                           echo "Failed to delete \"$dir\"!"
720                                 fi
721                         done
722                         echo -n "Clean up finished: "; date
723                 elif [ -n "$to_delete" ]; then
724                         echo "There have been errors, not cleaning up old generations!"
725                 else
726                         echo "Nothing to clean up (keep up to $generations generations)."
727                 fi
728         fi
729
730         destinations="$destinations $target"
731         echo
732 done
733
734 sync
735
736 if [ "$DRYRUN" -eq 0 ]; then
737         paths=""
738         paths_zfs=""
739         for dest in $(echo $destinations | sed -e 's/ /\n/g' | sort | uniq); do
740                 fs=$(GetFS "$dest")
741                 case $fs in
742                   "zfs" )
743                         paths_zfs="$paths_zfs $dest"
744                         ;;
745                   *)
746                         paths="$paths $dest"
747                 esac
748         done
749         if [ -n "$paths" ]; then
750                 df -h $paths
751                 echo
752         fi
753         if [ -n "$paths_zfs" ]; then
754                 zfs list $paths_zfs
755                 echo
756         fi
757 fi
758
759 CleanUp
760
761 echo -n "Done: "; date
762 echo
763 [ $count_all -eq 1 ] && s="" || s="s"
764 [ $count_enabled -eq $count_all ] \
765         && echo " - $count_all job$s defined (all enabled)," \
766         || echo " - $count_all job$s defined ($count_enabled enabled),"
767 [ $count_started -eq 1 ] && s="" || s="s"
768 echo " - $count_started job$s started,"
769 echo " - $count_ok done without errors."
770 echo
771
772 if [ $count_started -ne $count_ok ]; then
773         echo "----->  THERE HAVE BEEN ERRORS!  <-----"
774         echo
775         exit 6
776 elif [ $count_enabled -ne $count_started ]; then
777         exit 7
778 fi
779
780 # -eof-