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=""
36 default_exclude_args_add=""
42 echo "Usage: $NAME [<options>] [<system> [<system> [...]]]"
44 echo " -p, --progress Show progress, see rsync(1)."
45 echo " -d, --dry-run Test run only, don't copy any data."
47 echo "When no <system> is given, all defined systems are used."
53 logger -t "$NAME" "$*"
66 if [ -n "$post_exec" ]; then
67 echo "Executing \"$post_exec\" ..."
70 echo "Warning: post-exec command failed!"
79 echo "--> Got break signal, cleaning up & aborting ..."
82 echo -n "Aborted: "; date
87 while [ $# -gt 0 ]; do
103 if [ $# -ge 1 ]; then
105 if [ ! -r "${CONF_D}/$s" ]; then
106 echo "$NAME: Can' read \"${CONF_D}/$s\"!"
109 sys="$sys ${CONF_D}/$s"
115 trap GotSignal SIGINT
119 echo -n "Started: "; date
122 [ -r "${CONF_D}/backup-script.conf" ] && source "${CONF_D}/backup-script.conf"
124 # check and create PID file
125 if [ -e "$PIDFILE" ]; then
126 Log "Lockfile \"$PIDFILE\" already exists. Aborting!"
127 echo "Lockfile \"$PIDFILE\" already exists."
128 echo "Is an other instance still running?"
130 echo -n "Aborted: "; date
134 touch "$PIDFILE" 2>/dev/null
135 if [ $? -ne 0 ]; then
136 echo "Warning: can't create PID file \"$PIDFILE\"!"
139 echo "$$" >>"$PIDFILE"
142 if [ -n "$pre_exec" ]; then
143 echo "Executing \"$pre_exec\" ..."
145 if [ $? -ne 0 ]; then
146 echo "Error: pre-exec command failed!"; echo
148 echo "Aborting backup."; echo
156 [ -r "$f" -a -f "$f" ] || continue
160 "backup-script.conf"|*.sh)
165 # Set global defaults
167 target="$default_target"
168 ssh_args_add="$default_ssh_args_add"
169 rsync_args_add="$default_rsync_args_add"
170 exclude_args_add="$default_exclude_args_add"
171 compress="$default_compress"
173 local="$default_local"
175 # Read in system configuration file
179 && MessageLog "Working on \"$system\" ..." \
180 || MessageLog "Working on \"$system\" (local system) ..."
182 count_all=$count_all+1
184 # Check target directory
185 if [ -z "$target" ]; then
186 MessageLog "No target directory specified for \"$system\"!? Skipped!"
189 if [ ! -d "$target" ]; then
190 MessageLog "Target \"$target\" is not a directory!? \"$system\" skipped!"
195 target="$target/$system"
196 [ "$DRYRUN" -gt 1 ] || mkdir -p "$target"
198 if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
199 # Check if system is alive
200 ping -c 1 "$system" >/dev/null 2>&1
201 if [ $? -ne 0 ]; then
202 MessageLog "Host \"$system\" seems not to be alive!? Skipped."
205 Message "OK, host \"$system\" seems to be alive."
209 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
211 cmd="rsync --archive"
212 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
213 cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse"
214 [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
215 cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
216 cmd="$cmd --exclude=/run --exclude=/tmp --exclude=/var/tmp"
217 cmd="$cmd --exclude=/media --exclude=/mnt --exclude=/net"
218 cmd="$cmd --exclude=/var/cache/apt --exclude=/var/log"
219 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
220 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
223 && cmd="$cmd ${user}@${system}:/ $target" \
224 || cmd="$cmd / $target"
226 Message "Calling: $cmd"
227 echo -n "Start date: "; date
228 count_started=$count_started+1
229 rm -f "$target/.stamp"
231 if [ "$DRYRUN" -lt 1 ]; then
232 $SHELL -c "$cmd"; ret=$?
233 echo "code=$ret" >"$target/.stamp"
235 MessageLog " *** Trial run, not executing synchronization command!"
239 if [ $ret -eq 20 ]; then
240 MessageLog "Backup of \"$system\" interrupted. Aborting ..."
245 if [ $ret -eq 0 -o $ret -eq 24 ]; then
246 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
248 MessageLog "System \"$system\" completed with status $ret, OK."
249 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
251 MessageLog "System \"$system\" completed with ERRORS, code $ret!"
253 echo -n "End date: "; date
255 destinations="$destinations $destdir"
261 Log "Done: $count_all jobs, $count_started started, $count_ok completed without errors."
263 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
264 if [ -n "$paths" ]; then
271 echo -n "Done: "; date
273 echo " - $count_all jobs defined,"
274 echo " - $count_started jobs started,"
275 echo " - $count_ok done without errors."
278 if [ $count_started -ne $count_ok ]; then
279 echo "-----> THERE HAVE BEEN ERRORS! <-----"