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 conf_d="/etc/backup-script.d"
32 default_source_root="/"
35 default_ssh_args_add=""
36 default_rsync_args_add=""
37 default_exclude_args_add=""
42 default_job_pre_exec=""
43 default_job_post_exec=""
46 echo "Usage: $NAME [<options>] [<system> [<system> [...]]]"
48 echo " -p, --progress Show progress, see rsync(1)."
49 echo " -n, --dry-run Test run only, don't copy any data."
51 echo "When no <system> is given, all defined systems are used."
57 if [ -n "$post_exec" ]; then
58 echo "Executing \"$post_exec\" ..."
61 echo "Warning: post-exec command failed!"
70 echo "--> Got break signal, cleaning up & aborting ..."
73 echo -n "Aborted: "; date
83 echo "Running job ${what}-exec command ..."
85 && cmd="$ssh_cmd ${user}@${system} $cmd"
86 echo -n "Start date (${what}-exec): "; date
88 if [ "$DRYRUN" -eq 0 ]; then
89 $SHELL -c "$cmd"; local ret=$?
91 echo " *** Trial run, not executing ${what}-exec command!"
95 && echo "The ${what}-exec command completed with status 0, OK." \
96 || echo "The ${what}-exec command completed with ERRORS, code $ret!"
103 while [ -n "$dir" ]; do
104 findmnt -fn -o FSTYPE --raw "$dir" 2>/dev/null; local r=$?
105 if [ $r -eq 0 ]; then
107 elif [ $r -eq 127 ]; then
111 dir=$(dirname "$dir") || return 1
120 dir=$(dirname "$volume")
124 btrfs subvolume create "$volume" >/dev/null || return 1
127 zfs create "$(echo "$volume" | cut -c2-)" || return 1
130 echo "CreateSubvolume: Incompatible FS type \"$fs\" on \"$dir\"!"
144 dir=$(dirname "source")
145 fs=$(GetFS "$source")
148 btrfs subvolume snapshot "$source" "$snapshot" >/dev/null || return 1
151 zfs snapshot "$snapshot" || return 1
152 link_name="$(echo "$snapshot" | cut -d@ -f2-)"
154 "$volume/.zfs/snapshot/$link_name" \
155 "$(dirname "$volume")/$link_name"
158 echo "CloneSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
169 fs=$(GetFS "$source")
172 mv "$source" "$target" || return 1
176 "$(echo "$source" | cut -c2-)" \
177 "$(echo "$target" | cut -c2-)" \
181 echo "RenameSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
193 fs=$(GetFS "$volume")
196 btrfs subvolume delete "$volume" >/dev/null || return 1
199 id="$(basename "$volume")"
200 if [ -h "$volume" ]; then
201 snapshot="$(dirname "$volume")/current@$id"
205 zfs destroy -r "$(echo "$snapshot" | cut -c2-)" >/dev/null || return 1
206 [ -h "$volume" ] && rm "$volume"
209 echo "DeleteSubvolume: Incompatible FS type \"$fs\" on \"$volume\"!"
215 Initialize_Last_SysTarget_Snapshot() {
220 fs=$(GetFS "$sys_target")
223 # Search directory of last generation, if any
224 last=$(ls -1d "$sys_target"/[0-9]* 2>/dev/null | sort -r | head -n1)
225 if [ -n "$last" ]; then
226 if [ ! -d "$last" ]; then
227 echo "Last snapshot \"$last\" seems not to be a directory!? \"$system\" skipped!"
232 sys_target="$sys_target/$(date +%Y%m%d-%H%M%S)"
233 snapshot="$sys_target"
236 # On ZFS, the last generation is always named "current"
237 if [ -e "$sys_target/current" ]; then
238 last="$sys_target/current"
239 if [ "$(uname)" = "Linux" ]; then
240 date=$(LC_ALL=C stat "$1" | grep "^Modify: " \
241 | cut -d':' -f2- | cut -d. -f1)
243 date=$(LC_ALL=C stat -f "%Sc" "$1")
245 date=$(echo "$date" | sed -e's/^ //g' -e 's/[-:]//g' -e 's/ /-/g')
249 date="$(date +%Y%m%d-%H%M%S)"
251 snapshot="$(echo "$sys_target/current" | cut -c2-)@$date"
252 sys_target="$sys_target/current"
255 echo "Initialize_Last_SysTarget_Snapshot: Incompatible FS type \"$fs\" on \"$sys_target\"!"
261 while [ $# -gt 0 ]; do
277 trap GotSignal SIGINT
279 echo -n "Started: "; date
281 for conf in "/etc/backup-script.conf" "${conf_d}/backup-script.conf"; do
282 if [ -r "$conf" ]; then
283 echo "Reading configuration: \"$conf\" ..."
289 if [ $# -ge 1 ]; then
291 if [ ! -r "${conf_d}/$s" ]; then
292 echo "$NAME: Can' read \"${conf_d}/$s\"!"
295 sys="$sys ${conf_d}/$s"
301 # check and create PID file
302 if [ -e "$PIDFILE" ]; then
303 echo "Lockfile \"$PIDFILE\" already exists."
304 echo "Is an other instance still running?"
306 echo -n "Aborted: "; date
310 touch "$PIDFILE" 2>/dev/null
311 if [ $? -ne 0 ]; then
312 echo "Warning: can't create PID file \"$PIDFILE\"!"
315 echo "$$" >>"$PIDFILE"
318 if [ -n "$pre_exec" ]; then
319 echo "Executing \"$pre_exec\" ..."
321 if [ $? -ne 0 ]; then
322 echo "Error: pre-exec command failed!"; echo
324 echo "Aborting backup."; echo
332 [ -r "$f" -a -f "$f" ] || continue
334 fname=$(basename "$f")
336 "backup-script.conf"|*.sh)
341 # Set global defaults
344 source_root="$default_source_root"
345 target="$default_target"
346 ssh_args_add="$default_ssh_args_add"
347 rsync_args_add="$default_rsync_args_add"
348 exclude_args_add="$default_exclude_args_add"
349 compress="$default_compress"
351 local="$default_local"
352 generations="$default_generations"
353 job_pre_exec="$default_job_pre_exec"
354 job_post_exec="$default_job_post_exec"
356 # Compatibility with backup-pull(1) script: Save global values ...
357 pre_exec_saved="$pre_exec"
358 post_exec_saved="$post_exec"
360 # Compatibility with backup-pull(1) script: Set defaults
366 # Read in system configuration file
369 # Compatibility with backup-pull(1) script: Fix up configuration
370 [ "$system" = "$fname" -a -n "$host" ] \
372 [ "$source_root" = "$default_source_root" -a -n "$source" ] \
373 && source_root="$source"
374 [ -z "$job_pre_exec" -a -n "$pre_exec" ] \
375 && job_pre_exec="$pre_exec"
376 [ -z "$job_post_exec" -a -n "$post_exec" ] \
377 && job_post_exec="$post_exec"
379 # Compatibility with backup-pull(1) script: Restore global values ...
380 pre_exec="$pre_exec_saved"
381 post_exec="$post_exec_saved"
383 # Validate configuration
384 [ "$system" = "localhost" -o "$system" = "127.0.0.1" ] && local=1
386 # Make sure "source" ends with a slash ("/")
394 # Make sure "target" DOESN'T end with a slash ("/")
397 target=$( echo "$target" | sed -e 's/\/$//g' )
401 [ "$system" = "$fname" ] \
402 && systxt="\"$system\"" \
403 || systxt="\"$fname\" [\"$system\"]"
405 && echo "Working on $systxt ..." \
406 || echo "Working on $systxt (local system) ..."
408 count_all=$count_all+1
410 # Check target directory
411 if [ -z "$target" ]; then
412 echo "No target directory specified for \"$system\"!? Skipped!"
415 if [ ! -d "$target" ]; then
416 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
420 sys_target="$target/$fname"
421 sys_root="$sys_target"
422 if [ "$DRYRUN" -eq 0 -a ! -e "$sys_target" ]; then
423 if [ $generations -gt 0 ]; then
424 CreateSubvolume "$sys_target"
426 mkdir -p "$sys_target"
428 if [ $? -ne 0 ]; then
429 echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
434 if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
435 # Check if system is alive
436 ping -c 1 "$system" >/dev/null 2>&1
437 if [ $? -ne 0 ]; then
438 echo "Host \"$system\" seems not to be alive!? Skipped."
441 echo "OK, host \"$system\" seems to be alive."
444 if [ $generations -gt 0 ]; then
445 # Make sure no old backup is stored in system directory
446 if [ -e "$sys_target/.stamp" ]; then
447 # There seems to be a genearation-less backup in the
449 echo "Target directory \"$sys_target\" seems to be unclean!? \"$system\" skipped!"
453 Initialize_Last_SysTarget_Snapshot "$sys_target" || continue
455 if [ -n "$last" -a ! -e "$last/.stamp" ]; then
456 # Old backup directory without "stamp file", continue
457 echo "Found incomplete snapshot in \"$last\", reusing and renaming it ..."
458 RenameSubvolume "$last" "$sys_target"
459 if [ $? -ne 0 ]; then
460 echo "Failed to rename last snapshot \"$last\" to \"$sys_target\"!? \"$system\" skipped!"
463 elif [ -n "$last" ]; then
464 # Old backup directory found, create new snapshot
465 echo "Found last snapshot in \"$last\"."
466 if [ "$DRYRUN" -eq 0 ]; then
467 CloneSubvolume "$last" "$sys_target" "$snapshot"; r=$?
468 if [ $r -ne 0 ]; then
469 echo "Can't create snapshot \"$snapshot\" of \"$last\", code $r!? \"$system\" skipped!"
472 echo "Created new snapshot in \"$snapshot\"."
474 echo " *** Trial run, not creating new snapshot in \"$snapshot\"!"
477 # No old backup found, create new subvolume
478 if [ "$DRYRUN" -eq 0 ]; then
479 CreateSubvolume "$sys_target"; r=$?
480 if [ $r -ne 0 ]; then
481 echo "Can't create subvolume \"$sys_target\", code $r!? \"$system\" skipped!"
484 echo "Created new subvolume in \"$sys_target\"."
486 echo " *** Trial run, not creating new subvolume \"$sys_target\"!"
492 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
494 # execute job "pre-exec" command, if any
495 if [ -n "$job_pre_exec" ]; then
496 ExecJob pre "$job_pre_exec" ; ret=$?
497 if [ $ret -ne 0 ]; then
498 [ $ret -ne 99 ] && count_started=$count_started+1
499 echo "Pre-exec command failed, \"$system\" skipped!"
504 # prepare (remote) command ...
505 cmd="rsync --archive"
506 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
507 [ "$local" -eq 0 ] && cmd="$cmd --rsh=\"$ssh_cmd\""
508 cmd="$cmd --delete --delete-excluded --sparse"
509 [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
510 if [ "$source_root" = "$default_source_root" ]; then
511 cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
512 cmd="$cmd --exclude=/run --exclude=/tmp --exclude=/var/tmp"
513 cmd="$cmd --exclude=/media --exclude=/mnt --exclude=/net"
514 cmd="$cmd --exclude=/var/cache/apt --exclude=/var/log"
516 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
517 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
520 && cmd="$cmd ${user}@${system}:$source_root $sys_target/" \
521 || cmd="$cmd $source_root $sys_target/"
523 echo "Backing up to \"$sys_target\" ..."
524 echo -n "Start date: "; date
526 count_started=$count_started+1
529 if [ "$DRYRUN" -eq 0 ]; then
530 rm -f "$sys_target/.stamp"
531 $SHELL -c "$cmd"; ret=$?
532 echo "code=$ret" >"$sys_target/.stamp"
534 echo " *** Trial run, not executing synchronization command!"
538 if [ $ret -eq 20 ]; then
539 echo "Backup of \"$system\" interrupted. Aborting ..."
544 echo -n "End date: "; date
545 if [ $ret -eq 0 -o $ret -eq 24 ]; then
546 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
548 echo "System \"$system\" completed with status $ret, OK."
549 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
552 echo "System \"$system\" completed with ERRORS, code $ret!"
555 # execute job "post-exec" command, if any
556 if [ -n "$job_post_exec" ]; then
557 ExecJob post "$job_post_exec"
560 if [ $generations -gt 0 ]; then
561 # Update "latest" symlink
562 if [ "$DRYRUN" -eq 0 ]; then
563 rm -f "$sys_root/latest"
564 ln -s "$sys_target" "$sys_root/latest"
566 # Clean up old generations
567 declare -i gen_count=$generations+2
568 to_delete=$(ls -1t "$sys_root" 2>/dev/null | tail -n+$gen_count | sort)
569 if [ -n "$to_delete" -a $ok -eq 1 ]; then
570 [ "$DRYRUN" -eq 0 ] \
571 && echo "Deleting old backup generations (keep $generations) ..." \
572 || echo " *** Trial run, not deleting old generations:"
573 for delete in $to_delete; do
574 dir="$sys_root/$delete"
575 if [ ! -e "$dir/.stamp" ]; then
576 echo "Not deleting \"$dir\", not a backup directory!?"
579 last=$(stat "$dir/.stamp" 2>/dev/null | grep "^Modify: " \
580 | cut -d':' -f2- | cut -d. -f1)
581 echo "Removing backup from" $last "..."
582 if [ "$DRYRUN" -eq 0 ]; then
583 DeleteSubvolume "$dir"
585 echo "Failed to delete \"$dir\"!"
588 echo -n "Clean up finished: "; date
589 elif [ -n "$to_delete" ]; then
590 echo "There have been errors, not cleaning up old generations!"
592 echo "Nothing to clean up."
596 destinations="$destinations $target"
602 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
603 if [ "$DRYRUN" -eq 0 -a -n "$paths" ]; then
610 echo -n "Done: "; date
612 [ $count_all -eq 1 ] && s="" || s="s"
613 echo " - $count_all job$s defined,"
614 [ $count_started -eq 1 ] && s="" || s="s"
615 echo " - $count_started job$s started,"
616 echo " - $count_ok done without errors."
619 if [ $count_started -ne $count_ok ]; then
620 echo "-----> THERE HAVE BEEN ERRORS! <-----"