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 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
74 while [ $# -gt 0 ]; do
92 if [ ! -r "${CONF_D}/$s" ]; then
93 echo "$NAME: Can' read \"${CONF_D}/$s\"!"
96 sys="$sys ${CONF_D}/$s"
102 trap GotSignal SIGINT
106 echo -n "Started: "; date
109 [ -r "${CONF_D}/backup-script.conf" ] && source "${CONF_D}/backup-script.conf"
111 # check and create PID file
112 if [ -e "$PIDFILE" ]; then
113 Log "Lockfile \"$PIDFILE\" already exists. Aborting!"
114 echo "Lockfile \"$PIDFILE\" already exists."
115 echo "Is an other instance still running?"
117 echo -n "Aborted: "; date
121 touch "$PIDFILE" 2>/dev/null
122 if [ $? -ne 0 ]; then
123 echo "Warning: can't create PID file \"$PIDFILE\"!"
126 echo "$$" >>"$PIDFILE"
129 if [ -n "$pre_exec" ]; then
130 echo "Executing \"$pre_exec\" ..."
132 if [ $? -ne 0 ]; then
133 echo "Error: pre-exec command failed!"; echo
135 echo "Aborting backup."; echo
143 [ -r "$f" -a -f "$f" ] || continue
147 "backup-script.conf"|*.sh)
152 # Set global defaults
154 target="$default_target"
155 ssh_args_add="$default_ssh_args_add"
156 rsync_args_add="$default_rsync_args_add"
157 exclude_args_add="$default_exclude_args_add"
158 compress="$default_compress"
160 local="$default_local"
162 # Read in system configuration file
166 && echo "Working on \"$system\" ..." \
167 || echo "Working on \"$system\" (local system) ..."
169 count_all=$count_all+1
171 # Check target directory
172 if [ -z "$target" ]; then
173 echo "No target directory specified for \"$system\"!? Skipped!"
176 if [ ! -d "$target" ]; then
177 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
182 target="$target/$system"
183 [ "$DRYRUN" -gt 1 ] || mkdir -p "$target"
185 if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
186 # Check if system is alive
187 ping -c 1 "$system" >/dev/null 2>&1
188 if [ $? -ne 0 ]; then
189 echo "Host \"$system\" seems not to be alive!? Skipped."
192 echo "OK, host \"$system\" seems to be alive."
196 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
198 cmd="rsync --archive"
199 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
200 cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse"
201 [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
202 cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
203 cmd="$cmd --exclude=/run --exclude=/tmp --exclude=/var/tmp"
204 cmd="$cmd --exclude=/media --exclude=/mnt --exclude=/net"
205 cmd="$cmd --exclude=/var/cache/apt --exclude=/var/log"
206 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
207 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
210 && cmd="$cmd ${user}@${system}:/ $target" \
211 || cmd="$cmd / $target"
214 echo -n "Start date: "; date
215 count_started=$count_started+1
216 rm -f "$target/.stamp"
218 if [ "$DRYRUN" -lt 1 ]; then
219 $SHELL -c "$cmd"; ret=$?
220 echo "code=$ret" >"$target/.stamp"
222 echo " *** Trial run, not executing synchronization command!"
226 if [ $ret -eq 20 ]; then
227 echo "Backup of \"$system\" interrupted. Aborting ..."
232 if [ $ret -eq 0 -o $ret -eq 24 ]; then
233 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
235 echo "System \"$system\" completed with status $ret, OK."
236 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
238 echo "System \"$system\" completed with ERRORS, code $ret!"
240 echo -n "End date: "; date
242 destinations="$destinations $destdir"
248 Log "Done: $count_all jobs, $count_started started, $count_ok completed without errors."
250 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
251 if [ -n "$paths" ]; then
258 echo -n "Done: "; date
260 echo " - $count_all jobs defined,"
261 echo " - $count_started jobs started,"
262 echo " - $count_ok done without errors."
265 if [ $count_started -ne $count_ok ]; then
266 echo "-----> THERE HAVE BEEN ERRORS! <-----"