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