]> arthur.barton.de Git - backup-script.git/blob - bin/backup-script
Allow overriding of "system" variable
[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         [ "$system" = "$fname" ] \
164                 && systxt="\"$system\"" \
165                 || systxt="\"$fname\" [\"$system\"]"
166         [ "$local" -eq 0 ] \
167                 && echo "Working on $systxt ..." \
168                 || echo "Working on $sytxts (local system) ..."
169
170         count_all=$count_all+1
171
172         # Check target directory
173         if [ -z "$target" ]; then
174                 echo "No target directory specified for \"$system\"!? Skipped!"
175                 echo; continue
176         fi
177         if [ ! -d "$target" ]; then
178                 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
179                 echo; continue
180         fi
181
182         sys_target="$target/$system"
183         if [ "$DRYRUN" -eq 0 ]; then
184                 mkdir -p "$sys_target" >/dev/null 2>&1
185                 if [ $? -ne 0 ]; then
186                         echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
187                         echo continue
188                 fi
189         fi
190
191         if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
192                 # Check if system is alive
193                 ping -c 1 "$system" >/dev/null 2>&1
194                 if [ $? -ne 0 ]; then
195                         echo "Host \"$system\" seems not to be alive!? Skipped."
196                         echo; continue
197                 fi
198                 echo "OK, host \"$system\" seems to be alive."
199         fi
200
201         ssh_cmd="ssh"
202         [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
203
204         cmd="rsync --archive"
205         [ "$compress" -ne 0 ] && cmd="$cmd --compress"
206         cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse"
207         [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
208         cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
209         cmd="$cmd --exclude=/run --exclude=/tmp --exclude=/var/tmp"
210         cmd="$cmd --exclude=/media --exclude=/mnt --exclude=/net"
211         cmd="$cmd --exclude=/var/cache/apt --exclude=/var/log"
212         [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
213         [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
214
215         [ "$local" -eq 0 ] \
216                 && cmd="$cmd ${user}@${system}:/ $sys_target/" \
217                 || cmd="$cmd / $sys_target/"
218
219         echo "Backing up to \"$sys_target\" ..."
220         echo -n "Start date: "; date
221         echo "$cmd"
222         count_started=$count_started+1
223         
224         if [ "$DRYRUN" -eq 0 ]; then
225                 rm -f "$sys_target/.stamp"
226                 $SHELL -c "$cmd"; ret=$?
227                 echo "code=$ret" >"$sys_target/.stamp"
228         else
229                 echo " *** Trial run, not executing synchronization command!"
230                 ret=0
231         fi
232
233         if [ $ret -eq 20 ]; then
234                 echo "Backup of \"$system\" interrupted. Aborting ..."
235                 CleanUp
236                 exit 1
237         fi
238
239         echo -n "End date: "; date
240         if [ $ret -eq 0 -o $ret -eq 24 ]; then
241                 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
242
243                 echo "System \"$system\" completed with status $ret, OK."
244                 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
245         else
246                 echo "System \"$system\" completed with ERRORS, code $ret!"
247         fi
248
249         destinations="$destinations $target"
250         echo
251 done
252
253 sync
254
255 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
256 if [ "$DRYRUN" -eq 0 -a -n "$paths" ]; then
257         df -h $paths
258         echo
259 fi
260
261 CleanUp
262
263 echo -n "Done: "; date
264 echo
265 [ $count_all -eq 1 ] && s="" || s="s"
266 echo " - $count_all job$s defined,"
267 [ $count_started -eq 1 ] && s="" || s="s"
268 echo " - $count_started job$s started,"
269 echo " - $count_ok done without errors."
270 echo
271
272 if [ $count_started -ne $count_ok ]; then
273         echo "----->  THERE HAVE BEEN ERRORS!  <-----"
274         echo
275 fi
276
277 # -eof-