#!/bin/bash
#
# backup-script system for cloning systems using rsync
-# Copyright (c)2008-2014 Alexander Barton, alex@barton.de
+# Copyright (c)2008-2015 Alexander Barton <alex@barton.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
default_ping=1
default_local=0
default_generations=0
+default_job_pre_exec=""
+default_job_post_exec=""
Usage() {
echo "Usage: $NAME [<options>] [<system> [<system> [...]]]"
exit 9
}
+ExecJob() {
+ what="$1"
+ cmd="$2"
+
+ echo "Running job ${what}-exec command ..."
+ [ "$local" -eq 0 ] \
+ && cmd="$ssh_cmd ${user}@${system} $cmd"
+ echo -n "Start date (${what}-exec): "; date
+ echo "$cmd"
+ if [ "$DRYRUN" -eq 0 ]; then
+ $SHELL -c "$cmd"; ret=$?
+ else
+ echo " *** Trial run, not executing ${what}-exec command!"
+ ret=0
+ fi
+ [ $ret -eq 0 ] \
+ && echo "The ${what}-exec command completed with status 0, OK." \
+ || echo "The ${what}-exec command completed with ERRORS, code $ret!"
+ return $ret
+}
+
while [ $# -gt 0 ]; do
case "$1" in
"-n"|"--dry-run")
ping="$default_ping"
local="$default_local"
generations="$default_generations"
+ job_pre_exec="$default_job_pre_exec"
+ job_post_exec="$default_job_post_exec"
# Read in system configuration file
source "$f"
fi
sys_target="$target/$fname"
+ sys_root="$sys_target"
if [ "$DRYRUN" -eq 0 ]; then
mkdir -p "$sys_target" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
- echo continue
+ echo; continue
fi
fi
ssh_cmd="ssh"
[ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
+ # execute job "pre-exec" command, if any
+ if [ -n "$job_pre_exec" ]; then
+ ExecJob pre "$job_pre_exec" ; ret=$?
+ if [ $ret -ne 0 ]; then
+ [ $ret -ne 99 ] && count_started=$count_started+1
+ echo "Pre-exec command failed, \"$system\" skipped!"
+ echo; continue
+ fi
+ fi
+
+ # prepare (remote) command ...
cmd="rsync --archive"
[ "$compress" -ne 0 ] && cmd="$cmd --compress"
cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse"
echo "System \"$system\" completed with ERRORS, code $ret!"
fi
- # Clean up old generations
+ # execute job "post-exec" command, if any
+ if [ -n "$job_post_exec" ]; then
+ ExecJob post "$job_post_exec"
+ fi
+
if [ $generations -gt 0 ]; then
- sys_target="$target/$fname"
- to_delete=`ls -1t "$sys_target" 2>/dev/null | tail -n+$generations | sort`
+ # Clean up old generations
+ to_delete=`ls -1t "$sys_root" 2>/dev/null | tail -n+$generations | sort`
if [ -n "$to_delete" -a $ok -eq 1 ]; then
[ "$DRYRUN" -eq 0 ] \
&& echo "Deleting old backup generations:" \
|| echo " *** Trial run, not deleting old generations:"
for delete in $to_delete; do
- dir="$sys_target/$delete"
+ dir="$sys_root/$delete"
if [ ! -e "$dir/.stamp" ]; then
echo "Not deleting \"$dir\", not a backup directory!?"
continue