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