3 # backup-script system for cloning systems using rsync
4 # Copyright (c)2008-2015 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.
14 PIDFILE="/var/run/$NAME.pid"
21 declare -i count_all=0
22 declare -i count_started=0
24 declare -i count_ok_vanished=0
28 # Default settings, can be overwritten in backup-script.conf:
29 [ -d "/usr/local/etc/backup-script.d" ] \
30 && conf_d="/usr/local/etc/backup-script.d" \
31 || conf_d="/etc/backup-script.d"
34 default_source_root="/"
35 default_target="/var/backups"
37 default_ssh_args_add=""
38 default_rsync_args_add=""
39 default_exclude_args_add=""
44 default_job_pre_exec=""
45 default_job_post_exec=""
48 echo "Usage: $NAME [<options>] [<system> [<system> [...]]]"
50 echo " -p, --progress Show progress, see rsync(1)."
51 echo " -n, --dry-run Test run only, don't copy any data."
53 echo "When no <system> is given, all defined systems are used."
61 if [ -n "$post_exec" ]; then
62 echo "Executing \"$post_exec\" ..."
65 echo "Warning: post-exec command failed!"
74 echo "--> Got break signal, cleaning up & aborting ..."
77 echo -n "Aborted: "; date
87 echo "Running job ${what}-exec command ..."
89 && cmd="$ssh_cmd ${user}@${system} $cmd"
90 echo -n "Start date (${what}-exec): "; date
92 if [ "$DRYRUN" -eq 0 ]; then
93 $SHELL -c "$cmd"; local ret=$?
95 echo " *** Trial run, not executing ${what}-exec command!"
99 && echo "The ${what}-exec command completed with status 0, OK." \
100 || echo "The ${what}-exec command completed with ERRORS, code $ret!"
107 while [ -n "$dir" ]; do
108 findmnt -fn -o FSTYPE --raw "$dir" 2>/dev/null; local r=$?
109 if [ $r -eq 0 ]; then
111 elif [ $r -eq 127 ]; then
115 dir=$(dirname "$dir") || return 1
124 dir=$(dirname "$volume")
128 btrfs subvolume create "$volume" >/dev/null || return 1
131 zfs create "$(echo "$volume" | cut -c2-)" || return 1
134 echo "CreateSubvolume: Incompatible FS type \"$fs\" on \"$dir\"!"
148 dir=$(dirname "source")
149 fs=$(GetFS "$source")
152 btrfs subvolume snapshot "$source" "$snapshot" >/dev/null || return 1
155 zfs snapshot "$snapshot" || return 1
156 link_name="$(echo "$snapshot" | cut -d@ -f2-)"
158 "$volume/.zfs/snapshot/$link_name" \
159 "$(dirname "$volume")/$link_name"
162 echo "CloneSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
173 fs=$(GetFS "$source")
176 mv "$source" "$target" || return 1
180 "$(echo "$source" | cut -c2-)" \
181 "$(echo "$target" | cut -c2-)" \
185 echo "RenameSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
197 fs=$(GetFS "$volume")
200 btrfs subvolume delete "$volume" >/dev/null || return 1
203 id="$(basename "$volume")"
204 if [ -h "$volume" ]; then
205 snapshot="$(dirname "$volume")/current@$id"
209 zfs destroy -r "$(echo "$snapshot" | cut -c2-)" >/dev/null || return 1
210 [ -h "$volume" ] && rm "$volume"
213 echo "DeleteSubvolume: Incompatible FS type \"$fs\" on \"$volume\"!"
219 Initialize_Last_SysTarget_Snapshot() {
224 fs=$(GetFS "$sys_target")
227 # Search directory of last generation, if any
228 last=$(ls -1d "$sys_target"/[0-9]* 2>/dev/null | sort -r | head -n1)
229 if [ -n "$last" ]; then
230 if [ ! -d "$last" ]; then
231 echo "Last snapshot \"$last\" seems not to be a directory!? \"$system\" skipped!"
236 sys_target="$sys_target/$(date +%Y%m%d-%H%M%S)"
237 snapshot="$sys_target"
240 # On ZFS, the last generation is always named "current"
241 if [ -e "$sys_target/current" ]; then
242 last="$sys_target/current"
243 if [ "$(uname)" = "Linux" ]; then
244 date=$(LC_ALL=C stat "$1" | grep "^Modify: " \
245 | cut -d':' -f2- | cut -d. -f1)
247 date=$(LC_ALL=C stat -f "%Sc" "$1")
249 date=$(echo "$date" | sed -e's/^ //g' -e 's/[-:]//g' -e 's/ /-/g')
253 date="$(date +%Y%m%d-%H%M%S)"
255 snapshot="$(echo "$sys_target/current" | cut -c2-)@$date"
256 sys_target="$sys_target/current"
259 echo "Initialize_Last_SysTarget_Snapshot: Incompatible FS type \"$fs\" on \"$sys_target\"!"
265 # Search configuration file (last one is used as default!)
267 "/usr/local/etc/backup-script.conf" \
268 "/etc/backup-script.conf" \
269 "${conf_d}/backup-script.conf" \
270 "/usr/local/etc/backup-script.conf" \
272 [ -r "$conf" ] && break
275 # Read in configuration file
276 config_info="Configuration file is \"$conf\""
277 if [ -r "$conf" ]; then
280 config_info="${config_info} (not readable, using defaults)"
282 config_info="${config_info},\nusing \"$conf_d\" as configuration directory."
284 while [ $# -gt 0 ]; do
300 trap GotSignal SIGINT
302 echo -n "Started: "; date
306 if [ $# -ge 1 ]; then
308 if [ ! -r "${conf_d}/$s" ]; then
309 echo "$NAME: Can' read \"${conf_d}/$s\"!"
312 sys="$sys ${conf_d}/$s"
318 # check and create PID file
319 if [ -e "$PIDFILE" ]; then
320 echo "Lockfile \"$PIDFILE\" already exists."
321 echo "Is an other instance still running?"
323 echo -n "Aborted: "; date
327 touch "$PIDFILE" 2>/dev/null
328 if [ $? -ne 0 ]; then
329 echo "Warning: can't create PID file \"$PIDFILE\"!"
332 echo "$$" >>"$PIDFILE"
335 if [ -n "$pre_exec" ]; then
336 echo "Executing \"$pre_exec\" ..."
338 if [ $? -ne 0 ]; then
339 echo "Error: pre-exec command failed!"; echo
341 echo "Aborting backup."; echo
349 [ -r "$f" -a -f "$f" ] || continue
351 fname=$(basename "$f")
353 "backup-script.conf"|*.sh)
358 # Set global defaults
361 source_root="$default_source_root"
362 target="$default_target"
363 ssh_args_add="$default_ssh_args_add"
364 rsync_args_add="$default_rsync_args_add"
365 exclude_args_add="$default_exclude_args_add"
366 compress="$default_compress"
368 local="$default_local"
369 generations="$default_generations"
370 job_pre_exec="$default_job_pre_exec"
371 job_post_exec="$default_job_post_exec"
373 # Compatibility with backup-pull(1) script: Save global values ...
374 pre_exec_saved="$pre_exec"
375 post_exec_saved="$post_exec"
377 # Compatibility with backup-pull(1) script: Set defaults
383 # Read in system configuration file
386 # Compatibility with backup-pull(1) script: Fix up configuration
387 [ "$system" = "$fname" -a -n "$host" ] \
389 [ "$source_root" = "$default_source_root" -a -n "$source" ] \
390 && source_root="$source"
391 [ -z "$job_pre_exec" -a -n "$pre_exec" ] \
392 && job_pre_exec="$pre_exec"
393 [ -z "$job_post_exec" -a -n "$post_exec" ] \
394 && job_post_exec="$post_exec"
396 # Compatibility with backup-pull(1) script: Restore global values ...
397 pre_exec="$pre_exec_saved"
398 post_exec="$post_exec_saved"
400 # Validate configuration
401 if [ "$system" = "localhost" -o "$system" = "127.0.0.1" ]; then
407 # Make sure "source" ends with a slash ("/")
415 # Make sure "target" DOESN'T end with a slash ("/")
418 target=$( echo "$target" | sed -e 's/\/$//g' )
422 [ "$system" = "$fname" ] \
423 && systxt="\"$system\"" \
424 || systxt="\"$fname\" [\"$system\"]"
426 && echo "Working on $systxt ..." \
427 || echo "Working on $systxt (local system) ..."
429 count_all=$count_all+1
431 # Check target directory
432 if [ -z "$target" ]; then
433 echo "No target directory specified for \"$system\"!? Skipped!"
436 if [ ! -d "$target" ]; then
437 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
441 sys_target="$target/$fname"
442 sys_root="$sys_target"
443 if [ "$DRYRUN" -eq 0 -a ! -e "$sys_target" ]; then
444 if [ $generations -gt 0 ]; then
445 CreateSubvolume "$sys_target"
447 mkdir -p "$sys_target"
449 if [ $? -ne 0 ]; then
450 echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
455 if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
456 # Check if system is alive
457 ping -c 1 "$system" >/dev/null 2>&1
458 if [ $? -ne 0 ]; then
459 echo "Host \"$system\" seems not to be alive!? Skipped."
462 echo "OK, host \"$system\" seems to be alive."
465 if [ $generations -gt 0 ]; then
466 # Make sure no old backup is stored in system directory
467 if [ -e "$sys_target/.stamp" ]; then
468 # There seems to be a genearation-less backup in the
470 echo "Target directory \"$sys_target\" seems to be unclean!? \"$system\" skipped!"
474 Initialize_Last_SysTarget_Snapshot "$sys_target" || continue
476 if [ -n "$last" -a ! -e "$last/.stamp" ]; then
477 # Old backup directory without "stamp file", continue
478 echo "Found incomplete snapshot in \"$last\", reusing and renaming it ..."
479 RenameSubvolume "$last" "$sys_target"
480 if [ $? -ne 0 ]; then
481 echo "Failed to rename last snapshot \"$last\" to \"$sys_target\"!? \"$system\" skipped!"
484 elif [ -n "$last" ]; then
485 # Old backup directory found, create new snapshot
486 echo "Found last snapshot in \"$last\"."
487 if [ "$DRYRUN" -eq 0 ]; then
488 CloneSubvolume "$last" "$sys_target" "$snapshot"; r=$?
489 if [ $r -ne 0 ]; then
490 echo "Can't create snapshot \"$snapshot\" of \"$last\", code $r!? \"$system\" skipped!"
493 echo "Created new snapshot in \"$snapshot\"."
495 echo " *** Trial run, not creating new snapshot in \"$snapshot\"!"
498 # No old backup found, create new subvolume
499 if [ "$DRYRUN" -eq 0 ]; then
500 CreateSubvolume "$sys_target"; r=$?
501 if [ $r -ne 0 ]; then
502 echo "Can't create subvolume \"$sys_target\", code $r!? \"$system\" skipped!"
505 echo "Created new subvolume in \"$sys_target\"."
507 echo " *** Trial run, not creating new subvolume \"$sys_target\"!"
513 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
515 # execute job "pre-exec" command, if any
516 if [ -n "$job_pre_exec" ]; then
517 ExecJob pre "$job_pre_exec" ; ret=$?
518 if [ $ret -ne 0 ]; then
519 [ $ret -ne 99 ] && count_started=$count_started+1
520 echo "Pre-exec command failed, \"$system\" skipped!"
525 # prepare (remote) command ...
526 cmd="rsync --archive"
527 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
528 [ "$local" -eq 0 ] && cmd="$cmd --rsh=\"$ssh_cmd\""
529 cmd="$cmd --delete --delete-excluded --sparse"
530 [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
531 if [ "$source_root" = "$default_source_root" ]; then
532 cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
533 cmd="$cmd --exclude=/run --exclude=/tmp --exclude=/var/tmp"
534 cmd="$cmd --exclude=/media --exclude=/mnt --exclude=/net"
535 cmd="$cmd --exclude=/var/cache/apt --exclude=/var/log"
537 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
538 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
541 && cmd="$cmd ${user}@${system}:$source_root $sys_target/" \
542 || cmd="$cmd $source_root $sys_target/"
544 echo "Backing up to \"$sys_target\" ..."
545 echo -n "Start date: "; date
547 count_started=$count_started+1
550 if [ "$DRYRUN" -eq 0 ]; then
551 rm -f "$sys_target/.stamp"
552 $SHELL -c "$cmd"; ret=$?
553 echo "code=$ret" >"$sys_target/.stamp"
555 echo " *** Trial run, not executing synchronization command!"
559 if [ $ret -eq 20 ]; then
560 echo "Backup of \"$system\" interrupted. Aborting ..."
564 echo -n "End date: "; date
565 if [ $ret -eq 0 -o $ret -eq 24 ]; then
566 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
568 echo "System \"$system\" completed with status $ret, OK."
569 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
572 echo "System \"$system\" completed with ERRORS, code $ret!"
575 # execute job "post-exec" command, if any
576 if [ -n "$job_post_exec" ]; then
577 ExecJob post "$job_post_exec"
580 if [ $generations -gt 0 ]; then
581 # Update "latest" symlink
582 if [ "$DRYRUN" -eq 0 ]; then
583 rm -f "$sys_root/latest"
584 ln -s "$sys_target" "$sys_root/latest"
586 # Clean up old generations
587 declare -i gen_count=$generations+2
588 to_delete=$(ls -1t "$sys_root" 2>/dev/null | tail -n+$gen_count | sort)
589 if [ -n "$to_delete" -a $ok -eq 1 ]; then
590 [ "$DRYRUN" -eq 0 ] \
591 && echo "Deleting old backup generations (keep $generations) ..." \
592 || echo " *** Trial run, not deleting old generations:"
593 for delete in $to_delete; do
594 dir="$sys_root/$delete"
595 if [ ! -e "$dir/.stamp" ]; then
596 echo "Not deleting \"$dir\", not a backup directory!?"
599 last=$(stat "$dir/.stamp" 2>/dev/null | grep "^Modify: " \
600 | cut -d':' -f2- | cut -d. -f1)
601 echo "Removing backup from" $last "..."
602 if [ "$DRYRUN" -eq 0 ]; then
603 DeleteSubvolume "$dir"
605 echo "Failed to delete \"$dir\"!"
608 echo -n "Clean up finished: "; date
609 elif [ -n "$to_delete" ]; then
610 echo "There have been errors, not cleaning up old generations!"
612 echo "Nothing to clean up."
616 destinations="$destinations $target"
622 if [ "$DRYRUN" -eq 0 ]; then
625 for dest in $(echo $destinations | sed -e 's/ /\n/g' | sort | uniq); do
629 paths_zfs="$paths_zfs $dest"
635 if [ -n "$paths" ]; then
639 if [ -n "$paths_zfs" ]; then
647 echo -n "Done: "; date
649 [ $count_all -eq 1 ] && s="" || s="s"
650 echo " - $count_all job$s defined,"
651 [ $count_started -eq 1 ] && s="" || s="s"
652 echo " - $count_started job$s started,"
653 echo " - $count_ok done without errors."
656 if [ $count_started -ne $count_ok ]; then
657 echo "-----> THERE HAVE BEEN ERRORS! <-----"
660 elif [ $count_all -ne $count_started ]; then