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."
55 echo "Configuration file is \"$conf\","
56 echo "using \"$conf_d\" as configuration directory."
62 if [ -n "$post_exec" ]; then
63 echo "Executing \"$post_exec\" ..."
66 echo "Warning: post-exec command failed!"
75 echo "--> Got break signal, cleaning up & aborting ..."
78 echo -n "Aborted: "; date
88 echo "Running job ${what}-exec command ..."
90 && cmd="$ssh_cmd ${user}@${system} $cmd"
91 echo -n "Start date (${what}-exec): "; date
93 if [ "$DRYRUN" -eq 0 ]; then
94 $SHELL -c "$cmd"; local ret=$?
96 echo " *** Trial run, not executing ${what}-exec command!"
100 && echo "The ${what}-exec command completed with status 0, OK." \
101 || echo "The ${what}-exec command completed with ERRORS, code $ret!"
108 while [ -n "$dir" ]; do
109 findmnt -fn -o FSTYPE --raw "$dir" 2>/dev/null; local r=$?
110 if [ $r -eq 0 ]; then
112 elif [ $r -eq 127 ]; then
116 dir=$(dirname "$dir") || return 1
125 dir=$(dirname "$volume")
129 btrfs subvolume create "$volume" >/dev/null || return 1
132 zfs create "$(echo "$volume" | cut -c2-)" || return 1
135 echo "CreateSubvolume: Incompatible FS type \"$fs\" on \"$dir\"!"
149 dir=$(dirname "source")
150 fs=$(GetFS "$source")
153 btrfs subvolume snapshot "$source" "$snapshot" >/dev/null || return 1
156 zfs snapshot "$snapshot" || return 1
157 link_name="$(echo "$snapshot" | cut -d@ -f2-)"
159 "$volume/.zfs/snapshot/$link_name" \
160 "$(dirname "$volume")/$link_name"
163 echo "CloneSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
174 fs=$(GetFS "$source")
177 mv "$source" "$target" || return 1
181 "$(echo "$source" | cut -c2-)" \
182 "$(echo "$target" | cut -c2-)" \
186 echo "RenameSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
198 fs=$(GetFS "$volume")
201 btrfs subvolume delete "$volume" >/dev/null || return 1
204 id="$(basename "$volume")"
205 if [ -h "$volume" ]; then
206 snapshot="$(dirname "$volume")/current@$id"
210 zfs destroy -r "$(echo "$snapshot" | cut -c2-)" >/dev/null || return 1
211 [ -h "$volume" ] && rm "$volume"
214 echo "DeleteSubvolume: Incompatible FS type \"$fs\" on \"$volume\"!"
220 Initialize_Last_SysTarget_Snapshot() {
225 fs=$(GetFS "$sys_target")
228 # Search directory of last generation, if any
229 last=$(ls -1d "$sys_target"/[0-9]* 2>/dev/null | sort -r | head -n1)
230 if [ -n "$last" ]; then
231 if [ ! -d "$last" ]; then
232 echo "Last snapshot \"$last\" seems not to be a directory!? \"$system\" skipped!"
237 sys_target="$sys_target/$(date +%Y%m%d-%H%M%S)"
238 snapshot="$sys_target"
241 # On ZFS, the last generation is always named "current"
242 if [ -e "$sys_target/current" ]; then
243 last="$sys_target/current"
244 if [ "$(uname)" = "Linux" ]; then
245 date=$(LC_ALL=C stat "$1" | grep "^Modify: " \
246 | cut -d':' -f2- | cut -d. -f1)
248 date=$(LC_ALL=C stat -f "%Sc" "$1")
250 date=$(echo "$date" | sed -e's/^ //g' -e 's/[-:]//g' -e 's/ /-/g')
254 date="$(date +%Y%m%d-%H%M%S)"
256 snapshot="$(echo "$sys_target/current" | cut -c2-)@$date"
257 sys_target="$sys_target/current"
260 echo "Initialize_Last_SysTarget_Snapshot: Incompatible FS type \"$fs\" on \"$sys_target\"!"
266 # Search configuration file (last one is used as default!)
268 "/usr/local/etc/backup-script.conf" \
269 "/etc/backup-script.conf" \
270 "${conf_d}/backup-script.conf" \
271 "/usr/local/etc/backup-script.conf" \
273 [ -r "$conf" ] && break
276 while [ $# -gt 0 ]; do
292 trap GotSignal SIGINT
294 echo -n "Started: "; date
296 # Read in configuration file
297 if [ -r "$conf" ]; then
298 echo "Reading configuration: \"$conf\" ..."
301 echo "No configuration file found, using defaults."
305 if [ $# -ge 1 ]; then
307 if [ ! -r "${conf_d}/$s" ]; then
308 echo "$NAME: Can' read \"${conf_d}/$s\"!"
311 sys="$sys ${conf_d}/$s"
317 # check and create PID file
318 if [ -e "$PIDFILE" ]; then
319 echo "Lockfile \"$PIDFILE\" already exists."
320 echo "Is an other instance still running?"
322 echo -n "Aborted: "; date
326 touch "$PIDFILE" 2>/dev/null
327 if [ $? -ne 0 ]; then
328 echo "Warning: can't create PID file \"$PIDFILE\"!"
331 echo "$$" >>"$PIDFILE"
334 if [ -n "$pre_exec" ]; then
335 echo "Executing \"$pre_exec\" ..."
337 if [ $? -ne 0 ]; then
338 echo "Error: pre-exec command failed!"; echo
340 echo "Aborting backup."; echo
348 [ -r "$f" -a -f "$f" ] || continue
350 fname=$(basename "$f")
352 "backup-script.conf"|*.sh)
357 # Set global defaults
360 source_root="$default_source_root"
361 target="$default_target"
362 ssh_args_add="$default_ssh_args_add"
363 rsync_args_add="$default_rsync_args_add"
364 exclude_args_add="$default_exclude_args_add"
365 compress="$default_compress"
367 local="$default_local"
368 generations="$default_generations"
369 job_pre_exec="$default_job_pre_exec"
370 job_post_exec="$default_job_post_exec"
372 # Compatibility with backup-pull(1) script: Save global values ...
373 pre_exec_saved="$pre_exec"
374 post_exec_saved="$post_exec"
376 # Compatibility with backup-pull(1) script: Set defaults
382 # Read in system configuration file
385 # Compatibility with backup-pull(1) script: Fix up configuration
386 [ "$system" = "$fname" -a -n "$host" ] \
388 [ "$source_root" = "$default_source_root" -a -n "$source" ] \
389 && source_root="$source"
390 [ -z "$job_pre_exec" -a -n "$pre_exec" ] \
391 && job_pre_exec="$pre_exec"
392 [ -z "$job_post_exec" -a -n "$post_exec" ] \
393 && job_post_exec="$post_exec"
395 # Compatibility with backup-pull(1) script: Restore global values ...
396 pre_exec="$pre_exec_saved"
397 post_exec="$post_exec_saved"
399 # Validate configuration
400 if [ "$system" = "localhost" -o "$system" = "127.0.0.1" ]; then
406 # Make sure "source" ends with a slash ("/")
414 # Make sure "target" DOESN'T end with a slash ("/")
417 target=$( echo "$target" | sed -e 's/\/$//g' )
421 [ "$system" = "$fname" ] \
422 && systxt="\"$system\"" \
423 || systxt="\"$fname\" [\"$system\"]"
425 && echo "Working on $systxt ..." \
426 || echo "Working on $systxt (local system) ..."
428 count_all=$count_all+1
430 # Check target directory
431 if [ -z "$target" ]; then
432 echo "No target directory specified for \"$system\"!? Skipped!"
435 if [ ! -d "$target" ]; then
436 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
440 sys_target="$target/$fname"
441 sys_root="$sys_target"
442 if [ "$DRYRUN" -eq 0 -a ! -e "$sys_target" ]; then
443 if [ $generations -gt 0 ]; then
444 CreateSubvolume "$sys_target"
446 mkdir -p "$sys_target"
448 if [ $? -ne 0 ]; then
449 echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
454 if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
455 # Check if system is alive
456 ping -c 1 "$system" >/dev/null 2>&1
457 if [ $? -ne 0 ]; then
458 echo "Host \"$system\" seems not to be alive!? Skipped."
461 echo "OK, host \"$system\" seems to be alive."
464 if [ $generations -gt 0 ]; then
465 # Make sure no old backup is stored in system directory
466 if [ -e "$sys_target/.stamp" ]; then
467 # There seems to be a genearation-less backup in the
469 echo "Target directory \"$sys_target\" seems to be unclean!? \"$system\" skipped!"
473 Initialize_Last_SysTarget_Snapshot "$sys_target" || continue
475 if [ -n "$last" -a ! -e "$last/.stamp" ]; then
476 # Old backup directory without "stamp file", continue
477 echo "Found incomplete snapshot in \"$last\", reusing and renaming it ..."
478 RenameSubvolume "$last" "$sys_target"
479 if [ $? -ne 0 ]; then
480 echo "Failed to rename last snapshot \"$last\" to \"$sys_target\"!? \"$system\" skipped!"
483 elif [ -n "$last" ]; then
484 # Old backup directory found, create new snapshot
485 echo "Found last snapshot in \"$last\"."
486 if [ "$DRYRUN" -eq 0 ]; then
487 CloneSubvolume "$last" "$sys_target" "$snapshot"; r=$?
488 if [ $r -ne 0 ]; then
489 echo "Can't create snapshot \"$snapshot\" of \"$last\", code $r!? \"$system\" skipped!"
492 echo "Created new snapshot in \"$snapshot\"."
494 echo " *** Trial run, not creating new snapshot in \"$snapshot\"!"
497 # No old backup found, create new subvolume
498 if [ "$DRYRUN" -eq 0 ]; then
499 CreateSubvolume "$sys_target"; r=$?
500 if [ $r -ne 0 ]; then
501 echo "Can't create subvolume \"$sys_target\", code $r!? \"$system\" skipped!"
504 echo "Created new subvolume in \"$sys_target\"."
506 echo " *** Trial run, not creating new subvolume \"$sys_target\"!"
512 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
514 # execute job "pre-exec" command, if any
515 if [ -n "$job_pre_exec" ]; then
516 ExecJob pre "$job_pre_exec" ; ret=$?
517 if [ $ret -ne 0 ]; then
518 [ $ret -ne 99 ] && count_started=$count_started+1
519 echo "Pre-exec command failed, \"$system\" skipped!"
524 # prepare (remote) command ...
525 cmd="rsync --archive"
526 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
527 [ "$local" -eq 0 ] && cmd="$cmd --rsh=\"$ssh_cmd\""
528 cmd="$cmd --delete --delete-excluded --sparse"
529 [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
530 if [ "$source_root" = "$default_source_root" ]; then
531 cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
532 cmd="$cmd --exclude=/run --exclude=/tmp --exclude=/var/tmp"
533 cmd="$cmd --exclude=/media --exclude=/mnt --exclude=/net"
534 cmd="$cmd --exclude=/var/cache/apt --exclude=/var/log"
536 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
537 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
540 && cmd="$cmd ${user}@${system}:$source_root $sys_target/" \
541 || cmd="$cmd $source_root $sys_target/"
543 echo "Backing up to \"$sys_target\" ..."
544 echo -n "Start date: "; date
546 count_started=$count_started+1
549 if [ "$DRYRUN" -eq 0 ]; then
550 rm -f "$sys_target/.stamp"
551 $SHELL -c "$cmd"; ret=$?
552 echo "code=$ret" >"$sys_target/.stamp"
554 echo " *** Trial run, not executing synchronization command!"
558 if [ $ret -eq 20 ]; then
559 echo "Backup of \"$system\" interrupted. Aborting ..."
563 echo -n "End date: "; date
564 if [ $ret -eq 0 -o $ret -eq 24 ]; then
565 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
567 echo "System \"$system\" completed with status $ret, OK."
568 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
571 echo "System \"$system\" completed with ERRORS, code $ret!"
574 # execute job "post-exec" command, if any
575 if [ -n "$job_post_exec" ]; then
576 ExecJob post "$job_post_exec"
579 if [ $generations -gt 0 ]; then
580 # Update "latest" symlink
581 if [ "$DRYRUN" -eq 0 ]; then
582 rm -f "$sys_root/latest"
583 ln -s "$sys_target" "$sys_root/latest"
585 # Clean up old generations
586 declare -i gen_count=$generations+2
587 to_delete=$(ls -1t "$sys_root" 2>/dev/null | tail -n+$gen_count | sort)
588 if [ -n "$to_delete" -a $ok -eq 1 ]; then
589 [ "$DRYRUN" -eq 0 ] \
590 && echo "Deleting old backup generations (keep $generations) ..." \
591 || echo " *** Trial run, not deleting old generations:"
592 for delete in $to_delete; do
593 dir="$sys_root/$delete"
594 if [ ! -e "$dir/.stamp" ]; then
595 echo "Not deleting \"$dir\", not a backup directory!?"
598 last=$(stat "$dir/.stamp" 2>/dev/null | grep "^Modify: " \
599 | cut -d':' -f2- | cut -d. -f1)
600 echo "Removing backup from" $last "..."
601 if [ "$DRYRUN" -eq 0 ]; then
602 DeleteSubvolume "$dir"
604 echo "Failed to delete \"$dir\"!"
607 echo -n "Clean up finished: "; date
608 elif [ -n "$to_delete" ]; then
609 echo "There have been errors, not cleaning up old generations!"
611 echo "Nothing to clean up."
615 destinations="$destinations $target"
621 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
622 if [ "$DRYRUN" -eq 0 -a -n "$paths" ]; then
629 echo -n "Done: "; date
631 [ $count_all -eq 1 ] && s="" || s="s"
632 echo " - $count_all job$s defined,"
633 [ $count_started -eq 1 ] && s="" || s="s"
634 echo " - $count_started job$s started,"
635 echo " - $count_ok done without errors."
638 if [ $count_started -ne $count_ok ]; then
639 echo "-----> THERE HAVE BEEN ERRORS! <-----"
642 elif [ $count_all -ne $count_started ]; then