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