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_target="/var/backups"
38 default_ssh_args_add=""
39 default_rsync_args_add=""
40 default_exclude_args_add=""
41 default_exclude_dirs_add=""
46 default_job_pre_exec=""
47 default_job_post_exec=""
50 echo "Usage: $NAME [<options>] [<system> [<system> [...]]]"
52 echo " -p, --progress Show progress, see rsync(1)."
53 echo " -n, --dry-run Test run only, don't copy any data."
55 echo "When no <system> is given, all defined systems are used."
63 if [ -n "$post_exec" ]; then
64 echo "Executing \"$post_exec\" ..."
67 echo "Warning: post-exec command failed!"
76 echo "--> Got break signal, cleaning up & aborting ..."
79 echo -n "Aborted: "; date
89 echo "Running job ${what}-exec command ..."
91 && cmd="$ssh_cmd ${user}@${system} $cmd"
92 echo -n "Start date (${what}-exec): "; date
94 if [ "$DRYRUN" -eq 0 ]; then
95 $SHELL -c "$cmd"; local ret=$?
97 echo " *** Trial run, not executing ${what}-exec command!"
101 && echo "The ${what}-exec command completed with status 0, OK." \
102 || echo "The ${what}-exec command completed with ERRORS, code $ret!"
109 while [ -n "$dir" ]; do
110 findmnt -fn -o FSTYPE --raw "$dir" 2>/dev/null; local r=$?
111 if [ $r -eq 0 ]; then
113 elif [ $r -eq 127 ]; then
117 dir=$(dirname "$dir") || return 1
126 dir=$(dirname "$volume")
130 btrfs subvolume create "$volume" >/dev/null || return 1
133 zfs create "$(echo "$volume" | cut -c2-)" || return 1
136 echo "CreateSubvolume: Incompatible FS type \"$fs\" on \"$dir\"!"
150 dir=$(dirname "source")
151 fs=$(GetFS "$source")
154 btrfs subvolume snapshot "$source" "$snapshot" >/dev/null || return 1
157 zfs snapshot "$snapshot" || return 1
158 link_name="$(echo "$snapshot" | cut -d@ -f2-)"
160 "current/.zfs/snapshot/$link_name" \
161 "$(dirname "$volume")/$link_name"
164 echo "CloneSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
175 fs=$(GetFS "$source")
178 mv "$source" "$target" || return 1
182 "$(echo "$source" | cut -c2-)" \
183 "$(echo "$target" | cut -c2-)" \
187 echo "RenameSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
199 fs=$(GetFS "$volume")
202 btrfs subvolume delete "$volume" >/dev/null || return 1
205 id="$(basename "$volume")"
206 if [ -h "$volume" ]; then
207 snapshot="$(dirname "$volume")/current@$id"
211 zfs destroy -r "$(echo "$snapshot" | cut -c2-)" >/dev/null || return 1
212 [ -h "$volume" ] && rm "$volume"
215 echo "DeleteSubvolume: Incompatible FS type \"$fs\" on \"$volume\"!"
221 Initialize_Last_SysTarget_Snapshot() {
226 fs=$(GetFS "$sys_target")
229 # Search directory of last generation, if any
230 last=$(ls -1d "$sys_target"/[0-9]* 2>/dev/null | sort -r | head -n1)
231 if [ -n "$last" ]; then
232 if [ ! -d "$last" ]; then
233 echo "Last snapshot \"$last\" seems not to be a directory!? \"$system\" skipped!"
238 sys_target="$sys_target/$(date +%Y%m%d-%H%M%S)"
239 snapshot="$sys_target"
242 # On ZFS, the last generation is always named "current"
243 if [ -e "$sys_target/current" ]; then
244 last="$sys_target/current"
245 if [ "$(uname)" = "Linux" ]; then
246 date=$(LC_ALL=C stat "$1" | grep "^Modify: " \
247 | cut -d':' -f2- | cut -d. -f1)
249 date=$(LC_ALL=C stat -f "%Sc" "$1")
251 date=$(echo "$date" | sed -e's/^ //g' -e 's/[-:]//g' -e 's/ /-/g')
255 date="$(date +%Y%m%d-%H%M%S)"
257 snapshot="$(echo "$sys_target/current" | cut -c2-)@$date"
258 sys_target="$sys_target/current"
261 echo "Initialize_Last_SysTarget_Snapshot: Incompatible FS type \"$fs\" on \"$sys_target\"!"
267 # Search configuration file (last one is used as default!)
269 "/usr/local/etc/backup-script.conf" \
270 "/etc/backup-script.conf" \
271 "${conf_d}/backup-script.conf" \
272 "/usr/local/etc/backup-script.conf" \
274 [ -r "$conf" ] && break
277 # Read in configuration file
278 config_info="Configuration file is \"$conf\""
279 if [ -r "$conf" ]; then
282 config_info="${config_info} (not readable, using defaults)"
284 config_info="${config_info},\nusing \"$conf_d\" as configuration directory."
286 while [ $# -gt 0 ]; do
302 echo -n "Started: "; date
303 echo -e "$config_info"
305 # Check rsync and its protocol version
306 rsync=$(which "rsync" 2>/dev/null)
307 if [ $? -ne 0 ]; then
308 echo "Failed to detect rsync(1)! Is it installed in your \$PATH?"
311 rsync_proto=$($rsync --version 2>/dev/null | head -n 1 | sed 's/.* protocol version \([0-9]*\)$/\1/')
312 if [ $? -ne 0 ]; then
313 echo "Failed to detect protocol version of $rsync!"
316 echo "Rsync command is $rsync, protocol version $rsync_proto."
319 trap GotSignal SIGINT
321 if [ $# -ge 1 ]; then
323 if [ ! -r "${conf_d}/$s" ]; then
324 echo "$NAME: Can' read \"${conf_d}/$s\"!"
327 sys="$sys ${conf_d}/$s"
333 # check and create PID file
334 if [ -e "$PIDFILE" ]; then
335 echo "Lockfile \"$PIDFILE\" already exists."
336 echo "Is an other instance still running?"
338 echo -n "Aborted: "; date
342 touch "$PIDFILE" 2>/dev/null
343 if [ $? -ne 0 ]; then
344 echo "Warning: can't create PID file \"$PIDFILE\"!"
347 echo "$$" >>"$PIDFILE"
350 if [ -n "$pre_exec" ]; then
351 echo "Executing \"$pre_exec\" ..."
353 if [ $? -ne 0 ]; then
354 echo "Error: pre-exec command failed!"; echo
356 echo "Aborting backup."; echo
364 [[ -r "$f" && -f "$f" ]] || continue
366 fname=$(basename "$f")
368 "backup-script.conf"|*.sh)
373 # Set global defaults
375 backup_type="$default_backup_type"
377 source_root="$default_source_root"
378 target="$default_target"
379 ssh_args_add="$default_ssh_args_add"
380 rsync_args_add="$default_rsync_args_add"
381 exclude_args_add="$default_exclude_args_add"
382 exclude_dirs_add="$default_exclude_dirs_add"
383 compress="$default_compress"
385 local="$default_local"
386 generations="$default_generations"
387 job_pre_exec="$default_job_pre_exec"
388 job_post_exec="$default_job_post_exec"
390 # Compatibility with backup-pull(1) script: Save global values ...
391 pre_exec_saved="$pre_exec"
392 post_exec_saved="$post_exec"
394 # Compatibility with backup-pull(1) script: Set defaults
400 # Read in system configuration file
403 # Compatibility with backup-pull(1) script: Fix up configuration
404 [[ "$system" = "$fname" && -n "$host" ]] \
406 [[ "$source_root" = "$default_source_root" && -n "$source" ]] \
407 && source_root="$source"
408 [[ -z "$job_pre_exec" && -n "$pre_exec" ]] \
409 && job_pre_exec="$pre_exec"
410 [[ -z "$job_post_exec" && -n "$post_exec" ]] \
411 && job_post_exec="$post_exec"
413 # Compatibility with backup-pull(1) script: Restore global values ...
414 pre_exec="$pre_exec_saved"
415 post_exec="$post_exec_saved"
417 # Validate configuration
418 if [[ "$system" = "localhost" || "$system" = "127.0.0.1" ]]; then
424 # Make sure "source" ends with a slash ("/")
432 # Make sure "target" DOESN'T end with a slash ("/")
435 target=$( echo "$target" | sed -e 's/\/$//g' )
439 [ "$system" = "$fname" ] \
440 && systxt="\"$system\"" \
441 || systxt="\"$fname\" [\"$system\"]"
443 && echo "Working on $systxt ..." \
444 || echo "Working on $systxt (local system) ..."
446 count_all=$count_all+1
448 # Check target directory
449 if [ -z "$target" ]; then
450 echo "No target directory specified for \"$system\"!? Skipped!"
453 if [ ! -d "$target" ]; then
454 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
458 sys_target="$target/$fname"
459 sys_root="$sys_target"
460 if [[ "$DRYRUN" -eq 0 && ! -e "$sys_target" ]]; then
461 if [ $generations -gt 0 ]; then
462 CreateSubvolume "$sys_target"
464 mkdir -p "$sys_target"
466 if [ $? -ne 0 ]; then
467 echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
472 if [[ "$local" -eq 0 && "$ping" -ne 0 ]]; then
473 # Check if system is alive
474 ping -c 1 "$system" >/dev/null 2>&1
475 if [ $? -ne 0 ]; then
476 echo "Host \"$system\" seems not to be alive!? Skipped."
479 echo "OK, host \"$system\" seems to be alive."
482 if [ $generations -gt 0 ]; then
483 # Make sure no old backup is stored in system directory
484 if [ -e "$sys_target/.stamp" ]; then
485 # There seems to be a genearation-less backup in the
487 echo "Target directory \"$sys_target\" seems to be unclean!? \"$system\" skipped!"
491 Initialize_Last_SysTarget_Snapshot "$sys_target" || continue
493 if [[ -n "$last" && ! -e "$last/.stamp" ]]; then
494 # Old backup directory without "stamp file", continue
495 echo "Found incomplete snapshot in \"$last\", reusing and renaming it ..."
496 RenameSubvolume "$last" "$sys_target"
497 if [ $? -ne 0 ]; then
498 echo "Failed to rename last snapshot \"$last\" to \"$sys_target\"!? \"$system\" skipped!"
501 elif [ -n "$last" ]; then
502 # Old backup directory found, create new snapshot
503 echo "Found last snapshot in \"$last\"."
504 if [ "$DRYRUN" -eq 0 ]; then
505 CloneSubvolume "$last" "$sys_target" "$snapshot"; r=$?
506 if [ $r -ne 0 ]; then
507 echo "Can't create snapshot \"$snapshot\" of \"$last\", code $r!? \"$system\" skipped!"
510 echo "Created new snapshot in \"$snapshot\"."
512 echo " *** Trial run, not creating new snapshot in \"$snapshot\"!"
515 # No old backup found, create new subvolume
516 if [ "$DRYRUN" -eq 0 ]; then
517 CreateSubvolume "$sys_target"; r=$?
518 if [ $r -ne 0 ]; then
519 echo "Can't create subvolume \"$sys_target\", code $r!? \"$system\" skipped!"
522 echo "Created new subvolume in \"$sys_target\"."
524 echo " *** Trial run, not creating new subvolume \"$sys_target\"!"
530 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
532 # execute job "pre-exec" command, if any
533 if [ -n "$job_pre_exec" ]; then
534 ExecJob pre "$job_pre_exec" ; ret=$?
535 if [ $ret -ne 0 ]; then
536 [ $ret -ne 99 ] && count_started=$count_started+1
537 echo "Pre-exec command failed, \"$system\" skipped!"
542 # prepare (remote) command ...
543 if [[ "$backup_type" == "rsync" ]]; then
544 cmd="$rsync --archive"
545 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
546 [ "$local" -eq 0 ] && cmd="$cmd --rsh=\"$ssh_cmd\""
547 cmd="$cmd --delete --delete-excluded --sparse"
548 if [ "$VERBOSE" -gt 0 ]; then
549 [ "$rsync_proto" -ge 31 ] \
550 && cmd="$cmd --info=progress2" \
551 || cmd="$cmd --progress"
553 if [ "$source_root" = "$default_source_root" ]; then
563 "/var/cache/apt/**" \
567 cmd="$cmd --exclude=$dir"
570 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
571 for dir in $exclude_dirs_add; do
572 cmd="$cmd --exclude=$dir"
574 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
577 && cmd="$cmd ${user}@${system}:$source_root $sys_target/" \
578 || cmd="$cmd $source_root $sys_target/"
580 echo "Backup type \"$backup_type\" undefined, \"$system\" skipped!"
584 echo "Backing up to \"$sys_target\" ..."
585 echo -n "Start date: "; date
587 count_started=$count_started+1
590 if [ "$DRYRUN" -eq 0 ]; then
591 rm -f "$sys_target/.stamp"
592 $SHELL -c "$cmd"; ret=$?
593 echo "code=$ret" >"$sys_target/.stamp"
595 echo " *** Trial run, not executing synchronization command!"
599 if [ $ret -eq 20 ]; then
600 echo "Backup of \"$system\" interrupted. Aborting ..."
604 echo -n "End date: "; date
605 if [[ $ret -eq 0 || $ret -eq 24 ]]; then
606 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
608 echo "System \"$system\" completed with status $ret, OK."
609 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
612 echo "System \"$system\" completed with ERRORS, code $ret!"
615 # execute job "post-exec" command, if any
616 if [ -n "$job_post_exec" ]; then
617 ExecJob post "$job_post_exec"
620 if [ $generations -gt 0 ]; then
621 # Update "latest" symlink
622 if [ "$DRYRUN" -eq 0 ]; then
623 rm -f "$sys_root/latest"
624 ln -s "$sys_target" "$sys_root/latest"
626 # Clean up old generations
627 declare -i gen_count=$generations+2
628 to_delete=$(ls -1t "$sys_root" 2>/dev/null | tail -n+$gen_count | sort)
629 if [[ -n "$to_delete" && $ok -eq 1 ]]; then
630 [ "$DRYRUN" -eq 0 ] \
631 && echo "Deleting old backup generations (keep $generations) ..." \
632 || echo " *** Trial run, not deleting old generations:"
633 for delete in $to_delete; do
634 dir="$sys_root/$delete"
635 if [ ! -e "$dir/.stamp" ]; then
636 echo "Not deleting \"$dir\", not a backup directory!?"
639 last=$(stat "$dir/.stamp" 2>/dev/null | grep "^Modify: " \
640 | cut -d':' -f2- | cut -d. -f1)
641 echo "Removing backup from" $last "..."
642 if [ "$DRYRUN" -eq 0 ]; then
643 DeleteSubvolume "$dir"
645 echo "Failed to delete \"$dir\"!"
648 echo -n "Clean up finished: "; date
649 elif [ -n "$to_delete" ]; then
650 echo "There have been errors, not cleaning up old generations!"
652 echo "Nothing to clean up."
656 destinations="$destinations $target"
662 if [ "$DRYRUN" -eq 0 ]; then
665 for dest in $(echo $destinations | sed -e 's/ /\n/g' | sort | uniq); do
669 paths_zfs="$paths_zfs $dest"
675 if [ -n "$paths" ]; then
679 if [ -n "$paths_zfs" ]; then
687 echo -n "Done: "; date
689 [ $count_all -eq 1 ] && s="" || s="s"
690 echo " - $count_all job$s defined,"
691 [ $count_started -eq 1 ] && s="" || s="s"
692 echo " - $count_started job$s started,"
693 echo " - $count_ok done without errors."
696 if [ $count_started -ne $count_ok ]; then
697 echo "-----> THERE HAVE BEEN ERRORS! <-----"
700 elif [ $count_all -ne $count_started ]; then