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"
34 default_ssh_args_add=""
35 default_rsync_args_add=""
36 default_exclude_args_add=""
41 default_job_pre_exec=""
42 default_job_post_exec=""
45 echo "Usage: $NAME [<options>] [<system> [<system> [...]]]"
47 echo " -p, --progress Show progress, see rsync(1)."
48 echo " -n, --dry-run Test run only, don't copy any data."
50 echo "When no <system> is given, all defined systems are used."
56 if [ -n "$post_exec" ]; then
57 echo "Executing \"$post_exec\" ..."
60 echo "Warning: post-exec command failed!"
69 echo "--> Got break signal, cleaning up & aborting ..."
72 echo -n "Aborted: "; date
81 echo "Running job ${what}-exec command ..."
83 && cmd="$ssh_cmd ${user}@${system} $cmd"
84 echo -n "Start date (${what}-exec): "; date
86 if [ "$DRYRUN" -eq 0 ]; then
87 $SHELL -c "$cmd"; ret=$?
89 echo " *** Trial run, not executing ${what}-exec command!"
93 && echo "The ${what}-exec command completed with status 0, OK." \
94 || echo "The ${what}-exec command completed with ERRORS, code $ret!"
100 while [ -n "$dir" ]; do
101 findmnt -fn -o FSTYPE --raw "$dir" 2>/dev/null; r=$?
102 if [ $r -eq 0 ]; then
104 elif [ $r -eq 127 ]; then
108 dir=$(dirname "$dir") || return 1
115 dir=$(dirname "$volume")
119 btrfs subvolume create "$volume" >/dev/null || return 1
122 echo "CreateSubvolume: Incompatible FS type \"$fs\" on \"$dir\"!"
132 dir=$(dirname "source")
133 fs=$(GetFS "$source")
136 btrfs subvolume snapshot "$source" "$volume" >/dev/null || return 1
139 echo "CloneSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
149 fs=$(GetFS "$source")
152 mv "$source" "$target" || return 1
155 echo "RenameSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
164 fs=$(GetFS "$source")
167 btrfs subvolume delete "$volume" >/dev/null || return 1
170 echo "DeleteSubvolume: Incompatible FS type \"$fs\" on \"$volume\"!"
176 while [ $# -gt 0 ]; do
192 trap GotSignal SIGINT
194 echo -n "Started: "; date
196 for conf in "/etc/backup-script.conf" "${conf_d}/backup-script.conf"; do
197 if [ -r "$conf" ]; then
198 echo "Reading configuration: \"$conf\" ..."
204 if [ $# -ge 1 ]; then
206 if [ ! -r "${conf_d}/$s" ]; then
207 echo "$NAME: Can' read \"${conf_d}/$s\"!"
210 sys="$sys ${conf_d}/$s"
216 # check and create PID file
217 if [ -e "$PIDFILE" ]; then
218 echo "Lockfile \"$PIDFILE\" already exists."
219 echo "Is an other instance still running?"
221 echo -n "Aborted: "; date
225 touch "$PIDFILE" 2>/dev/null
226 if [ $? -ne 0 ]; then
227 echo "Warning: can't create PID file \"$PIDFILE\"!"
230 echo "$$" >>"$PIDFILE"
233 if [ -n "$pre_exec" ]; then
234 echo "Executing \"$pre_exec\" ..."
236 if [ $? -ne 0 ]; then
237 echo "Error: pre-exec command failed!"; echo
239 echo "Aborting backup."; echo
247 [ -r "$f" -a -f "$f" ] || continue
249 fname=$(basename "$f")
251 "backup-script.conf"|*.sh)
256 # Set global defaults
259 target="$default_target"
260 ssh_args_add="$default_ssh_args_add"
261 rsync_args_add="$default_rsync_args_add"
262 exclude_args_add="$default_exclude_args_add"
263 compress="$default_compress"
265 local="$default_local"
266 generations="$default_generations"
267 job_pre_exec="$default_job_pre_exec"
268 job_post_exec="$default_job_post_exec"
270 # Read in system configuration file
273 # Validate configuration
274 [ "$system" = "localhost" -o "$system" = "127.0.0.1" ] && local=1
276 [ "$system" = "$fname" ] \
277 && systxt="\"$system\"" \
278 || systxt="\"$fname\" [\"$system\"]"
280 && echo "Working on $systxt ..." \
281 || echo "Working on $systxt (local system) ..."
283 count_all=$count_all+1
285 # Check target directory
286 if [ -z "$target" ]; then
287 echo "No target directory specified for \"$system\"!? Skipped!"
290 if [ ! -d "$target" ]; then
291 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
295 sys_target="$target/$fname"
296 sys_root="$sys_target"
297 if [ "$DRYRUN" -eq 0 ]; then
298 mkdir -p "$sys_target" >/dev/null 2>&1
299 if [ $? -ne 0 ]; then
300 echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
305 if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
306 # Check if system is alive
307 ping -c 1 "$system" >/dev/null 2>&1
308 if [ $? -ne 0 ]; then
309 echo "Host \"$system\" seems not to be alive!? Skipped."
312 echo "OK, host \"$system\" seems to be alive."
315 if [ $generations -gt 0 ]; then
316 # Make sure no old backup is stored in system directory
317 if [ -e "$sys_target/.stamp" ]; then
318 # There seems to be a genearation-less backup in the
320 echo "Target directory \"$sys_target\" seems to be unclean!? \"$system\" skipped!"
324 # Search directory of last generation, if any
325 last=$(ls -1d "$sys_target"/[0-9]* 2>/dev/null | sort -r | head -n1)
326 if [ -n "$last" ]; then
327 if [ ! -d "$last" ]; then
328 echo "Last snapshot \"$last\" seems not to be a directory!? \"$system\" skipped!"
332 sys_target="$sys_target/$(date +%Y%m%d-%H%M%S)"
334 if [ -n "$last" -a ! -e "$last/.stamp" ]; then
335 # Old backup directory without "stamp file", continue
336 echo "Found incomplete snapshot in \"$last\", reusing and renaming it ..."
337 RenameSubvolume "$last" "$sys_target"
338 if [ $? -ne 0 ]; then
339 echo "Failed to rename last snapshot \"$last\" to \"$sys_target\"!? \"$system\" skipped!"
342 elif [ -n "$last" ]; then
343 # Old backup directory found, create new snapshot
344 echo "Found last snapshot in \"$last\"."
345 if [ "$DRYRUN" -eq 0 ]; then
346 CloneSubvolume "$last" "$sys_target"; r=$?
347 if [ $r -ne 0 ]; then
348 echo "Can't create snapshot \"$sys_target\" of \"$last\", code $r!? \"$system\" skipped!"
351 echo "Created new snapshot in \"$sys_target\"."
353 echo " *** Trial run, not creating new snapshot in \"$sys_target\"!"
356 # No old backup found, create new subvolume
357 if [ "$DRYRUN" -eq 0 ]; then
358 CreateSubvolume "$sys_target"; r=$?
359 if [ $r -ne 0 ]; then
360 echo "Can't create subvolume \"$sys_target\", code $r!? \"$system\" skipped!"
363 echo "Created new subvolume in \"$sys_target\"."
365 echo " *** Trial run, not creating new subvolume \"$sys_target\"!"
371 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
373 # execute job "pre-exec" command, if any
374 if [ -n "$job_pre_exec" ]; then
375 ExecJob pre "$job_pre_exec" ; ret=$?
376 if [ $ret -ne 0 ]; then
377 [ $ret -ne 99 ] && count_started=$count_started+1
378 echo "Pre-exec command failed, \"$system\" skipped!"
383 # prepare (remote) command ...
384 cmd="rsync --archive"
385 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
386 cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse"
387 [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
388 cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
389 cmd="$cmd --exclude=/run --exclude=/tmp --exclude=/var/tmp"
390 cmd="$cmd --exclude=/media --exclude=/mnt --exclude=/net"
391 cmd="$cmd --exclude=/var/cache/apt --exclude=/var/log"
392 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
393 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
396 && cmd="$cmd ${user}@${system}:/ $sys_target/" \
397 || cmd="$cmd / $sys_target/"
399 echo "Backing up to \"$sys_target\" ..."
400 echo -n "Start date: "; date
402 count_started=$count_started+1
405 if [ "$DRYRUN" -eq 0 ]; then
406 rm -f "$sys_target/.stamp"
407 $SHELL -c "$cmd"; ret=$?
408 echo "code=$ret" >"$sys_target/.stamp"
410 echo " *** Trial run, not executing synchronization command!"
414 if [ $ret -eq 20 ]; then
415 echo "Backup of \"$system\" interrupted. Aborting ..."
420 echo -n "End date: "; date
421 if [ $ret -eq 0 -o $ret -eq 24 ]; then
422 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
424 echo "System \"$system\" completed with status $ret, OK."
425 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
428 echo "System \"$system\" completed with ERRORS, code $ret!"
431 # execute job "post-exec" command, if any
432 if [ -n "$job_post_exec" ]; then
433 ExecJob post "$job_post_exec"
436 if [ $generations -gt 0 ]; then
437 # Update "latest" symlink
438 if [ "$DRYRUN" -eq 0 ]; then
439 rm -f "$sys_root/latest"
440 ln -s "$sys_target" "$sys_root/latest"
442 # Clean up old generations
443 to_delete=$(ls -1t "$sys_root" 2>/dev/null | tail -n+$generations | sort)
444 if [ -n "$to_delete" -a $ok -eq 1 ]; then
445 [ "$DRYRUN" -eq 0 ] \
446 && echo "Deleting old backup generations:" \
447 || echo " *** Trial run, not deleting old generations:"
448 for delete in $to_delete; do
449 dir="$sys_root/$delete"
450 if [ ! -e "$dir/.stamp" ]; then
451 echo "Not deleting \"$dir\", not a backup directory!?"
454 last=$(stat "$dir/.stamp" 2>/dev/null | grep "^Modify: " \
455 | cut -d':' -f2- | cut -d. -f1)
456 echo "Removing backup from" $last "..."
457 if [ "$DRYRUN" -eq 0 ]; then
458 DeleteSubvolume "$dir"
460 echo "Failed to delete \"$dir\"!"
463 echo -n "Clean up finished: "; date
464 elif [ -n "$to_delete" ]; then
465 echo "There have been errors, not cleaning up old generations!"
467 echo "Nothing to clean up."
471 destinations="$destinations $target"
477 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
478 if [ "$DRYRUN" -eq 0 -a -n "$paths" ]; then
485 echo -n "Done: "; date
487 [ $count_all -eq 1 ] && s="" || s="s"
488 echo " - $count_all job$s defined,"
489 [ $count_started -eq 1 ] && s="" || s="s"
490 echo " - $count_started job$s started,"
491 echo " - $count_ok done without errors."
494 if [ $count_started -ne $count_ok ]; then
495 echo "-----> THERE HAVE BEEN ERRORS! <-----"