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