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
29 # Default settings, can be overwritten in backup-script.conf:
34 default_ssh_args_add=""
35 default_rsync_args_add=""
41 echo "Usage: $NAME [<options>] [<system> [<system> [...]]]"
43 echo " -p, --progress Show progress, see rsync(1)."
44 echo " -d, --dry-run Test run only, don't copy any data."
46 echo "When no <system> is given, all defined systems are used."
52 logger -t "$NAME" "$*"
65 if [ -n "$post_exec" ]; then
66 echo "Executing \"$post_exec\" ..."
69 echo "Warning: post-exec command failed!"
78 echo "--> Got break signal, cleaning up & aborting ..."
81 echo -n "Aborted: "; date
86 while [ $# -gt 0 ]; do
102 if [ $# -ge 1 ]; then
104 if [ ! -r "${CONF_D}/$s" ]; then
105 echo "$NAME: Can' read \"${CONF_D}/$s\"!"
108 sys="$sys ${CONF_D}/$s"
114 trap GotSignal SIGINT
118 echo -n "Started: "; date
121 [ -r "${CONF_D}/backup-script.conf" ] && source "${CONF_D}/backup-script.conf"
123 # check and create PID file
124 if [ -e "$PIDFILE" ]; then
125 Log "Lockfile \"$PIDFILE\" already exists. Aborting!"
126 echo "Lockfile \"$PIDFILE\" already exists."
127 echo "Is an other instance still running?"
129 echo -n "Aborted: "; date
133 touch "$PIDFILE" 2>/dev/null
134 if [ $? -ne 0 ]; then
135 echo "Warning: can't create PID file \"$PIDFILE\"!"
138 echo "$$" >>"$PIDFILE"
141 if [ -n "$pre_exec" ]; then
142 echo "Executing \"$pre_exec\" ..."
144 if [ $? -ne 0 ]; then
145 echo "Error: pre-exec command failed!"; echo
147 echo "Aborting backup."; echo
155 [ -r "$f" -a -f "$f" ] || continue
159 "backup-script.conf"|*.sh)
164 # Set global defaults
166 target="$default_target"
167 ssh_args_add="$default_ssh_args_add"
168 rsync_args_add="$default_rsync_args_add"
169 compress="$default_compress"
171 local="$default_local"
173 # Read in system configuration file
177 && MessageLog "Working on \"$system\" ..." \
178 || MessageLog "Working on \"$system\" (local system) ..."
180 count_all=$count_all+1
182 # Check target directory
183 if [ -z "$target" ]; then
184 MessageLog "No target directory specified for \"$system\"!? Skipped!"
187 if [ ! -d "$target" ]; then
188 MessageLog "Target \"$target\" is not a directory!? \"$system\" skipped!"
193 target="$target/$system"
194 [ "$DRYRUN" -gt 1 ] || mkdir -p "$target"
196 if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
197 # Check if system is alive
198 ping -c 1 "$system" >/dev/null 2>&1
199 if [ $? -ne 0 ]; then
200 MessageLog "Host \"$system\" seems not to be alive!? Skipped."
203 Message "OK, host \"$system\" seems to be alive."
207 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
209 cmd="rsync --archive"
210 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
211 cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse"
212 [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
213 cmd="$cmd --exclude=/BACKUP --exclude=/backup --exclude=/mnt"
214 cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
215 cmd="$cmd --exclude=/usr/src --exclude=/usr/local/src"
216 cmd="$cmd --exclude=/var/cache/apt --exclude=/var/amavis/blocked"
217 cmd="$cmd --exclude=/var/log --exclude=/tmp --exclude=/var/tmp"
218 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
221 && cmd="$cmd ${user}@${system}:/ $target" \
222 || cmd="$cmd / $target"
224 Message "Calling: $cmd"
225 echo -n "Start date: "; date
226 count_started=$count_started+1
227 rm -f "$target/.stamp"
229 if [ "$DRYRUN" -lt 1 ]; then
230 $SHELL -c "$cmd"; ret=$?
231 echo "code=$ret" >"$target/.stamp"
233 MessageLog " *** Trial run, not executing synchronization command!"
237 if [ $ret -eq 20 ]; then
238 MessageLog "Backup of \"$system\" interrupted. Aborting ..."
243 if [ $ret -eq 0 -o $ret -eq 24 ]; then
244 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
246 MessageLog "System \"$system\" completed with status $ret, OK."
247 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
249 MessageLog "System \"$system\" completed with ERRORS, code $ret!"
251 echo -n "End date: "; date
253 destinations="$destinations $destdir"
259 Log "Done: $count_all jobs, $count_started started, $count_ok completed without errors."
261 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
262 if [ -n "$paths" ]; then
269 echo -n "Done: "; date
271 echo " - $count_all jobs defined,"
272 echo " - $count_started jobs started,"
273 echo " - $count_ok done without errors."
276 if [ $count_started -ne $count_ok ]; then
277 echo "-----> THERE HAVE BEEN ERRORS! <-----"