3 # backup-script system for cloning systems using rsync
4 # Copyright (c)2008-2011 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 CONF_D="/etc/backup-script.d"
16 PIDFILE="/var/run/$NAME.pid"
20 declare -i count_all=0
21 declare -i count_started=0
23 declare -i count_ok_vanished=0
29 if [ "$1" == "-p" ]; then
35 echo "Usage: $NAME [-p] [<system>]"
40 logger -t "$NAME" "$*"
53 if [ -n "$post_exec" ]; then
54 echo "Executing \"$post_exec\" ..."
57 echo "Warning: post-exec command failed!"
66 echo "--> Got break signal, cleaning up & aborting ..."
69 echo -n "Aborted: "; date
76 if [ ! -r "$sys" ]; then
77 echo "$NAME: Can' read \"$sys\"!"
88 echo -n "Started: "; date
91 [ -r "${CONF_D}/backup-script.conf" ] && source "${CONF_D}/backup-script.conf"
93 # check and create PID file
94 if [ -e "$PIDFILE" ]; then
95 Log "Lockfile \"$PIDFILE\" already exists. Aborting!"
96 echo "Lockfile \"$PIDFILE\" already exists."
97 echo "Is an other instance still running?"
99 echo -n "Aborted: "; date
103 touch "$PIDFILE" 2>/dev/null
104 if [ $? -ne 0 ]; then
105 echo "Warning: can't create PID file \"$PIDFILE\"!"
108 echo "$$" >>"$PIDFILE"
111 if [ -n "$pre_exec" ]; then
112 echo "Executing \"$pre_exec\" ..."
114 if [ $? -ne 0 ]; then
115 echo "Error: pre-exec command failed!"; echo
117 echo "Aborting backup."; echo
125 [ -r "$f" -a -f "$f" ] || continue
137 "backup-script.conf"|*.sh)
142 # Read in configuration file
146 && MessageLog "Working on \"$system\" ..." \
147 || MessageLog "Working on \"$system\" (local system) ..."
149 count_all=$count_all+1
151 # Check target directory
152 if [ -z "$target" ]; then
153 MessageLog "No target directory specified for \"$system\"!? Skipped."
156 if [ ! -d "$target" ]; then
157 MessageLog "Target \"$target\" is not a directory!? \"$system\" skipped."
162 target="$target/$system"
165 if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
166 # Check if system is alive
167 ping -c 1 "$system" >/dev/null 2>&1
168 if [ $? -ne 0 ]; then
169 MessageLog "Host \"$system\" seems not to be alive!? Skipped."
172 Message "OK, host \"$system\" seems to be alive."
176 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
178 cmd="rsync --archive"
179 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
180 cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse"
181 [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
182 cmd="$cmd --exclude=/BACKUP --exclude=/backup --exclude=/mnt"
183 cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
184 cmd="$cmd --exclude=/usr/src --exclude=/usr/local/src"
185 cmd="$cmd --exclude=/var/cache/apt --exclude=/var/amavis/blocked"
186 cmd="$cmd --exclude=/var/log --exclude=/tmp --exclude=/var/tmp"
187 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
190 && cmd="$cmd ${user}@${system}:/ $target" \
191 || cmd="$cmd / $target"
193 Message "Calling: $cmd"
194 echo -n "Start date: "; date
195 count_started=$count_started+1
196 rm -f "$target/.stamp"
198 $SHELL -c "$cmd"; ret=$?
199 echo "code=$ret" >"$target/.stamp"
201 if [ $ret -eq 20 ]; then
202 MessageLog "Backup of \"$system\" interrupted. Aborting ..."
207 if [ $ret -eq 0 -o $ret -eq 24 ]; then
208 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
210 MessageLog "System \"$system\" completed with status $ret, OK."
213 MessageLog "System \"$system\" completed with ERRORS, code $ret!"
215 echo -n "End date: "; date
217 destinations="$destinations $destdir"
223 Log "Done: $count_all jobs, $count_started started, $count_ok completed without errors."
225 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
226 if [ -n "$paths" ]; then
233 echo -n "Done: "; date
235 echo " - $count_all jobs defined,"
236 echo " - $count_started jobs started,"
237 echo " - $count_ok done without errors."
240 if [ $count_started -ne $count_ok ]; then
241 echo "-----> THERE HAVE BEEN ERRORS! <-----"