3 # backup-script system for cloning systems using rsync
4 # Copyright (c)2008-2013 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"
15 PIDFILE="/var/run/$NAME.pid"
22 declare -i count_all=0
23 declare -i count_started=0
25 declare -i count_ok_vanished=0
34 echo "Usage: $NAME [<options>] [<system> [<system> [...]]]"
36 echo " -p, --progress Show progress, see rsync(1)."
37 echo " -d, --dry-run Test run only, don't copy any data."
39 echo "When no <system> is given, all defined systems are used."
45 logger -t "$NAME" "$*"
58 if [ -n "$post_exec" ]; then
59 echo "Executing \"$post_exec\" ..."
62 echo "Warning: post-exec command failed!"
71 echo "--> Got break signal, cleaning up & aborting ..."
74 echo -n "Aborted: "; date
79 while [ $# -gt 0 ]; do
97 if [ ! -r "${CONF_D}/$s" ]; then
98 echo "$NAME: Can' read \"${CONF_D}/$s\"!"
101 sys="$sys ${CONF_D}/$s"
107 trap GotSignal SIGINT
111 echo -n "Started: "; date
114 [ -r "${CONF_D}/backup-script.conf" ] && source "${CONF_D}/backup-script.conf"
116 # check and create PID file
117 if [ -e "$PIDFILE" ]; then
118 Log "Lockfile \"$PIDFILE\" already exists. Aborting!"
119 echo "Lockfile \"$PIDFILE\" already exists."
120 echo "Is an other instance still running?"
122 echo -n "Aborted: "; date
126 touch "$PIDFILE" 2>/dev/null
127 if [ $? -ne 0 ]; then
128 echo "Warning: can't create PID file \"$PIDFILE\"!"
131 echo "$$" >>"$PIDFILE"
134 if [ -n "$pre_exec" ]; then
135 echo "Executing \"$pre_exec\" ..."
137 if [ $? -ne 0 ]; then
138 echo "Error: pre-exec command failed!"; echo
140 echo "Aborting backup."; echo
148 [ -r "$f" -a -f "$f" ] || continue
152 target="$default_target"
160 "backup-script.conf"|*.sh)
165 # Read in configuration file
169 && MessageLog "Working on \"$system\" ..." \
170 || MessageLog "Working on \"$system\" (local system) ..."
172 count_all=$count_all+1
174 # Check target directory
175 if [ -z "$target" ]; then
176 MessageLog "No target directory specified for \"$system\"!? Skipped!"
179 if [ ! -d "$target" ]; then
180 MessageLog "Target \"$target\" is not a directory!? \"$system\" skipped!"
185 target="$target/$system"
186 [ "$DRYRUN" -gt 1 ] || mkdir -p "$target"
188 if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
189 # Check if system is alive
190 ping -c 1 "$system" >/dev/null 2>&1
191 if [ $? -ne 0 ]; then
192 MessageLog "Host \"$system\" seems not to be alive!? Skipped."
195 Message "OK, host \"$system\" seems to be alive."
199 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
201 cmd="rsync --archive"
202 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
203 cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse"
204 [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
205 cmd="$cmd --exclude=/BACKUP --exclude=/backup --exclude=/mnt"
206 cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
207 cmd="$cmd --exclude=/usr/src --exclude=/usr/local/src"
208 cmd="$cmd --exclude=/var/cache/apt --exclude=/var/amavis/blocked"
209 cmd="$cmd --exclude=/var/log --exclude=/tmp --exclude=/var/tmp"
210 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
213 && cmd="$cmd ${user}@${system}:/ $target" \
214 || cmd="$cmd / $target"
216 Message "Calling: $cmd"
217 echo -n "Start date: "; date
218 count_started=$count_started+1
219 rm -f "$target/.stamp"
221 if [ "$DRYRUN" -lt 1 ]; then
222 $SHELL -c "$cmd"; ret=$?
223 echo "code=$ret" >"$target/.stamp"
225 MessageLog " *** Trial run, not executing synchronization command!"
229 if [ $ret -eq 20 ]; then
230 MessageLog "Backup of \"$system\" interrupted. Aborting ..."
235 if [ $ret -eq 0 -o $ret -eq 24 ]; then
236 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
238 MessageLog "System \"$system\" completed with status $ret, OK."
239 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
241 MessageLog "System \"$system\" completed with ERRORS, code $ret!"
243 echo -n "End date: "; date
245 destinations="$destinations $destdir"
251 Log "Done: $count_all jobs, $count_started started, $count_ok completed without errors."
253 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
254 if [ -n "$paths" ]; then
261 echo -n "Done: "; date
263 echo " - $count_all jobs defined,"
264 echo " - $count_started jobs started,"
265 echo " - $count_ok done without errors."
268 if [ $count_started -ne $count_ok ]; then
269 echo "-----> THERE HAVE BEEN ERRORS! <-----"