]> arthur.barton.de Git - backup-script.git/blob - bin/backup-script
Implement "--dry-run" ("-d") command line option
[backup-script.git] / bin / backup-script
1 #!/bin/bash
2 #
3 # backup-script system for cloning systems using rsync
4 # Copyright (c)2008-2013 Alexander Barton, alex@barton.de
5 #
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.
11 #
12
13 NAME=`basename $0`
14 CONF_D="/etc/backup-script.d"
15 PIDFILE="/var/run/$NAME.pid"
16
17 DRYRUN=0
18 VERBOSE=0
19
20 export LC_ALL=C
21
22 declare -i count_all=0
23 declare -i count_started=0
24 declare -i count_ok=0
25 declare -i count_ok_vanished=0
26
27 destinations=""
28 pre_exec=""
29 post_exec=""
30 default_target=""
31 default_user="root"
32
33 Usage() {
34         echo "Usage: $NAME [<options>] [<system> [<system> [...]]]"
35         echo
36         echo "  -p, --progress    Show progress, see rsync(1)."
37         echo "  -d, --dry-run     Test run only, don't copy any data."
38         echo
39         echo "When no <system> is given, all defined systems are used."
40         echo
41         exit 1
42 }
43
44 Log() {
45         logger -t "$NAME" "$*"
46 }
47
48 Message() {
49         echo "$*"
50 }
51
52 MessageLog() {
53         Log "$*"
54         Message "$*"
55 }
56
57 CleanUp() {
58         if [ -n "$post_exec" ]; then
59                 echo "Executing \"$post_exec\" ..."
60                 sh -c $post_exec
61                 if [ $? -ne 0 ]; then
62                         echo "Warning: post-exec command failed!"
63                 fi
64                 echo
65         fi
66         rm -f "$PIDFILE"
67 }
68
69 GotSignal() {
70         echo
71         echo "--> Got break signal, cleaning up & aborting ..."
72         echo
73         CleanUp
74         echo -n "Aborted: "; date
75         echo
76         exit 9
77 }
78
79 while [ $# -gt 0 ]; do
80         case "$1" in
81           "-n"|"--dry-run")
82                 DRYRUN=1; shift
83                 ;;
84           "-p"|"--progress")
85                 VERBOSE=1; shift
86                 ;;
87           "-"*)
88                 Usage
89                 ;;
90           *)
91                 break
92         esac
93 done
94
95 if [ $# -ge 1 ]; then
96         for s in $@; do
97                 if [ ! -r "${CONF_D}/$s" ]; then
98                         echo "$NAME: Can' read \"${CONF_D}/$s\"!"
99                         exit 1
100                 fi
101                 sys="$sys ${CONF_D}/$s"
102         done
103 else
104         sys=${CONF_D}/*
105 fi
106
107 trap GotSignal SIGINT
108
109 Log "Started ..."
110
111 echo -n "Started: "; date
112 echo
113
114 [ -r "${CONF_D}/backup-script.conf" ] && source "${CONF_D}/backup-script.conf"
115
116 # check and create PID file
117 if [ -e "$PIDFILE" ]; then
118         Log "Lockfile \"$PIDFILE\" already exists. Aborting!"
119         echo "Lockfile \"$PIDFILE\" already exists."
120         echo "Is an other instance still running?"
121         echo
122         echo -n "Aborted: "; date
123         echo
124         exit 3
125 fi
126 touch "$PIDFILE" 2>/dev/null
127 if [ $? -ne 0 ]; then
128         echo "Warning: can't create PID file \"$PIDFILE\"!"
129         echo
130 else
131         echo "$$" >>"$PIDFILE"
132 fi
133
134 if [ -n "$pre_exec" ]; then
135         echo "Executing \"$pre_exec\" ..."
136         sh -c $pre_exec
137         if [ $? -ne 0 ]; then
138                 echo "Error: pre-exec command failed!"; echo
139                 CleanUp
140                 echo "Aborting backup."; echo
141                 exit 2
142         fi
143         sleep 2
144         echo
145 fi
146
147 for f in $sys; do
148         [ -r "$f" -a -f "$f" ] || continue
149
150         system=`basename $f`
151         user="$default_user"
152         target="$default_target"
153         ssh_args_add=""
154         rsync_args_add=""
155         compress=1
156         ping=1
157         local=0
158
159         case "$system" in
160                 "backup-script.conf"|*.sh)
161                         continue
162                         ;;
163         esac
164
165         # Read in configuration file
166         source "$f"
167
168         [ "$local" -eq 0 ] \
169                 && MessageLog "Working on \"$system\" ..." \
170                 || MessageLog "Working on \"$system\" (local system) ..."
171
172         count_all=$count_all+1
173
174         # Check target directory
175         if [ -z "$target" ]; then
176                 MessageLog "No target directory specified for \"$system\"!? Skipped!"
177                 echo; continue
178         fi
179         if [ ! -d "$target" ]; then
180                 MessageLog "Target \"$target\" is not a directory!? \"$system\" skipped!"
181                 echo; continue
182         fi
183
184         destdir="$target"
185         target="$target/$system"
186         [ "$DRYRUN" -gt 1 ] || mkdir -p "$target"
187
188         if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
189                 # Check if system is alive
190                 ping -c 1 "$system" >/dev/null 2>&1
191                 if [ $? -ne 0 ]; then
192                         MessageLog "Host \"$system\" seems not to be alive!? Skipped."
193                         echo; continue
194                 fi
195                 Message "OK, host \"$system\" seems to be alive."
196         fi
197
198         ssh_cmd="ssh"
199         [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
200
201         cmd="rsync --archive"
202         [ "$compress" -ne 0 ] && cmd="$cmd --compress"
203         cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse"
204         [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
205         cmd="$cmd --exclude=/BACKUP --exclude=/backup --exclude=/mnt"
206         cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
207         cmd="$cmd --exclude=/usr/src --exclude=/usr/local/src"
208         cmd="$cmd --exclude=/var/cache/apt --exclude=/var/amavis/blocked"
209         cmd="$cmd --exclude=/var/log --exclude=/tmp --exclude=/var/tmp"
210         [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
211
212         [ "$local" -eq 0 ] \
213                 && cmd="$cmd ${user}@${system}:/ $target" \
214                 || cmd="$cmd / $target"
215
216         Message "Calling: $cmd"
217         echo -n "Start date: "; date
218         count_started=$count_started+1
219         rm -f "$target/.stamp"
220         
221         if [ "$DRYRUN" -lt 1 ]; then
222                 $SHELL -c "$cmd"; ret=$?
223                 echo "code=$ret" >"$target/.stamp"
224         else
225                 MessageLog " *** Trial run, not executing synchronization command!"
226                 ret=0
227         fi
228
229         if [ $ret -eq 20 ]; then
230                 MessageLog "Backup of \"$system\" interrupted. Aborting ..."
231                 CleanUp
232                 exit 1
233         fi
234
235         if [ $ret -eq 0 -o $ret -eq 24 ]; then
236                 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
237
238                 MessageLog "System \"$system\" completed with status $ret, OK."
239                 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
240         else
241                 MessageLog "System \"$system\" completed with ERRORS, code $ret!"
242         fi
243         echo -n "End date: "; date
244
245         destinations="$destinations $destdir"
246         echo
247 done
248
249 sync
250
251 Log "Done: $count_all jobs, $count_started started, $count_ok completed without errors."
252
253 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
254 if [ -n "$paths" ]; then
255         df -h $paths
256         echo
257 fi
258
259 CleanUp
260
261 echo -n "Done: "; date
262 echo
263 echo " - $count_all jobs defined,"
264 echo " - $count_started jobs started,"
265 echo " - $count_ok done without errors."
266 echo
267
268 if [ $count_started -ne $count_ok ]; then
269         echo "----->  THERE HAVE BEEN ERRORS!  <-----"
270         echo
271 fi
272
273 # -eof-