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 echo "CreateSubvolume: Incompatible FS type \"$fs\" on \"$dir\"!"
139 dir=$(dirname "source")
140 fs=$(GetFS "$source")
143 btrfs subvolume snapshot "$source" "$snapshot" >/dev/null || return 1
146 echo "CloneSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
157 fs=$(GetFS "$source")
160 mv "$source" "$target" || return 1
163 echo "RenameSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
173 fs=$(GetFS "$source")
176 btrfs subvolume delete "$volume" >/dev/null || return 1
179 echo "DeleteSubvolume: Incompatible FS type \"$fs\" on \"$volume\"!"
185 while [ $# -gt 0 ]; do
201 trap GotSignal SIGINT
203 echo -n "Started: "; date
205 for conf in "/etc/backup-script.conf" "${conf_d}/backup-script.conf"; do
206 if [ -r "$conf" ]; then
207 echo "Reading configuration: \"$conf\" ..."
213 if [ $# -ge 1 ]; then
215 if [ ! -r "${conf_d}/$s" ]; then
216 echo "$NAME: Can' read \"${conf_d}/$s\"!"
219 sys="$sys ${conf_d}/$s"
225 # check and create PID file
226 if [ -e "$PIDFILE" ]; then
227 echo "Lockfile \"$PIDFILE\" already exists."
228 echo "Is an other instance still running?"
230 echo -n "Aborted: "; date
234 touch "$PIDFILE" 2>/dev/null
235 if [ $? -ne 0 ]; then
236 echo "Warning: can't create PID file \"$PIDFILE\"!"
239 echo "$$" >>"$PIDFILE"
242 if [ -n "$pre_exec" ]; then
243 echo "Executing \"$pre_exec\" ..."
245 if [ $? -ne 0 ]; then
246 echo "Error: pre-exec command failed!"; echo
248 echo "Aborting backup."; echo
256 [ -r "$f" -a -f "$f" ] || continue
258 fname=$(basename "$f")
260 "backup-script.conf"|*.sh)
265 # Set global defaults
268 source_root="$default_source_root"
269 target="$default_target"
270 ssh_args_add="$default_ssh_args_add"
271 rsync_args_add="$default_rsync_args_add"
272 exclude_args_add="$default_exclude_args_add"
273 compress="$default_compress"
275 local="$default_local"
276 generations="$default_generations"
277 job_pre_exec="$default_job_pre_exec"
278 job_post_exec="$default_job_post_exec"
280 # Compatibility with backup-pull(1) script: Save global values ...
281 pre_exec_saved="$pre_exec"
282 post_exec_saved="$post_exec"
284 # Compatibility with backup-pull(1) script: Set defaults
290 # Read in system configuration file
293 # Compatibility with backup-pull(1) script: Fix up configuration
294 [ "$system" = "$fname" -a -n "$host" ] \
296 [ "$source_root" = "$default_source_root" -a -n "$source" ] \
297 && source_root="$source"
298 [ -z "$job_pre_exec" -a -n "$pre_exec" ] \
299 && job_pre_exec="$pre_exec"
300 [ -z "$job_post_exec" -a -n "$post_exec" ] \
301 && job_post_exec="$post_exec"
303 # Compatibility with backup-pull(1) script: Restore global values ...
304 pre_exec="$pre_exec_saved"
305 post_exec="$post_exec_saved"
307 # Validate configuration
308 [ "$system" = "localhost" -o "$system" = "127.0.0.1" ] && local=1
310 [ "$system" = "$fname" ] \
311 && systxt="\"$system\"" \
312 || systxt="\"$fname\" [\"$system\"]"
314 && echo "Working on $systxt ..." \
315 || echo "Working on $systxt (local system) ..."
317 count_all=$count_all+1
319 # Check target directory
320 if [ -z "$target" ]; then
321 echo "No target directory specified for \"$system\"!? Skipped!"
324 if [ ! -d "$target" ]; then
325 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
329 sys_target="$target/$fname"
330 sys_root="$sys_target"
331 if [ "$DRYRUN" -eq 0 ]; then
332 mkdir -p "$sys_target" >/dev/null 2>&1
333 if [ $? -ne 0 ]; then
334 echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
339 if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
340 # Check if system is alive
341 ping -c 1 "$system" >/dev/null 2>&1
342 if [ $? -ne 0 ]; then
343 echo "Host \"$system\" seems not to be alive!? Skipped."
346 echo "OK, host \"$system\" seems to be alive."
349 if [ $generations -gt 0 ]; then
350 # Make sure no old backup is stored in system directory
351 if [ -e "$sys_target/.stamp" ]; then
352 # There seems to be a genearation-less backup in the
354 echo "Target directory \"$sys_target\" seems to be unclean!? \"$system\" skipped!"
358 # Search directory of last generation, if any
359 last=$(ls -1d "$sys_target"/[0-9]* 2>/dev/null | sort -r | head -n1)
360 if [ -n "$last" ]; then
361 if [ ! -d "$last" ]; then
362 echo "Last snapshot \"$last\" seems not to be a directory!? \"$system\" skipped!"
366 sys_target="$sys_target/$(date +%Y%m%d-%H%M%S)"
367 snapshot="$sys_target"
369 if [ -n "$last" -a ! -e "$last/.stamp" ]; then
370 # Old backup directory without "stamp file", continue
371 echo "Found incomplete snapshot in \"$last\", reusing and renaming it ..."
372 RenameSubvolume "$last" "$sys_target"
373 if [ $? -ne 0 ]; then
374 echo "Failed to rename last snapshot \"$last\" to \"$sys_target\"!? \"$system\" skipped!"
377 elif [ -n "$last" ]; then
378 # Old backup directory found, create new snapshot
379 echo "Found last snapshot in \"$last\"."
380 if [ "$DRYRUN" -eq 0 ]; then
381 CloneSubvolume "$last" "$sys_target" "$snapshot"; r=$?
382 if [ $r -ne 0 ]; then
383 echo "Can't create snapshot \"$snapshot\" of \"$last\", code $r!? \"$system\" skipped!"
386 echo "Created new snapshot in \"$snapshot\"."
388 echo " *** Trial run, not creating new snapshot in \"$sys_target\"!"
391 # No old backup found, create new subvolume
392 if [ "$DRYRUN" -eq 0 ]; then
393 CreateSubvolume "$sys_target"; r=$?
394 if [ $r -ne 0 ]; then
395 echo "Can't create subvolume \"$sys_target\", code $r!? \"$system\" skipped!"
398 echo "Created new subvolume in \"$sys_target\"."
400 echo " *** Trial run, not creating new subvolume \"$sys_target\"!"
406 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
408 # execute job "pre-exec" command, if any
409 if [ -n "$job_pre_exec" ]; then
410 ExecJob pre "$job_pre_exec" ; ret=$?
411 if [ $ret -ne 0 ]; then
412 [ $ret -ne 99 ] && count_started=$count_started+1
413 echo "Pre-exec command failed, \"$system\" skipped!"
418 # prepare (remote) command ...
419 cmd="rsync --archive"
420 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
421 [ "$local" -eq 0 ] && cmd="$cmd --rsh=\"$ssh_cmd\""
422 cmd="$cmd --delete --delete-excluded --sparse"
423 [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
424 if [ "$source_root" = "$default_source_root" ]; then
425 cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
426 cmd="$cmd --exclude=/run --exclude=/tmp --exclude=/var/tmp"
427 cmd="$cmd --exclude=/media --exclude=/mnt --exclude=/net"
428 cmd="$cmd --exclude=/var/cache/apt --exclude=/var/log"
430 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
431 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
434 && cmd="$cmd ${user}@${system}:$source_root $sys_target/" \
435 || cmd="$cmd $source_root $sys_target/"
437 echo "Backing up to \"$sys_target\" ..."
438 echo -n "Start date: "; date
440 count_started=$count_started+1
443 if [ "$DRYRUN" -eq 0 ]; then
444 rm -f "$sys_target/.stamp"
445 $SHELL -c "$cmd"; ret=$?
446 echo "code=$ret" >"$sys_target/.stamp"
448 echo " *** Trial run, not executing synchronization command!"
452 if [ $ret -eq 20 ]; then
453 echo "Backup of \"$system\" interrupted. Aborting ..."
458 echo -n "End date: "; date
459 if [ $ret -eq 0 -o $ret -eq 24 ]; then
460 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
462 echo "System \"$system\" completed with status $ret, OK."
463 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
466 echo "System \"$system\" completed with ERRORS, code $ret!"
469 # execute job "post-exec" command, if any
470 if [ -n "$job_post_exec" ]; then
471 ExecJob post "$job_post_exec"
474 if [ $generations -gt 0 ]; then
475 # Update "latest" symlink
476 if [ "$DRYRUN" -eq 0 ]; then
477 rm -f "$sys_root/latest"
478 ln -s "$sys_target" "$sys_root/latest"
480 # Clean up old generations
481 declare -i gen_count=$generations+2
482 to_delete=$(ls -1t "$sys_root" 2>/dev/null | tail -n+$gen_count | sort)
483 if [ -n "$to_delete" -a $ok -eq 1 ]; then
484 [ "$DRYRUN" -eq 0 ] \
485 && echo "Deleting old backup generations (keep $generations) ..." \
486 || echo " *** Trial run, not deleting old generations:"
487 for delete in $to_delete; do
488 dir="$sys_root/$delete"
489 if [ ! -e "$dir/.stamp" ]; then
490 echo "Not deleting \"$dir\", not a backup directory!?"
493 last=$(stat "$dir/.stamp" 2>/dev/null | grep "^Modify: " \
494 | cut -d':' -f2- | cut -d. -f1)
495 echo "Removing backup from" $last "..."
496 if [ "$DRYRUN" -eq 0 ]; then
497 DeleteSubvolume "$dir"
499 echo "Failed to delete \"$dir\"!"
502 echo -n "Clean up finished: "; date
503 elif [ -n "$to_delete" ]; then
504 echo "There have been errors, not cleaning up old generations!"
506 echo "Nothing to clean up."
510 destinations="$destinations $target"
516 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
517 if [ "$DRYRUN" -eq 0 -a -n "$paths" ]; then
524 echo -n "Done: "; date
526 [ $count_all -eq 1 ] && s="" || s="s"
527 echo " - $count_all job$s defined,"
528 [ $count_started -eq 1 ] && s="" || s="s"
529 echo " - $count_started job$s started,"
530 echo " - $count_ok done without errors."
533 if [ $count_started -ne $count_ok ]; then
534 echo "-----> THERE HAVE BEEN ERRORS! <-----"