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."
53 echo "Configuration file is \"$conf\","
54 echo "using \"$conf_d\" as configuration directory."
60 if [ -n "$post_exec" ]; then
61 echo "Executing \"$post_exec\" ..."
64 echo "Warning: post-exec command failed!"
73 echo "--> Got break signal, cleaning up & aborting ..."
76 echo -n "Aborted: "; date
86 echo "Running job ${what}-exec command ..."
88 && cmd="$ssh_cmd ${user}@${system} $cmd"
89 echo -n "Start date (${what}-exec): "; date
91 if [ "$DRYRUN" -eq 0 ]; then
92 $SHELL -c "$cmd"; local ret=$?
94 echo " *** Trial run, not executing ${what}-exec command!"
98 && echo "The ${what}-exec command completed with status 0, OK." \
99 || echo "The ${what}-exec command completed with ERRORS, code $ret!"
106 while [ -n "$dir" ]; do
107 findmnt -fn -o FSTYPE --raw "$dir" 2>/dev/null; local r=$?
108 if [ $r -eq 0 ]; then
110 elif [ $r -eq 127 ]; then
114 dir=$(dirname "$dir") || return 1
123 dir=$(dirname "$volume")
127 btrfs subvolume create "$volume" >/dev/null || return 1
130 zfs create "$(echo "$volume" | cut -c2-)" || return 1
133 echo "CreateSubvolume: Incompatible FS type \"$fs\" on \"$dir\"!"
147 dir=$(dirname "source")
148 fs=$(GetFS "$source")
151 btrfs subvolume snapshot "$source" "$snapshot" >/dev/null || return 1
154 zfs snapshot "$snapshot" || return 1
155 link_name="$(echo "$snapshot" | cut -d@ -f2-)"
157 "$volume/.zfs/snapshot/$link_name" \
158 "$(dirname "$volume")/$link_name"
161 echo "CloneSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
172 fs=$(GetFS "$source")
175 mv "$source" "$target" || return 1
179 "$(echo "$source" | cut -c2-)" \
180 "$(echo "$target" | cut -c2-)" \
184 echo "RenameSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
196 fs=$(GetFS "$volume")
199 btrfs subvolume delete "$volume" >/dev/null || return 1
202 id="$(basename "$volume")"
203 if [ -h "$volume" ]; then
204 snapshot="$(dirname "$volume")/current@$id"
208 zfs destroy -r "$(echo "$snapshot" | cut -c2-)" >/dev/null || return 1
209 [ -h "$volume" ] && rm "$volume"
212 echo "DeleteSubvolume: Incompatible FS type \"$fs\" on \"$volume\"!"
218 Initialize_Last_SysTarget_Snapshot() {
223 fs=$(GetFS "$sys_target")
226 # Search directory of last generation, if any
227 last=$(ls -1d "$sys_target"/[0-9]* 2>/dev/null | sort -r | head -n1)
228 if [ -n "$last" ]; then
229 if [ ! -d "$last" ]; then
230 echo "Last snapshot \"$last\" seems not to be a directory!? \"$system\" skipped!"
235 sys_target="$sys_target/$(date +%Y%m%d-%H%M%S)"
236 snapshot="$sys_target"
239 # On ZFS, the last generation is always named "current"
240 if [ -e "$sys_target/current" ]; then
241 last="$sys_target/current"
242 if [ "$(uname)" = "Linux" ]; then
243 date=$(LC_ALL=C stat "$1" | grep "^Modify: " \
244 | cut -d':' -f2- | cut -d. -f1)
246 date=$(LC_ALL=C stat -f "%Sc" "$1")
248 date=$(echo "$date" | sed -e's/^ //g' -e 's/[-:]//g' -e 's/ /-/g')
252 date="$(date +%Y%m%d-%H%M%S)"
254 snapshot="$(echo "$sys_target/current" | cut -c2-)@$date"
255 sys_target="$sys_target/current"
258 echo "Initialize_Last_SysTarget_Snapshot: Incompatible FS type \"$fs\" on \"$sys_target\"!"
264 # Search configuration file (last one is used as default!)
266 "/usr/local/etc/backup-script.conf" \
267 "${conf_d}/backup-script.conf" \
268 "/etc/backup-script.conf" \
270 [ -r "$conf" ] && break
273 while [ $# -gt 0 ]; do
289 trap GotSignal SIGINT
291 echo -n "Started: "; date
293 # Read in configuration file
294 if [ -r "$conf" ]; then
295 echo "Reading configuration: \"$conf\" ..."
298 echo "No configuration file found, using defaults."
302 if [ $# -ge 1 ]; then
304 if [ ! -r "${conf_d}/$s" ]; then
305 echo "$NAME: Can' read \"${conf_d}/$s\"!"
308 sys="$sys ${conf_d}/$s"
314 # check and create PID file
315 if [ -e "$PIDFILE" ]; then
316 echo "Lockfile \"$PIDFILE\" already exists."
317 echo "Is an other instance still running?"
319 echo -n "Aborted: "; date
323 touch "$PIDFILE" 2>/dev/null
324 if [ $? -ne 0 ]; then
325 echo "Warning: can't create PID file \"$PIDFILE\"!"
328 echo "$$" >>"$PIDFILE"
331 if [ -n "$pre_exec" ]; then
332 echo "Executing \"$pre_exec\" ..."
334 if [ $? -ne 0 ]; then
335 echo "Error: pre-exec command failed!"; echo
337 echo "Aborting backup."; echo
345 [ -r "$f" -a -f "$f" ] || continue
347 fname=$(basename "$f")
349 "backup-script.conf"|*.sh)
354 # Set global defaults
357 source_root="$default_source_root"
358 target="$default_target"
359 ssh_args_add="$default_ssh_args_add"
360 rsync_args_add="$default_rsync_args_add"
361 exclude_args_add="$default_exclude_args_add"
362 compress="$default_compress"
364 local="$default_local"
365 generations="$default_generations"
366 job_pre_exec="$default_job_pre_exec"
367 job_post_exec="$default_job_post_exec"
369 # Compatibility with backup-pull(1) script: Save global values ...
370 pre_exec_saved="$pre_exec"
371 post_exec_saved="$post_exec"
373 # Compatibility with backup-pull(1) script: Set defaults
379 # Read in system configuration file
382 # Compatibility with backup-pull(1) script: Fix up configuration
383 [ "$system" = "$fname" -a -n "$host" ] \
385 [ "$source_root" = "$default_source_root" -a -n "$source" ] \
386 && source_root="$source"
387 [ -z "$job_pre_exec" -a -n "$pre_exec" ] \
388 && job_pre_exec="$pre_exec"
389 [ -z "$job_post_exec" -a -n "$post_exec" ] \
390 && job_post_exec="$post_exec"
392 # Compatibility with backup-pull(1) script: Restore global values ...
393 pre_exec="$pre_exec_saved"
394 post_exec="$post_exec_saved"
396 # Validate configuration
397 [ "$system" = "localhost" -o "$system" = "127.0.0.1" ] && local=1
399 # Make sure "source" ends with a slash ("/")
407 # Make sure "target" DOESN'T end with a slash ("/")
410 target=$( echo "$target" | sed -e 's/\/$//g' )
414 [ "$system" = "$fname" ] \
415 && systxt="\"$system\"" \
416 || systxt="\"$fname\" [\"$system\"]"
418 && echo "Working on $systxt ..." \
419 || echo "Working on $systxt (local system) ..."
421 count_all=$count_all+1
423 # Check target directory
424 if [ -z "$target" ]; then
425 echo "No target directory specified for \"$system\"!? Skipped!"
428 if [ ! -d "$target" ]; then
429 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
433 sys_target="$target/$fname"
434 sys_root="$sys_target"
435 if [ "$DRYRUN" -eq 0 -a ! -e "$sys_target" ]; then
436 if [ $generations -gt 0 ]; then
437 CreateSubvolume "$sys_target"
439 mkdir -p "$sys_target"
441 if [ $? -ne 0 ]; then
442 echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
447 if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
448 # Check if system is alive
449 ping -c 1 "$system" >/dev/null 2>&1
450 if [ $? -ne 0 ]; then
451 echo "Host \"$system\" seems not to be alive!? Skipped."
454 echo "OK, host \"$system\" seems to be alive."
457 if [ $generations -gt 0 ]; then
458 # Make sure no old backup is stored in system directory
459 if [ -e "$sys_target/.stamp" ]; then
460 # There seems to be a genearation-less backup in the
462 echo "Target directory \"$sys_target\" seems to be unclean!? \"$system\" skipped!"
466 Initialize_Last_SysTarget_Snapshot "$sys_target" || continue
468 if [ -n "$last" -a ! -e "$last/.stamp" ]; then
469 # Old backup directory without "stamp file", continue
470 echo "Found incomplete snapshot in \"$last\", reusing and renaming it ..."
471 RenameSubvolume "$last" "$sys_target"
472 if [ $? -ne 0 ]; then
473 echo "Failed to rename last snapshot \"$last\" to \"$sys_target\"!? \"$system\" skipped!"
476 elif [ -n "$last" ]; then
477 # Old backup directory found, create new snapshot
478 echo "Found last snapshot in \"$last\"."
479 if [ "$DRYRUN" -eq 0 ]; then
480 CloneSubvolume "$last" "$sys_target" "$snapshot"; r=$?
481 if [ $r -ne 0 ]; then
482 echo "Can't create snapshot \"$snapshot\" of \"$last\", code $r!? \"$system\" skipped!"
485 echo "Created new snapshot in \"$snapshot\"."
487 echo " *** Trial run, not creating new snapshot in \"$snapshot\"!"
490 # No old backup found, create new subvolume
491 if [ "$DRYRUN" -eq 0 ]; then
492 CreateSubvolume "$sys_target"; r=$?
493 if [ $r -ne 0 ]; then
494 echo "Can't create subvolume \"$sys_target\", code $r!? \"$system\" skipped!"
497 echo "Created new subvolume in \"$sys_target\"."
499 echo " *** Trial run, not creating new subvolume \"$sys_target\"!"
505 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
507 # execute job "pre-exec" command, if any
508 if [ -n "$job_pre_exec" ]; then
509 ExecJob pre "$job_pre_exec" ; ret=$?
510 if [ $ret -ne 0 ]; then
511 [ $ret -ne 99 ] && count_started=$count_started+1
512 echo "Pre-exec command failed, \"$system\" skipped!"
517 # prepare (remote) command ...
518 cmd="rsync --archive"
519 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
520 [ "$local" -eq 0 ] && cmd="$cmd --rsh=\"$ssh_cmd\""
521 cmd="$cmd --delete --delete-excluded --sparse"
522 [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
523 if [ "$source_root" = "$default_source_root" ]; then
524 cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
525 cmd="$cmd --exclude=/run --exclude=/tmp --exclude=/var/tmp"
526 cmd="$cmd --exclude=/media --exclude=/mnt --exclude=/net"
527 cmd="$cmd --exclude=/var/cache/apt --exclude=/var/log"
529 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
530 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
533 && cmd="$cmd ${user}@${system}:$source_root $sys_target/" \
534 || cmd="$cmd $source_root $sys_target/"
536 echo "Backing up to \"$sys_target\" ..."
537 echo -n "Start date: "; date
539 count_started=$count_started+1
542 if [ "$DRYRUN" -eq 0 ]; then
543 rm -f "$sys_target/.stamp"
544 $SHELL -c "$cmd"; ret=$?
545 echo "code=$ret" >"$sys_target/.stamp"
547 echo " *** Trial run, not executing synchronization command!"
551 if [ $ret -eq 20 ]; then
552 echo "Backup of \"$system\" interrupted. Aborting ..."
557 echo -n "End date: "; date
558 if [ $ret -eq 0 -o $ret -eq 24 ]; then
559 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
561 echo "System \"$system\" completed with status $ret, OK."
562 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
565 echo "System \"$system\" completed with ERRORS, code $ret!"
568 # execute job "post-exec" command, if any
569 if [ -n "$job_post_exec" ]; then
570 ExecJob post "$job_post_exec"
573 if [ $generations -gt 0 ]; then
574 # Update "latest" symlink
575 if [ "$DRYRUN" -eq 0 ]; then
576 rm -f "$sys_root/latest"
577 ln -s "$sys_target" "$sys_root/latest"
579 # Clean up old generations
580 declare -i gen_count=$generations+2
581 to_delete=$(ls -1t "$sys_root" 2>/dev/null | tail -n+$gen_count | sort)
582 if [ -n "$to_delete" -a $ok -eq 1 ]; then
583 [ "$DRYRUN" -eq 0 ] \
584 && echo "Deleting old backup generations (keep $generations) ..." \
585 || echo " *** Trial run, not deleting old generations:"
586 for delete in $to_delete; do
587 dir="$sys_root/$delete"
588 if [ ! -e "$dir/.stamp" ]; then
589 echo "Not deleting \"$dir\", not a backup directory!?"
592 last=$(stat "$dir/.stamp" 2>/dev/null | grep "^Modify: " \
593 | cut -d':' -f2- | cut -d. -f1)
594 echo "Removing backup from" $last "..."
595 if [ "$DRYRUN" -eq 0 ]; then
596 DeleteSubvolume "$dir"
598 echo "Failed to delete \"$dir\"!"
601 echo -n "Clean up finished: "; date
602 elif [ -n "$to_delete" ]; then
603 echo "There have been errors, not cleaning up old generations!"
605 echo "Nothing to clean up."
609 destinations="$destinations $target"
615 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
616 if [ "$DRYRUN" -eq 0 -a -n "$paths" ]; then
623 echo -n "Done: "; date
625 [ $count_all -eq 1 ] && s="" || s="s"
626 echo " - $count_all job$s defined,"
627 [ $count_started -eq 1 ] && s="" || s="s"
628 echo " - $count_started job$s started,"
629 echo " - $count_ok done without errors."
632 if [ $count_started -ne $count_ok ]; then
633 echo "-----> THERE HAVE BEEN ERRORS! <-----"