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
82 echo "Running job ${what}-exec command ..."
84 && cmd="$ssh_cmd ${user}@${system} $cmd"
85 echo -n "Start date (${what}-exec): "; date
87 if [ "$DRYRUN" -eq 0 ]; then
88 $SHELL -c "$cmd"; local ret=$?
90 echo " *** Trial run, not executing ${what}-exec command!"
94 && echo "The ${what}-exec command completed with status 0, OK." \
95 || echo "The ${what}-exec command completed with ERRORS, code $ret!"
102 while [ -n "$dir" ]; do
103 findmnt -fn -o FSTYPE --raw "$dir" 2>/dev/null; local r=$?
104 if [ $r -eq 0 ]; then
106 elif [ $r -eq 127 ]; then
110 dir=$(dirname "$dir") || return 1
119 dir=$(dirname "$volume")
123 btrfs subvolume create "$volume" >/dev/null || return 1
126 zfs create "$(echo "$volume" | cut -c2-)" || return 1
129 echo "CreateSubvolume: Incompatible FS type \"$fs\" on \"$dir\"!"
143 dir=$(dirname "source")
144 fs=$(GetFS "$source")
147 btrfs subvolume snapshot "$source" "$snapshot" >/dev/null || return 1
150 zfs snapshot "$snapshot" || return 1
151 link_name="$(echo "$snapshot" | cut -d@ -f2-)"
153 "$volume/.zfs/snapshot/$link_name" \
154 "$(dirname "$volume")/$link_name"
157 echo "CloneSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
168 fs=$(GetFS "$source")
171 mv "$source" "$target" || return 1
175 "$(echo "$source" | cut -c2-)" \
176 "$(echo "$target" | cut -c2-)" \
180 echo "RenameSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
192 fs=$(GetFS "$volume")
195 btrfs subvolume delete "$volume" >/dev/null || return 1
198 id="$(basename "$volume")"
199 if [ -h "$volume" ]; then
200 snapshot="$(dirname "$volume")/current@$id"
204 zfs destroy -r "$(echo "$snapshot" | cut -c2-)" >/dev/null || return 1
205 [ -h "$volume" ] && rm "$volume"
208 echo "DeleteSubvolume: Incompatible FS type \"$fs\" on \"$volume\"!"
214 Initialize_Last_SysTarget_Snapshot() {
219 fs=$(GetFS "$sys_target")
222 # Search directory of last generation, if any
223 last=$(ls -1d "$sys_target"/[0-9]* 2>/dev/null | sort -r | head -n1)
224 if [ -n "$last" ]; then
225 if [ ! -d "$last" ]; then
226 echo "Last snapshot \"$last\" seems not to be a directory!? \"$system\" skipped!"
231 sys_target="$sys_target/$(date +%Y%m%d-%H%M%S)"
232 snapshot="$sys_target"
235 # On ZFS, the last generation is always named "current"
236 [ -e "$sys_target/current" ] \
237 && last="$sys_target/current" \
239 snapshot="$(echo "$sys_target/current" | cut -c2-)@$(date +%Y%m%d-%H%M%S)"
240 sys_target="$sys_target/current"
243 echo "Initialize_Last_SysTarget_Snapshot: Incompatible FS type \"$fs\" on \"$sys_target\"!"
249 while [ $# -gt 0 ]; do
265 trap GotSignal SIGINT
267 echo -n "Started: "; date
269 for conf in "/etc/backup-script.conf" "${conf_d}/backup-script.conf"; do
270 if [ -r "$conf" ]; then
271 echo "Reading configuration: \"$conf\" ..."
277 if [ $# -ge 1 ]; then
279 if [ ! -r "${conf_d}/$s" ]; then
280 echo "$NAME: Can' read \"${conf_d}/$s\"!"
283 sys="$sys ${conf_d}/$s"
289 # check and create PID file
290 if [ -e "$PIDFILE" ]; then
291 echo "Lockfile \"$PIDFILE\" already exists."
292 echo "Is an other instance still running?"
294 echo -n "Aborted: "; date
298 touch "$PIDFILE" 2>/dev/null
299 if [ $? -ne 0 ]; then
300 echo "Warning: can't create PID file \"$PIDFILE\"!"
303 echo "$$" >>"$PIDFILE"
306 if [ -n "$pre_exec" ]; then
307 echo "Executing \"$pre_exec\" ..."
309 if [ $? -ne 0 ]; then
310 echo "Error: pre-exec command failed!"; echo
312 echo "Aborting backup."; echo
320 [ -r "$f" -a -f "$f" ] || continue
322 fname=$(basename "$f")
324 "backup-script.conf"|*.sh)
329 # Set global defaults
332 source_root="$default_source_root"
333 target="$default_target"
334 ssh_args_add="$default_ssh_args_add"
335 rsync_args_add="$default_rsync_args_add"
336 exclude_args_add="$default_exclude_args_add"
337 compress="$default_compress"
339 local="$default_local"
340 generations="$default_generations"
341 job_pre_exec="$default_job_pre_exec"
342 job_post_exec="$default_job_post_exec"
344 # Compatibility with backup-pull(1) script: Save global values ...
345 pre_exec_saved="$pre_exec"
346 post_exec_saved="$post_exec"
348 # Compatibility with backup-pull(1) script: Set defaults
354 # Read in system configuration file
357 # Compatibility with backup-pull(1) script: Fix up configuration
358 [ "$system" = "$fname" -a -n "$host" ] \
360 [ "$source_root" = "$default_source_root" -a -n "$source" ] \
361 && source_root="$source"
362 [ -z "$job_pre_exec" -a -n "$pre_exec" ] \
363 && job_pre_exec="$pre_exec"
364 [ -z "$job_post_exec" -a -n "$post_exec" ] \
365 && job_post_exec="$post_exec"
367 # Compatibility with backup-pull(1) script: Restore global values ...
368 pre_exec="$pre_exec_saved"
369 post_exec="$post_exec_saved"
371 # Validate configuration
372 [ "$system" = "localhost" -o "$system" = "127.0.0.1" ] && local=1
374 [ "$system" = "$fname" ] \
375 && systxt="\"$system\"" \
376 || systxt="\"$fname\" [\"$system\"]"
378 && echo "Working on $systxt ..." \
379 || echo "Working on $systxt (local system) ..."
381 count_all=$count_all+1
383 # Check target directory
384 if [ -z "$target" ]; then
385 echo "No target directory specified for \"$system\"!? Skipped!"
388 if [ ! -d "$target" ]; then
389 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
393 sys_target="$target/$fname"
394 sys_root="$sys_target"
395 if [ "$DRYRUN" -eq 0 -a ! -e "$sys_target" ]; then
396 if [ $generations -gt 0 ]; then
397 CreateSubvolume "$sys_target"
399 mkdir -p "$sys_target"
401 if [ $? -ne 0 ]; then
402 echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
407 if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
408 # Check if system is alive
409 ping -c 1 "$system" >/dev/null 2>&1
410 if [ $? -ne 0 ]; then
411 echo "Host \"$system\" seems not to be alive!? Skipped."
414 echo "OK, host \"$system\" seems to be alive."
417 if [ $generations -gt 0 ]; then
418 # Make sure no old backup is stored in system directory
419 if [ -e "$sys_target/.stamp" ]; then
420 # There seems to be a genearation-less backup in the
422 echo "Target directory \"$sys_target\" seems to be unclean!? \"$system\" skipped!"
426 Initialize_Last_SysTarget_Snapshot "$sys_target" || continue
428 if [ -n "$last" -a ! -e "$last/.stamp" ]; then
429 # Old backup directory without "stamp file", continue
430 echo "Found incomplete snapshot in \"$last\", reusing and renaming it ..."
431 RenameSubvolume "$last" "$sys_target"
432 if [ $? -ne 0 ]; then
433 echo "Failed to rename last snapshot \"$last\" to \"$sys_target\"!? \"$system\" skipped!"
436 elif [ -n "$last" ]; then
437 # Old backup directory found, create new snapshot
438 echo "Found last snapshot in \"$last\"."
439 if [ "$DRYRUN" -eq 0 ]; then
440 CloneSubvolume "$last" "$sys_target" "$snapshot"; r=$?
441 if [ $r -ne 0 ]; then
442 echo "Can't create snapshot \"$snapshot\" of \"$last\", code $r!? \"$system\" skipped!"
445 echo "Created new snapshot in \"$snapshot\"."
447 echo " *** Trial run, not creating new snapshot in \"$sys_target\"!"
450 # No old backup found, create new subvolume
451 if [ "$DRYRUN" -eq 0 ]; then
452 CreateSubvolume "$sys_target"; r=$?
453 if [ $r -ne 0 ]; then
454 echo "Can't create subvolume \"$sys_target\", code $r!? \"$system\" skipped!"
457 echo "Created new subvolume in \"$sys_target\"."
459 echo " *** Trial run, not creating new subvolume \"$sys_target\"!"
465 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
467 # execute job "pre-exec" command, if any
468 if [ -n "$job_pre_exec" ]; then
469 ExecJob pre "$job_pre_exec" ; ret=$?
470 if [ $ret -ne 0 ]; then
471 [ $ret -ne 99 ] && count_started=$count_started+1
472 echo "Pre-exec command failed, \"$system\" skipped!"
477 # prepare (remote) command ...
478 cmd="rsync --archive"
479 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
480 [ "$local" -eq 0 ] && cmd="$cmd --rsh=\"$ssh_cmd\""
481 cmd="$cmd --delete --delete-excluded --sparse"
482 [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
483 if [ "$source_root" = "$default_source_root" ]; then
484 cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
485 cmd="$cmd --exclude=/run --exclude=/tmp --exclude=/var/tmp"
486 cmd="$cmd --exclude=/media --exclude=/mnt --exclude=/net"
487 cmd="$cmd --exclude=/var/cache/apt --exclude=/var/log"
489 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
490 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
493 && cmd="$cmd ${user}@${system}:$source_root $sys_target/" \
494 || cmd="$cmd $source_root $sys_target/"
496 echo "Backing up to \"$sys_target\" ..."
497 echo -n "Start date: "; date
499 count_started=$count_started+1
502 if [ "$DRYRUN" -eq 0 ]; then
503 rm -f "$sys_target/.stamp"
504 $SHELL -c "$cmd"; ret=$?
505 echo "code=$ret" >"$sys_target/.stamp"
507 echo " *** Trial run, not executing synchronization command!"
511 if [ $ret -eq 20 ]; then
512 echo "Backup of \"$system\" interrupted. Aborting ..."
517 echo -n "End date: "; date
518 if [ $ret -eq 0 -o $ret -eq 24 ]; then
519 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
521 echo "System \"$system\" completed with status $ret, OK."
522 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
525 echo "System \"$system\" completed with ERRORS, code $ret!"
528 # execute job "post-exec" command, if any
529 if [ -n "$job_post_exec" ]; then
530 ExecJob post "$job_post_exec"
533 if [ $generations -gt 0 ]; then
534 # Update "latest" symlink
535 if [ "$DRYRUN" -eq 0 ]; then
536 rm -f "$sys_root/latest"
537 ln -s "$sys_target" "$sys_root/latest"
539 # Clean up old generations
540 declare -i gen_count=$generations+2
541 to_delete=$(ls -1t "$sys_root" 2>/dev/null | tail -n+$gen_count | sort)
542 if [ -n "$to_delete" -a $ok -eq 1 ]; then
543 [ "$DRYRUN" -eq 0 ] \
544 && echo "Deleting old backup generations (keep $generations) ..." \
545 || echo " *** Trial run, not deleting old generations:"
546 for delete in $to_delete; do
547 dir="$sys_root/$delete"
548 if [ ! -e "$dir/.stamp" ]; then
549 echo "Not deleting \"$dir\", not a backup directory!?"
552 last=$(stat "$dir/.stamp" 2>/dev/null | grep "^Modify: " \
553 | cut -d':' -f2- | cut -d. -f1)
554 echo "Removing backup from" $last "..."
555 if [ "$DRYRUN" -eq 0 ]; then
556 DeleteSubvolume "$dir"
558 echo "Failed to delete \"$dir\"!"
561 echo -n "Clean up finished: "; date
562 elif [ -n "$to_delete" ]; then
563 echo "There have been errors, not cleaning up old generations!"
565 echo "Nothing to clean up."
569 destinations="$destinations $target"
575 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
576 if [ "$DRYRUN" -eq 0 -a -n "$paths" ]; then
583 echo -n "Done: "; date
585 [ $count_all -eq 1 ] && s="" || s="s"
586 echo " - $count_all job$s defined,"
587 [ $count_started -eq 1 ] && s="" || s="s"
588 echo " - $count_started job$s started,"
589 echo " - $count_ok done without errors."
592 if [ $count_started -ne $count_ok ]; then
593 echo "-----> THERE HAVE BEEN ERRORS! <-----"