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_backup_type="rsync"
35 default_source_root="/"
36 default_files="running-config"
37 default_target="/var/backups"
39 default_ssh_args_add=""
40 default_rsync_args_add=""
41 default_exclude_args_add=""
42 default_exclude_dirs_add=""
47 default_job_pre_exec=""
48 default_job_post_exec=""
51 echo "Usage: $NAME [<options>] [<system> [<system> [...]]]"
53 echo " -p, --progress Show progress, see rsync(1)."
54 echo " -n, --dry-run Test run only, don't copy any data."
56 echo "When no <system> is given, all defined systems are used."
64 if [ -n "$post_exec" ]; then
65 echo "Executing \"$post_exec\" ..."
68 echo "Warning: post-exec command failed!"
77 echo "--> Got break signal, cleaning up & aborting ..."
80 echo -n "Aborted: "; date
90 echo "Running job ${what}-exec command ..."
92 && cmd="$ssh_cmd ${user}@${system} $cmd"
93 echo -n "Start date (${what}-exec): "; date
95 if [ "$DRYRUN" -eq 0 ]; then
96 $SHELL -c "$cmd"; local ret=$?
98 echo " *** Trial run, not executing ${what}-exec command!"
102 && echo "The ${what}-exec command completed with status 0, OK." \
103 || echo "The ${what}-exec command completed with ERRORS, code $ret!"
110 while [ -n "$dir" ]; do
111 findmnt -fn -o FSTYPE --raw "$dir" 2>/dev/null; local r=$?
112 if [ $r -eq 0 ]; then
114 elif [ $r -eq 127 ]; then
118 dir=$(dirname "$dir") || return 1
127 dir=$(dirname "$volume")
131 btrfs subvolume create "$volume" >/dev/null || return 1
134 zfs create "$(echo "$volume" | cut -c2-)" || return 1
137 echo "CreateSubvolume: Incompatible FS type \"$fs\" on \"$dir\"!"
151 dir=$(dirname "source")
152 fs=$(GetFS "$source")
155 btrfs subvolume snapshot "$source" "$snapshot" >/dev/null || return 1
158 zfs snapshot "$snapshot" || return 1
159 link_name="$(echo "$snapshot" | cut -d@ -f2-)"
161 "current/.zfs/snapshot/$link_name" \
162 "$(dirname "$volume")/$link_name"
165 echo "CloneSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
176 fs=$(GetFS "$source")
179 mv "$source" "$target" || return 1
183 "$(echo "$source" | cut -c2-)" \
184 "$(echo "$target" | cut -c2-)" \
188 echo "RenameSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
200 fs=$(GetFS "$volume")
203 btrfs subvolume delete "$volume" >/dev/null || return 1
206 id="$(basename "$volume")"
207 if [ -h "$volume" ]; then
208 snapshot="$(dirname "$volume")/current@$id"
212 zfs destroy -r "$(echo "$snapshot" | cut -c2-)" >/dev/null || return 1
213 [ -h "$volume" ] && rm "$volume"
216 echo "DeleteSubvolume: Incompatible FS type \"$fs\" on \"$volume\"!"
222 Initialize_Last_SysTarget_Snapshot() {
227 fs=$(GetFS "$sys_target")
230 # Search directory of last generation, if any
231 last=$(ls -1d "$sys_target"/[0-9]* 2>/dev/null | sort -r | head -n1)
232 if [ -n "$last" ]; then
233 if [ ! -d "$last" ]; then
234 echo "Last snapshot \"$last\" seems not to be a directory!? \"$system\" skipped!"
239 sys_target="$sys_target/$(date +%Y%m%d-%H%M%S)"
240 snapshot="$sys_target"
243 # On ZFS, the last generation is always named "current"
244 if [ -e "$sys_target/current" ]; then
245 last="$sys_target/current"
246 if [ "$(uname)" = "Linux" ]; then
247 date=$(LC_ALL=C stat "$1" | grep "^Modify: " \
248 | cut -d':' -f2- | cut -d. -f1)
250 date=$(LC_ALL=C stat -f "%Sc" "$1")
252 date=$(echo "$date" | sed -e's/^ //g' -e 's/[-:]//g' -e 's/ /-/g')
256 date="$(date +%Y%m%d-%H%M%S)"
258 snapshot="$(echo "$sys_target/current" | cut -c2-)@$date"
259 sys_target="$sys_target/current"
262 echo "Initialize_Last_SysTarget_Snapshot: Incompatible FS type \"$fs\" on \"$sys_target\"!"
268 # Search configuration file (last one is used as default!)
270 "/usr/local/etc/backup-script.conf" \
271 "/etc/backup-script.conf" \
272 "${conf_d}/backup-script.conf" \
273 "/usr/local/etc/backup-script.conf" \
275 [ -r "$conf" ] && break
278 # Read in configuration file
279 config_info="Configuration file is \"$conf\""
280 if [ -r "$conf" ]; then
283 config_info="${config_info} (not readable, using defaults)"
285 config_info="${config_info},\nusing \"$conf_d\" as configuration directory."
287 while [ $# -gt 0 ]; do
303 echo -n "Started: "; date
304 echo -e "$config_info"
306 # Check rsync and its protocol version
307 rsync=$(which "rsync" 2>/dev/null)
308 if [ $? -ne 0 ]; then
309 echo "Failed to detect rsync(1)! Is it installed in your \$PATH?"
312 rsync_proto=$($rsync --version 2>/dev/null | head -n 1 | sed 's/.* protocol version \([0-9]*\)$/\1/')
313 if [ $? -ne 0 ]; then
314 echo "Failed to detect protocol version of $rsync!"
317 echo "Rsync command is $rsync, protocol version $rsync_proto."
320 trap GotSignal SIGINT
322 if [ $# -ge 1 ]; then
324 if [ ! -r "${conf_d}/$s" ]; then
325 echo "$NAME: Can' read \"${conf_d}/$s\"!"
328 sys="$sys ${conf_d}/$s"
334 # check and create PID file
335 if [ -e "$PIDFILE" ]; then
336 echo "Lockfile \"$PIDFILE\" already exists."
337 echo "Is an other instance still running?"
339 echo -n "Aborted: "; date
343 touch "$PIDFILE" 2>/dev/null
344 if [ $? -ne 0 ]; then
345 echo "Warning: can't create PID file \"$PIDFILE\"!"
348 echo "$$" >>"$PIDFILE"
351 if [ -n "$pre_exec" ]; then
352 echo "Executing \"$pre_exec\" ..."
354 if [ $? -ne 0 ]; then
355 echo "Error: pre-exec command failed!"; echo
357 echo "Aborting backup."; echo
365 [[ -r "$f" && -f "$f" ]] || continue
367 fname=$(basename "$f")
369 "backup-script.conf"|*.sh)
374 # Set global defaults
376 backup_type="$default_backup_type"
378 source_root="$default_source_root"
379 files="$default_files"
380 target="$default_target"
381 ssh_args_add="$default_ssh_args_add"
382 rsync_args_add="$default_rsync_args_add"
383 exclude_args_add="$default_exclude_args_add"
384 exclude_dirs_add="$default_exclude_dirs_add"
385 compress="$default_compress"
387 local="$default_local"
388 generations="$default_generations"
389 job_pre_exec="$default_job_pre_exec"
390 job_post_exec="$default_job_post_exec"
392 # Compatibility with backup-pull(1) script: Save global values ...
393 pre_exec_saved="$pre_exec"
394 post_exec_saved="$post_exec"
396 # Compatibility with backup-pull(1) script: Set defaults
402 # Read in system configuration file
405 # Compatibility with backup-pull(1) script: Fix up configuration
406 [[ "$system" = "$fname" && -n "$host" ]] \
408 [[ "$source_root" = "$default_source_root" && -n "$source" ]] \
409 && source_root="$source"
410 [[ -z "$job_pre_exec" && -n "$pre_exec" ]] \
411 && job_pre_exec="$pre_exec"
412 [[ -z "$job_post_exec" && -n "$post_exec" ]] \
413 && job_post_exec="$post_exec"
415 # Compatibility with backup-pull(1) script: Restore global values ...
416 pre_exec="$pre_exec_saved"
417 post_exec="$post_exec_saved"
419 # Validate configuration
420 if [[ "$system" = "localhost" || "$system" = "127.0.0.1" ]]; then
426 # Make sure "source" ends with a slash ("/")
434 # Make sure "target" DOESN'T end with a slash ("/")
437 target=$( echo "$target" | sed -e 's/\/$//g' )
441 [ "$system" = "$fname" ] \
442 && systxt="\"$system\"" \
443 || systxt="\"$fname\" [\"$system\"]"
445 && echo "Working on $systxt ..." \
446 || echo "Working on $systxt (local system) ..."
448 count_all=$count_all+1
450 # Check target directory
451 if [ -z "$target" ]; then
452 echo "No target directory specified for \"$system\"!? Skipped!"
455 if [ ! -d "$target" ]; then
456 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
460 sys_target="$target/$fname"
461 sys_root="$sys_target"
462 if [[ "$DRYRUN" -eq 0 && ! -e "$sys_target" ]]; then
463 if [ $generations -gt 0 ]; then
464 CreateSubvolume "$sys_target"
466 mkdir -p "$sys_target"
468 if [ $? -ne 0 ]; then
469 echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
474 if [[ "$local" -eq 0 && "$ping" -ne 0 ]]; then
475 # Check if system is alive
476 ping -c 1 "$system" >/dev/null 2>&1
477 if [ $? -ne 0 ]; then
478 echo "Host \"$system\" seems not to be alive!? Skipped."
481 echo "OK, host \"$system\" seems to be alive."
484 if [ $generations -gt 0 ]; then
485 # Make sure no old backup is stored in system directory
486 if [ -e "$sys_target/.stamp" ]; then
487 # There seems to be a genearation-less backup in the
489 echo "Target directory \"$sys_target\" seems to be unclean!? \"$system\" skipped!"
493 Initialize_Last_SysTarget_Snapshot "$sys_target" || continue
495 if [[ -n "$last" && ! -e "$last/.stamp" ]]; then
496 # Old backup directory without "stamp file", continue
497 echo "Found incomplete snapshot in \"$last\", reusing and renaming it ..."
498 RenameSubvolume "$last" "$sys_target"
499 if [ $? -ne 0 ]; then
500 echo "Failed to rename last snapshot \"$last\" to \"$sys_target\"!? \"$system\" skipped!"
503 elif [ -n "$last" ]; then
504 # Old backup directory found, create new snapshot
505 echo "Found last snapshot in \"$last\"."
506 if [ "$DRYRUN" -eq 0 ]; then
507 CloneSubvolume "$last" "$sys_target" "$snapshot"; r=$?
508 if [ $r -ne 0 ]; then
509 echo "Can't create snapshot \"$snapshot\" of \"$last\", code $r!? \"$system\" skipped!"
512 echo "Created new snapshot in \"$snapshot\"."
514 echo " *** Trial run, not creating new snapshot in \"$snapshot\"!"
517 # No old backup found, create new subvolume
518 if [ "$DRYRUN" -eq 0 ]; then
519 CreateSubvolume "$sys_target"; r=$?
520 if [ $r -ne 0 ]; then
521 echo "Can't create subvolume \"$sys_target\", code $r!? \"$system\" skipped!"
524 echo "Created new subvolume in \"$sys_target\"."
526 echo " *** Trial run, not creating new subvolume \"$sys_target\"!"
532 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
534 # execute job "pre-exec" command, if any
535 if [ -n "$job_pre_exec" ]; then
536 ExecJob pre "$job_pre_exec" ; ret=$?
537 if [ $ret -ne 0 ]; then
538 [ $ret -ne 99 ] && count_started=$count_started+1
539 echo "Pre-exec command failed, \"$system\" skipped!"
544 # prepare (remote) command ...
545 if [[ "$backup_type" == "rsync" ]]; then
546 cmd="$rsync --archive"
547 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
548 [ "$local" -eq 0 ] && cmd="$cmd --rsh=\"$ssh_cmd\""
549 cmd="$cmd --delete --delete-excluded --sparse"
550 if [ "$VERBOSE" -gt 0 ]; then
551 [ "$rsync_proto" -ge 31 ] \
552 && cmd="$cmd --info=progress2" \
553 || cmd="$cmd --progress"
555 if [ "$source_root" = "$default_source_root" ]; then
565 "/var/cache/apt/**" \
569 cmd="$cmd --exclude=$dir"
572 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
573 for dir in $exclude_dirs_add; do
574 cmd="$cmd --exclude=$dir"
576 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
579 && cmd="$cmd ${user}@${system}:$source_root $sys_target/" \
580 || cmd="$cmd $source_root $sys_target/"
581 elif [[ "$backup_type" == "scp" ]]; then
583 [ "$VERBOSE" -eq 0 ] && cmd="$cmd -q"
584 for file in $files; do
585 cmd="$cmd ${user}@${system}:$file $sys_target/"
588 echo "Backup type \"$backup_type\" undefined, \"$system\" skipped!"
592 echo "Backing up to \"$sys_target\" ..."
593 echo -n "Start date: "; date
595 count_started=$count_started+1
598 if [ "$DRYRUN" -eq 0 ]; then
599 stamp_file="$sys_target/.stamp"
602 # Execute backup command:
603 start_t=$(date "+%s")
604 $SHELL -c "$cmd"; ret=$?
607 echo "code=$ret" >"$stamp_file"
608 echo "start_t=$start_t" >>"$stamp_file"
609 echo "end_t=$end_t" >>"$stamp_file"
610 echo "cmd='$cmd'" >>"$stamp_file"
611 echo "backup_host='`hostname -f`'" >>"$stamp_file"
612 echo "backup_user='`id -un`'" >>"$stamp_file"
614 echo " *** Trial run, not executing save command!"
618 if [ $ret -eq 20 ]; then
619 echo "Backup of \"$system\" interrupted. Aborting ..."
623 echo -n "End date: "; date
624 if [[ $ret -eq 0 || $ret -eq 24 ]]; then
625 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
627 echo "System \"$system\" completed with status $ret, OK."
628 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
631 echo "System \"$system\" completed with ERRORS, code $ret!"
634 # execute job "post-exec" command, if any
635 if [ -n "$job_post_exec" ]; then
636 ExecJob post "$job_post_exec"
639 if [ $generations -gt 0 ]; then
640 # Update "latest" symlink
641 if [ "$DRYRUN" -eq 0 ]; then
642 rm -f "$sys_root/latest"
643 ln -s "$sys_target" "$sys_root/latest"
645 # Clean up old generations
646 declare -i gen_count=$generations+2
647 to_delete=$(ls -1t "$sys_root" 2>/dev/null | tail -n+$gen_count | sort)
648 if [[ -n "$to_delete" && $ok -eq 1 ]]; then
649 [ "$DRYRUN" -eq 0 ] \
650 && echo "Deleting old backup generations (keep $generations) ..." \
651 || echo " *** Trial run, not deleting old generations:"
652 for delete in $to_delete; do
653 dir="$sys_root/$delete"
654 if [ ! -e "$dir/.stamp" ]; then
655 echo "Not deleting \"$dir\", not a backup directory!?"
658 last=$(stat "$dir/.stamp" 2>/dev/null | grep "^Modify: " \
659 | cut -d':' -f2- | cut -d. -f1)
660 echo "Removing backup from" $last "..."
661 if [ "$DRYRUN" -eq 0 ]; then
662 DeleteSubvolume "$dir"
664 echo "Failed to delete \"$dir\"!"
667 echo -n "Clean up finished: "; date
668 elif [ -n "$to_delete" ]; then
669 echo "There have been errors, not cleaning up old generations!"
671 echo "Nothing to clean up."
675 destinations="$destinations $target"
681 if [ "$DRYRUN" -eq 0 ]; then
684 for dest in $(echo $destinations | sed -e 's/ /\n/g' | sort | uniq); do
688 paths_zfs="$paths_zfs $dest"
694 if [ -n "$paths" ]; then
698 if [ -n "$paths_zfs" ]; then
706 echo -n "Done: "; date
708 [ $count_all -eq 1 ] && s="" || s="s"
709 echo " - $count_all job$s defined,"
710 [ $count_started -eq 1 ] && s="" || s="s"
711 echo " - $count_started job$s started,"
712 echo " - $count_ok done without errors."
715 if [ $count_started -ne $count_ok ]; then
716 echo "-----> THERE HAVE BEEN ERRORS! <-----"
719 elif [ $count_all -ne $count_started ]; then