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
104 echo -n "Started: "; date
107 [ -r "${CONF_D}/backup-script.conf" ] && source "${CONF_D}/backup-script.conf"
109 # check and create PID file
110 if [ -e "$PIDFILE" ]; then
111 echo "Lockfile \"$PIDFILE\" already exists."
112 echo "Is an other instance still running?"
114 echo -n "Aborted: "; date
118 touch "$PIDFILE" 2>/dev/null
119 if [ $? -ne 0 ]; then
120 echo "Warning: can't create PID file \"$PIDFILE\"!"
123 echo "$$" >>"$PIDFILE"
126 if [ -n "$pre_exec" ]; then
127 echo "Executing \"$pre_exec\" ..."
129 if [ $? -ne 0 ]; then
130 echo "Error: pre-exec command failed!"; echo
132 echo "Aborting backup."; echo
140 [ -r "$f" -a -f "$f" ] || continue
144 "backup-script.conf"|*.sh)
149 # Set global defaults
151 target="$default_target"
152 ssh_args_add="$default_ssh_args_add"
153 rsync_args_add="$default_rsync_args_add"
154 exclude_args_add="$default_exclude_args_add"
155 compress="$default_compress"
157 local="$default_local"
159 # Read in system configuration file
163 && echo "Working on \"$system\" ..." \
164 || echo "Working on \"$system\" (local system) ..."
166 count_all=$count_all+1
168 # Check target directory
169 if [ -z "$target" ]; then
170 echo "No target directory specified for \"$system\"!? Skipped!"
173 if [ ! -d "$target" ]; then
174 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
178 sys_target="$target/$system"
179 if [ "$DRYRUN" -eq 0 ]; then
180 mkdir -p "$sys_target" >/dev/null 2>&1
181 if [ $? -ne 0 ]; then
182 echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
187 if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
188 # Check if system is alive
189 ping -c 1 "$system" >/dev/null 2>&1
190 if [ $? -ne 0 ]; then
191 echo "Host \"$system\" seems not to be alive!? Skipped."
194 echo "OK, host \"$system\" seems to be alive."
198 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
200 cmd="rsync --archive"
201 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
202 cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse"
203 [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
204 cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
205 cmd="$cmd --exclude=/run --exclude=/tmp --exclude=/var/tmp"
206 cmd="$cmd --exclude=/media --exclude=/mnt --exclude=/net"
207 cmd="$cmd --exclude=/var/cache/apt --exclude=/var/log"
208 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
209 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
212 && cmd="$cmd ${user}@${system}:/ $sys_target/" \
213 || cmd="$cmd / $sys_target/"
216 echo -n "Start date: "; date
217 count_started=$count_started+1
218 rm -f "$sys_target/.stamp"
220 if [ "$DRYRUN" -eq 0 ]; then
221 $SHELL -c "$cmd"; ret=$?
222 echo "code=$ret" >"$sys_target/.stamp"
224 echo " *** Trial run, not executing synchronization command!"
228 if [ $ret -eq 20 ]; then
229 echo "Backup of \"$system\" interrupted. Aborting ..."
234 if [ $ret -eq 0 -o $ret -eq 24 ]; then
235 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
237 echo "System \"$system\" completed with status $ret, OK."
238 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
240 echo "System \"$system\" completed with ERRORS, code $ret!"
242 echo -n "End date: "; date
244 destinations="$destinations $target"
250 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
251 if [ "$DRYRUN" -eq 0 -a -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! <-----"