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
152 target="$default_target"
153 ssh_args_add="$default_ssh_args_add"
154 rsync_args_add="$default_rsync_args_add"
155 exclude_args_add="$default_exclude_args_add"
156 compress="$default_compress"
158 local="$default_local"
160 # Read in system configuration file
163 # Validate configuration
164 [ "$system" = "localhost" -o "$system" = "127.0.0.1" ] && local=1
166 [ "$system" = "$fname" ] \
167 && systxt="\"$system\"" \
168 || systxt="\"$fname\" [\"$system\"]"
170 && echo "Working on $systxt ..." \
171 || echo "Working on $sytxts (local system) ..."
173 count_all=$count_all+1
175 # Check target directory
176 if [ -z "$target" ]; then
177 echo "No target directory specified for \"$system\"!? Skipped!"
180 if [ ! -d "$target" ]; then
181 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
185 sys_target="$target/$system"
186 if [ "$DRYRUN" -eq 0 ]; then
187 mkdir -p "$sys_target" >/dev/null 2>&1
188 if [ $? -ne 0 ]; then
189 echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
194 if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
195 # Check if system is alive
196 ping -c 1 "$system" >/dev/null 2>&1
197 if [ $? -ne 0 ]; then
198 echo "Host \"$system\" seems not to be alive!? Skipped."
201 echo "OK, host \"$system\" seems to be alive."
205 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
207 cmd="rsync --archive"
208 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
209 cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse"
210 [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
211 cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
212 cmd="$cmd --exclude=/run --exclude=/tmp --exclude=/var/tmp"
213 cmd="$cmd --exclude=/media --exclude=/mnt --exclude=/net"
214 cmd="$cmd --exclude=/var/cache/apt --exclude=/var/log"
215 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
216 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
219 && cmd="$cmd ${user}@${system}:/ $sys_target/" \
220 || cmd="$cmd / $sys_target/"
222 echo "Backing up to \"$sys_target\" ..."
223 echo -n "Start date: "; date
225 count_started=$count_started+1
227 if [ "$DRYRUN" -eq 0 ]; then
228 rm -f "$sys_target/.stamp"
229 $SHELL -c "$cmd"; ret=$?
230 echo "code=$ret" >"$sys_target/.stamp"
232 echo " *** Trial run, not executing synchronization command!"
236 if [ $ret -eq 20 ]; then
237 echo "Backup of \"$system\" interrupted. Aborting ..."
242 echo -n "End date: "; date
243 if [ $ret -eq 0 -o $ret -eq 24 ]; then
244 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
246 echo "System \"$system\" completed with status $ret, OK."
247 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
249 echo "System \"$system\" completed with ERRORS, code $ret!"
252 destinations="$destinations $target"
258 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
259 if [ "$DRYRUN" -eq 0 -a -n "$paths" ]; then
266 echo -n "Done: "; date
268 [ $count_all -eq 1 ] && s="" || s="s"
269 echo " - $count_all job$s defined,"
270 [ $count_started -eq 1 ] && s="" || s="s"
271 echo " - $count_started job$s started,"
272 echo " - $count_ok done without errors."
275 if [ $count_started -ne $count_ok ]; then
276 echo "-----> THERE HAVE BEEN ERRORS! <-----"