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