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