3 # backup-script system for cloning systems using rsync
4 # Copyright (c)2008-2016 Alexander Barton <alex@barton.de>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 # Please read the file COPYING, README and AUTHORS for more information.
14 PIDFILE="/var/run/$NAME.pid"
22 declare -i count_all=0
23 declare -i count_started=0
25 declare -i count_ok_vanished=0
29 # Default settings, can be overwritten in backup-script.conf:
30 [ -d "/usr/local/etc/backup-script.d" ] \
31 && conf_d="/usr/local/etc/backup-script.d" \
32 || conf_d="/etc/backup-script.d"
35 default_backup_type="rsync"
36 default_source_root="/"
37 default_files="running-config"
38 default_target="/var/backups"
40 default_ssh_args_add=""
41 default_rsync_args_add=""
42 default_exclude_args_add=""
43 default_exclude_dirs_add=""
48 default_job_pre_exec=""
49 default_job_post_exec=""
53 echo "Usage: $NAME [<options>] [<system> [<system> [...]]]"
55 echo " -n, --dry-run Test run only, don't copy any data."
56 echo " -p, --progress Show progress, see rsync(1)."
57 echo " -t TAG, --tag TAG Only run jobs with tag TAG."
59 echo "When no <system> is given, all defined systems are used."
67 if [ -n "$post_exec" ]; then
68 echo "Executing \"$post_exec\" ..."
71 echo "Warning: post-exec command failed!"
80 echo "--> Got break signal, cleaning up & aborting ..."
83 echo -n "Aborted: "; date
93 echo "Running job ${what}-exec command ..."
95 && cmd="$ssh_cmd ${user}@${system} $cmd"
96 echo -n "Start date (${what}-exec): "; date
98 if [ "$DRYRUN" -eq 0 ]; then
99 $SHELL -c "$cmd"; local ret=$?
101 echo " *** Trial run, not executing ${what}-exec command!"
105 && echo "The ${what}-exec command completed with status 0, OK." \
106 || echo "The ${what}-exec command completed with ERRORS, code $ret!"
113 while [ -n "$dir" ]; do
114 findmnt -fn -o FSTYPE --raw "$dir" 2>/dev/null; local r=$?
115 if [ $r -eq 0 ]; then
117 elif [ $r -eq 127 ]; then
121 dir=$(dirname "$dir") || return 1
130 dir=$(dirname "$volume")
134 btrfs subvolume create "$volume" >/dev/null || return 1
137 zfs create "$(echo "$volume" | cut -c2-)" || return 1
140 echo "CreateSubvolume: Incompatible FS type \"$fs\" on \"$dir\"!"
154 dir=$(dirname "source")
155 fs=$(GetFS "$source")
158 btrfs subvolume snapshot "$source" "$snapshot" >/dev/null || return 1
161 zfs snapshot "$snapshot" || return 1
162 link_name="$(echo "$snapshot" | cut -d@ -f2-)"
164 "current/.zfs/snapshot/$link_name" \
165 "$(dirname "$volume")/$link_name"
168 echo "CloneSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
179 fs=$(GetFS "$source")
182 mv "$source" "$target" || return 1
186 "$(echo "$source" | cut -c2-)" \
187 "$(echo "$target" | cut -c2-)" \
191 echo "RenameSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
203 fs=$(GetFS "$volume")
206 btrfs subvolume delete "$volume" >/dev/null || return 1
209 id="$(basename "$volume")"
210 if [ -h "$volume" ]; then
211 snapshot="$(dirname "$volume")/current@$id"
215 zfs destroy -r "$(echo "$snapshot" | cut -c2-)" >/dev/null || return 1
216 [ -h "$volume" ] && rm "$volume"
219 echo "DeleteSubvolume: Incompatible FS type \"$fs\" on \"$volume\"!"
225 Initialize_Last_SysTarget_Snapshot() {
230 fs=$(GetFS "$sys_target")
233 # Search directory of last generation, if any
234 last=$(ls -1d "$sys_target"/[0-9]* 2>/dev/null | sort -r | head -n1)
235 if [ -n "$last" ]; then
236 if [ ! -d "$last" ]; then
237 echo "Last snapshot \"$last\" seems not to be a directory!? \"$system\" skipped!"
242 sys_target="$sys_target/$(date +%Y%m%d-%H%M%S)"
243 snapshot="$sys_target"
246 # On ZFS, the last generation is always named "current"
247 if [ -e "$sys_target/current" ]; then
248 last="$sys_target/current"
249 if [ "$(uname)" = "Linux" ]; then
250 date=$(LC_ALL=C stat "$1" | grep "^Modify: " \
251 | cut -d':' -f2- | cut -d. -f1)
253 date=$(LC_ALL=C stat -f "%Sc" "$1")
255 date=$(echo "$date" | sed -e's/^ //g' -e 's/[-:]//g' -e 's/ /-/g')
259 date="$(date +%Y%m%d-%H%M%S)"
261 snapshot="$(echo "$sys_target/current" | cut -c2-)@$date"
262 sys_target="$sys_target/current"
265 echo "Initialize_Last_SysTarget_Snapshot: Incompatible FS type \"$fs\" on \"$sys_target\"!"
271 # Search configuration file (last one is used as default!)
273 "/usr/local/etc/backup-script.conf" \
274 "/etc/backup-script.conf" \
275 "${conf_d}/backup-script.conf" \
276 "/usr/local/etc/backup-script.conf" \
278 [ -r "$conf" ] && break
281 # Read in configuration file
282 config_info="Configuration file is \"$conf\""
283 if [ -r "$conf" ]; then
286 config_info="${config_info} (not readable, using defaults)"
288 config_info="${config_info},\nusing \"$conf_d\" as configuration directory."
290 while [ $# -gt 0 ]; do
299 shift; TAG="$1"; shift
300 [ -n "$TAG" ] || Usage
310 echo -n "Started: "; date
311 echo -e "$config_info"
313 # Check rsync and its protocol version
314 rsync=$(which "rsync" 2>/dev/null)
315 if [ $? -ne 0 ]; then
316 echo "Failed to detect rsync(1)! Is it installed in your \$PATH?"
319 rsync_proto=$($rsync --version 2>/dev/null | head -n 1 | sed 's/.* protocol version \([0-9]*\)$/\1/')
320 if [ $? -ne 0 ]; then
321 echo "Failed to detect protocol version of $rsync!"
324 echo "Rsync command is $rsync, protocol version $rsync_proto."
326 [[ -n "$TAG" ]] && echo "Running jobs tagged with \"$TAG\"."
329 trap GotSignal SIGINT
331 if [ $# -ge 1 ]; then
333 if [ ! -r "${conf_d}/$s" ]; then
334 echo "$NAME: Can' read \"${conf_d}/$s\"!"
337 sys="$sys ${conf_d}/$s"
343 # check and create PID file
344 if [ -e "$PIDFILE" ]; then
345 echo "Lockfile \"$PIDFILE\" already exists."
346 echo "Is an other instance still running?"
348 echo -n "Aborted: "; date
352 touch "$PIDFILE" 2>/dev/null
353 if [ $? -ne 0 ]; then
354 echo "Warning: can't create PID file \"$PIDFILE\"!"
357 echo "$$" >>"$PIDFILE"
360 if [ -n "$pre_exec" ]; then
361 echo "Executing \"$pre_exec\" ..."
363 if [ $? -ne 0 ]; then
364 echo "Error: pre-exec command failed!"; echo
366 echo "Aborting backup."; echo
374 [[ -r "$f" && -f "$f" ]] || continue
376 fname=$(basename "$f")
378 "backup-script.conf"|*.sh)
383 # Set global defaults
385 backup_type="$default_backup_type"
387 source_root="$default_source_root"
388 files="$default_files"
389 target="$default_target"
390 ssh_args_add="$default_ssh_args_add"
391 rsync_args_add="$default_rsync_args_add"
392 exclude_args_add="$default_exclude_args_add"
393 exclude_dirs_add="$default_exclude_dirs_add"
394 compress="$default_compress"
396 local="$default_local"
397 generations="$default_generations"
398 job_pre_exec="$default_job_pre_exec"
399 job_post_exec="$default_job_post_exec"
402 # Compatibility with backup-pull(1) script: Save global values ...
403 pre_exec_saved="$pre_exec"
404 post_exec_saved="$post_exec"
406 # Compatibility with backup-pull(1) script: Set defaults
412 # Read in system configuration file
415 # Compatibility with backup-pull(1) script: Fix up configuration
416 [[ "$system" = "$fname" && -n "$host" ]] \
418 [[ "$source_root" = "$default_source_root" && -n "$source" ]] \
419 && source_root="$source"
420 [[ -z "$job_pre_exec" && -n "$pre_exec" ]] \
421 && job_pre_exec="$pre_exec"
422 [[ -z "$job_post_exec" && -n "$post_exec" ]] \
423 && job_post_exec="$post_exec"
425 # Compatibility with backup-pull(1) script: Restore global values ...
426 pre_exec="$pre_exec_saved"
427 post_exec="$post_exec_saved"
429 # Validate configuration
430 if [[ "$system" = "localhost" || "$system" = "127.0.0.1" ]]; then
436 # Add "NONE" tag when no tags are given in the config file:
437 [[ -z "$tags" ]] && tags="NONE"
439 [[ "$local" -eq 1 ]] && tags="$tags,LOCAL"
441 if [[ -n "$TAG" && "$TAG" != "ALL" ]]; then
442 echo "$tags" | grep -E "(^|,)$TAG(,|$)" >/dev/null 2>&1
443 if [ $? -ne 0 ]; then
444 if [ "$DRYRUN" -ne 0 ]; then
445 echo "Tags of system \"$system\" don't match \"$TAG\": \"$tags\". Skipped."
452 # Make sure "source" ends with a slash ("/")
460 # Make sure "target" DOESN'T end with a slash ("/")
463 target=$( echo "$target" | sed -e 's/\/$//g' )
467 [ "$system" = "$fname" ] \
468 && systxt="\"$system\"" \
469 || systxt="\"$fname\" [\"$system\"]"
471 && echo "Working on $systxt ..." \
472 || echo "Working on $systxt (local system) ..."
474 count_all=$count_all+1
476 # Check target directory
477 if [ -z "$target" ]; then
478 echo "No target directory specified for \"$system\"!? Skipped!"
481 if [ ! -d "$target" ]; then
482 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
486 sys_target="$target/$fname"
487 sys_root="$sys_target"
488 if [[ "$DRYRUN" -eq 0 && ! -e "$sys_target" ]]; then
489 if [ $generations -gt 0 ]; then
490 CreateSubvolume "$sys_target"
492 mkdir -p "$sys_target"
494 if [ $? -ne 0 ]; then
495 echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
500 if [[ "$local" -eq 0 && "$ping" -ne 0 ]]; then
501 # Check if system is alive
502 ping -c 1 "$system" >/dev/null 2>&1
503 if [ $? -ne 0 ]; then
504 echo "Host \"$system\" seems not to be alive!? Skipped."
507 echo "OK, host \"$system\" seems to be alive."
510 if [ $generations -gt 0 ]; then
511 # Make sure no old backup is stored in system directory
512 if [ -e "$sys_target/.stamp" ]; then
513 # There seems to be a genearation-less backup in the
515 echo "Target directory \"$sys_target\" seems to be unclean!? \"$system\" skipped!"
519 Initialize_Last_SysTarget_Snapshot "$sys_target" || continue
521 if [[ -n "$last" && ! -e "$last/.stamp" ]]; then
522 # Old backup directory without "stamp file", continue
523 echo "Found incomplete snapshot in \"$last\", reusing and renaming it ..."
524 RenameSubvolume "$last" "$sys_target"
525 if [ $? -ne 0 ]; then
526 echo "Failed to rename last snapshot \"$last\" to \"$sys_target\"!? \"$system\" skipped!"
529 elif [ -n "$last" ]; then
530 # Old backup directory found, create new snapshot
531 echo "Found last snapshot in \"$last\"."
532 if [ "$DRYRUN" -eq 0 ]; then
533 CloneSubvolume "$last" "$sys_target" "$snapshot"; r=$?
534 if [ $r -ne 0 ]; then
535 echo "Can't create snapshot \"$snapshot\" of \"$last\", code $r!? \"$system\" skipped!"
538 echo "Created new snapshot in \"$snapshot\"."
540 echo " *** Trial run, not creating new snapshot in \"$snapshot\"!"
543 # No old backup found, create new subvolume
544 if [ "$DRYRUN" -eq 0 ]; then
545 CreateSubvolume "$sys_target"; r=$?
546 if [ $r -ne 0 ]; then
547 echo "Can't create subvolume \"$sys_target\", code $r!? \"$system\" skipped!"
550 echo "Created new subvolume in \"$sys_target\"."
552 echo " *** Trial run, not creating new subvolume \"$sys_target\"!"
558 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
560 # execute job "pre-exec" command, if any
561 if [ -n "$job_pre_exec" ]; then
562 ExecJob pre "$job_pre_exec" ; ret=$?
563 if [ $ret -ne 0 ]; then
564 [ $ret -ne 99 ] && count_started=$count_started+1
565 echo "Pre-exec command failed, \"$system\" skipped!"
570 # prepare (remote) command ...
571 if [[ "$backup_type" == "rsync" ]]; then
572 cmd="$rsync --archive"
573 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
574 [ "$local" -eq 0 ] && cmd="$cmd --rsh=\"$ssh_cmd\""
575 cmd="$cmd --delete --delete-excluded --sparse"
576 if [ "$VERBOSE" -gt 0 ]; then
577 [ "$rsync_proto" -ge 31 ] \
578 && cmd="$cmd --info=progress2" \
579 || cmd="$cmd --progress"
582 if [ "$source_root" = "$default_source_root" ]; then
592 "/var/cache/apt/**" \
596 cmd="$cmd --exclude=$dir"
599 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
600 for dir in $exclude_dirs_add; do
601 cmd="$cmd --exclude=$dir"
603 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
607 && cmd="$cmd ${user}@${system}:$source_root $sys_target/" \
608 || cmd="$cmd $source_root $sys_target/"
609 elif [[ "$backup_type" == "scp" ]]; then
611 [ "$VERBOSE" -eq 0 ] && cmd="$cmd -q"
612 for file in $files; do
613 cmd="$cmd ${user}@${system}:$file $sys_target/"
616 echo "Backup type \"$backup_type\" undefined, \"$system\" skipped!"
620 echo "Backing up to \"$sys_target\" ..."
621 echo -n "Start date: "; date
623 count_started=$count_started+1
626 if [ "$DRYRUN" -eq 0 ]; then
627 stamp_file="$sys_target/.stamp"
630 # Execute backup command:
631 start_t=$(date "+%s")
632 $SHELL -c "$cmd"; ret=$?
635 echo "code=$ret" >"$stamp_file"
636 echo "start_t=$start_t" >>"$stamp_file"
637 echo "end_t=$end_t" >>"$stamp_file"
638 echo "cmd='$cmd'" >>"$stamp_file"
639 echo "backup_host='`hostname -f`'" >>"$stamp_file"
640 echo "backup_user='`id -un`'" >>"$stamp_file"
642 echo " *** Trial run, not executing save command!"
646 if [ $ret -eq 20 ]; then
647 echo "Backup of \"$system\" interrupted. Aborting ..."
651 echo -n "End date: "; date
652 if [[ $ret -eq 0 || $ret -eq 24 ]]; then
653 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
655 echo "System \"$system\" completed with status $ret, OK."
656 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
659 echo "System \"$system\" completed with ERRORS, code $ret!"
662 # execute job "post-exec" command, if any
663 if [ -n "$job_post_exec" ]; then
664 ExecJob post "$job_post_exec"
667 if [ $generations -gt 0 ]; then
668 # Update "latest" symlink
669 if [ "$DRYRUN" -eq 0 ]; then
670 rm -f "$sys_root/latest"
671 ln -s "$sys_target" "$sys_root/latest"
673 # Clean up old generations
674 declare -i gen_count=$generations+2
675 to_delete=$(ls -1t "$sys_root" 2>/dev/null | tail -n+$gen_count | sort)
676 if [[ -n "$to_delete" && $ok -eq 1 ]]; then
677 [ "$DRYRUN" -eq 0 ] \
678 && echo "Deleting old backup generations (keep $generations) ..." \
679 || echo " *** Trial run, not deleting old generations:"
680 for delete in $to_delete; do
681 dir="$sys_root/$delete"
682 if [ ! -e "$dir/.stamp" ]; then
683 echo "Not deleting \"$dir\", not a backup directory!?"
686 last=$(stat "$dir/.stamp" 2>/dev/null | grep "^Modify: " \
687 | cut -d':' -f2- | cut -d. -f1)
688 echo "Removing backup from" $last "..."
689 if [ "$DRYRUN" -eq 0 ]; then
690 DeleteSubvolume "$dir"
692 echo "Failed to delete \"$dir\"!"
695 echo -n "Clean up finished: "; date
696 elif [ -n "$to_delete" ]; then
697 echo "There have been errors, not cleaning up old generations!"
699 echo "Nothing to clean up (keep up to $generations generations)."
703 destinations="$destinations $target"
709 if [ "$DRYRUN" -eq 0 ]; then
712 for dest in $(echo $destinations | sed -e 's/ /\n/g' | sort | uniq); do
716 paths_zfs="$paths_zfs $dest"
722 if [ -n "$paths" ]; then
726 if [ -n "$paths_zfs" ]; then
734 echo -n "Done: "; date
736 [ $count_all -eq 1 ] && s="" || s="s"
737 echo " - $count_all job$s defined,"
738 [ $count_started -eq 1 ] && s="" || s="s"
739 echo " - $count_started job$s started,"
740 echo " - $count_ok done without errors."
743 if [ $count_started -ne $count_ok ]; then
744 echo "-----> THERE HAVE BEEN ERRORS! <-----"
747 elif [ $count_all -ne $count_started ]; then