#!/bin/bash # # backup-script system for cloning systems using rsync # Copyright (c)2008-2013 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 # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # Please read the file COPYING, README and AUTHORS for more information. # NAME=`basename $0` CONF_D="/etc/backup-script.d" PIDFILE="/var/run/$NAME.pid" DRYRUN=0 VERBOSE=0 export LC_ALL=C declare -i count_all=0 declare -i count_started=0 declare -i count_ok=0 declare -i count_ok_vanished=0 destinations="" pre_exec="" post_exec="" default_target="" default_user="root" Usage() { echo "Usage: $NAME [] [ [ [...]]]" echo echo " -p, --progress Show progress, see rsync(1)." echo " -d, --dry-run Test run only, don't copy any data." echo echo "When no is given, all defined systems are used." echo exit 1 } Log() { logger -t "$NAME" "$*" } Message() { echo "$*" } MessageLog() { Log "$*" Message "$*" } CleanUp() { if [ -n "$post_exec" ]; then echo "Executing \"$post_exec\" ..." sh -c $post_exec if [ $? -ne 0 ]; then echo "Warning: post-exec command failed!" fi echo fi rm -f "$PIDFILE" } GotSignal() { echo echo "--> Got break signal, cleaning up & aborting ..." echo CleanUp echo -n "Aborted: "; date echo exit 9 } while [ $# -gt 0 ]; do case "$1" in "-n"|"--dry-run") DRYRUN=1; shift ;; "-p"|"--progress") VERBOSE=1; shift ;; "-"*) Usage ;; *) break esac done if [ $# -ge 1 ]; then for s in $@; do if [ ! -r "${CONF_D}/$s" ]; then echo "$NAME: Can' read \"${CONF_D}/$s\"!" exit 1 fi sys="$sys ${CONF_D}/$s" done else sys=${CONF_D}/* fi trap GotSignal SIGINT Log "Started ..." echo -n "Started: "; date echo [ -r "${CONF_D}/backup-script.conf" ] && source "${CONF_D}/backup-script.conf" # check and create PID file if [ -e "$PIDFILE" ]; then Log "Lockfile \"$PIDFILE\" already exists. Aborting!" echo "Lockfile \"$PIDFILE\" already exists." echo "Is an other instance still running?" echo echo -n "Aborted: "; date echo exit 3 fi touch "$PIDFILE" 2>/dev/null if [ $? -ne 0 ]; then echo "Warning: can't create PID file \"$PIDFILE\"!" echo else echo "$$" >>"$PIDFILE" fi if [ -n "$pre_exec" ]; then echo "Executing \"$pre_exec\" ..." sh -c $pre_exec if [ $? -ne 0 ]; then echo "Error: pre-exec command failed!"; echo CleanUp echo "Aborting backup."; echo exit 2 fi sleep 2 echo fi for f in $sys; do [ -r "$f" -a -f "$f" ] || continue system=`basename $f` user="$default_user" target="$default_target" ssh_args_add="" rsync_args_add="" compress=1 ping=1 local=0 case "$system" in "backup-script.conf"|*.sh) continue ;; esac # Read in configuration file source "$f" [ "$local" -eq 0 ] \ && MessageLog "Working on \"$system\" ..." \ || MessageLog "Working on \"$system\" (local system) ..." count_all=$count_all+1 # Check target directory if [ -z "$target" ]; then MessageLog "No target directory specified for \"$system\"!? Skipped!" echo; continue fi if [ ! -d "$target" ]; then MessageLog "Target \"$target\" is not a directory!? \"$system\" skipped!" echo; continue fi destdir="$target" target="$target/$system" [ "$DRYRUN" -gt 1 ] || mkdir -p "$target" if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then # Check if system is alive ping -c 1 "$system" >/dev/null 2>&1 if [ $? -ne 0 ]; then MessageLog "Host \"$system\" seems not to be alive!? Skipped." echo; continue fi Message "OK, host \"$system\" seems to be alive." fi ssh_cmd="ssh" [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add" cmd="rsync --archive" [ "$compress" -ne 0 ] && cmd="$cmd --compress" cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse" [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress" cmd="$cmd --exclude=/BACKUP --exclude=/backup --exclude=/mnt" cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys" cmd="$cmd --exclude=/usr/src --exclude=/usr/local/src" cmd="$cmd --exclude=/var/cache/apt --exclude=/var/amavis/blocked" cmd="$cmd --exclude=/var/log --exclude=/tmp --exclude=/var/tmp" [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add" [ "$local" -eq 0 ] \ && cmd="$cmd ${user}@${system}:/ $target" \ || cmd="$cmd / $target" Message "Calling: $cmd" echo -n "Start date: "; date count_started=$count_started+1 rm -f "$target/.stamp" if [ "$DRYRUN" -lt 1 ]; then $SHELL -c "$cmd"; ret=$? echo "code=$ret" >"$target/.stamp" else MessageLog " *** Trial run, not executing synchronization command!" ret=0 fi if [ $ret -eq 20 ]; then MessageLog "Backup of \"$system\" interrupted. Aborting ..." CleanUp exit 1 fi if [ $ret -eq 0 -o $ret -eq 24 ]; then [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1 MessageLog "System \"$system\" completed with status $ret, OK." [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1 else MessageLog "System \"$system\" completed with ERRORS, code $ret!" fi echo -n "End date: "; date destinations="$destinations $destdir" echo done sync Log "Done: $count_all jobs, $count_started started, $count_ok completed without errors." paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq ) if [ -n "$paths" ]; then df -h $paths echo fi CleanUp echo -n "Done: "; date echo echo " - $count_all jobs defined," echo " - $count_started jobs started," echo " - $count_ok done without errors." echo if [ $count_started -ne $count_ok ]; then echo "-----> THERE HAVE BEEN ERRORS! <-----" echo fi # -eof-