]> arthur.barton.de Git - backup-script.git/blob - bin/backup-script
Autodetect "localhost" and 127.0.0.1 as being "local"
[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 echo -n "Started: "; date
105 echo
106
107 [ -r "${CONF_D}/backup-script.conf" ] && source "${CONF_D}/backup-script.conf"
108
109 # check and create PID file
110 if [ -e "$PIDFILE" ]; then
111         echo "Lockfile \"$PIDFILE\" already exists."
112         echo "Is an other instance still running?"
113         echo
114         echo -n "Aborted: "; date
115         echo
116         exit 3
117 fi
118 touch "$PIDFILE" 2>/dev/null
119 if [ $? -ne 0 ]; then
120         echo "Warning: can't create PID file \"$PIDFILE\"!"
121         echo
122 else
123         echo "$$" >>"$PIDFILE"
124 fi
125
126 if [ -n "$pre_exec" ]; then
127         echo "Executing \"$pre_exec\" ..."
128         sh -c $pre_exec
129         if [ $? -ne 0 ]; then
130                 echo "Error: pre-exec command failed!"; echo
131                 CleanUp
132                 echo "Aborting backup."; echo
133                 exit 2
134         fi
135         sleep 2
136         echo
137 fi
138
139 for f in $sys; do
140         [ -r "$f" -a -f "$f" ] || continue
141
142         fname=`basename $f`
143         case "$fname" in
144                 "backup-script.conf"|*.sh)
145                         continue
146                         ;;
147         esac
148
149         # Set global defaults
150         system="$fname"
151         user="$default_user"
152         target="$default_target"
153         ssh_args_add="$default_ssh_args_add"
154         rsync_args_add="$default_rsync_args_add"
155         exclude_args_add="$default_exclude_args_add"
156         compress="$default_compress"
157         ping="$default_ping"
158         local="$default_local"
159
160         # Read in system configuration file
161         source "$f"
162
163         # Validate configuration
164         [ "$system" = "localhost" -o "$system" = "127.0.0.1" ] && local=1
165
166         [ "$system" = "$fname" ] \
167                 && systxt="\"$system\"" \
168                 || systxt="\"$fname\" [\"$system\"]"
169         [ "$local" -eq 0 ] \
170                 && echo "Working on $systxt ..." \
171                 || echo "Working on $sytxts (local system) ..."
172
173         count_all=$count_all+1
174
175         # Check target directory
176         if [ -z "$target" ]; then
177                 echo "No target directory specified for \"$system\"!? Skipped!"
178                 echo; continue
179         fi
180         if [ ! -d "$target" ]; then
181                 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
182                 echo; continue
183         fi
184
185         sys_target="$target/$system"
186         if [ "$DRYRUN" -eq 0 ]; then
187                 mkdir -p "$sys_target" >/dev/null 2>&1
188                 if [ $? -ne 0 ]; then
189                         echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
190                         echo continue
191                 fi
192         fi
193
194         if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
195                 # Check if system is alive
196                 ping -c 1 "$system" >/dev/null 2>&1
197                 if [ $? -ne 0 ]; then
198                         echo "Host \"$system\" seems not to be alive!? Skipped."
199                         echo; continue
200                 fi
201                 echo "OK, host \"$system\" seems to be alive."
202         fi
203
204         ssh_cmd="ssh"
205         [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
206
207         cmd="rsync --archive"
208         [ "$compress" -ne 0 ] && cmd="$cmd --compress"
209         cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse"
210         [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
211         cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
212         cmd="$cmd --exclude=/run --exclude=/tmp --exclude=/var/tmp"
213         cmd="$cmd --exclude=/media --exclude=/mnt --exclude=/net"
214         cmd="$cmd --exclude=/var/cache/apt --exclude=/var/log"
215         [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
216         [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
217
218         [ "$local" -eq 0 ] \
219                 && cmd="$cmd ${user}@${system}:/ $sys_target/" \
220                 || cmd="$cmd / $sys_target/"
221
222         echo "Backing up to \"$sys_target\" ..."
223         echo -n "Start date: "; date
224         echo "$cmd"
225         count_started=$count_started+1
226         
227         if [ "$DRYRUN" -eq 0 ]; then
228                 rm -f "$sys_target/.stamp"
229                 $SHELL -c "$cmd"; ret=$?
230                 echo "code=$ret" >"$sys_target/.stamp"
231         else
232                 echo " *** Trial run, not executing synchronization command!"
233                 ret=0
234         fi
235
236         if [ $ret -eq 20 ]; then
237                 echo "Backup of \"$system\" interrupted. Aborting ..."
238                 CleanUp
239                 exit 1
240         fi
241
242         echo -n "End date: "; date
243         if [ $ret -eq 0 -o $ret -eq 24 ]; then
244                 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
245
246                 echo "System \"$system\" completed with status $ret, OK."
247                 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
248         else
249                 echo "System \"$system\" completed with ERRORS, code $ret!"
250         fi
251
252         destinations="$destinations $target"
253         echo
254 done
255
256 sync
257
258 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
259 if [ "$DRYRUN" -eq 0 -a -n "$paths" ]; then
260         df -h $paths
261         echo
262 fi
263
264 CleanUp
265
266 echo -n "Done: "; date
267 echo
268 [ $count_all -eq 1 ] && s="" || s="s"
269 echo " - $count_all job$s defined,"
270 [ $count_started -eq 1 ] && s="" || s="s"
271 echo " - $count_started job$s started,"
272 echo " - $count_ok done without errors."
273 echo
274
275 if [ $count_started -ne $count_ok ]; then
276         echo "----->  THERE HAVE BEEN ERRORS!  <-----"
277         echo
278 fi
279
280 # -eof-