]> arthur.barton.de Git - backup-script.git/blob - bin/backup-script
Create "latest" link to latest generation
[backup-script.git] / bin / backup-script
1 #!/bin/bash
2 #
3 # backup-script system for cloning systems using rsync
4 # Copyright (c)2008-2015 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 PIDFILE="/var/run/$NAME.pid"
15
16 DRYRUN=0
17 VERBOSE=0
18
19 export LC_ALL=C
20
21 declare -i count_all=0
22 declare -i count_started=0
23 declare -i count_ok=0
24 declare -i count_ok_vanished=0
25
26 destinations=""
27
28 # Default settings, can be overwritten in backup-script.conf:
29 conf_d="/etc/backup-script.d"
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 default_generations=0
41 default_job_pre_exec=""
42 default_job_post_exec=""
43
44 Usage() {
45         echo "Usage: $NAME [<options>] [<system> [<system> [...]]]"
46         echo
47         echo "  -p, --progress    Show progress, see rsync(1)."
48         echo "  -n, --dry-run     Test run only, don't copy any data."
49         echo
50         echo "When no <system> is given, all defined systems are used."
51         echo
52         exit 1
53 }
54
55 CleanUp() {
56         if [ -n "$post_exec" ]; then
57                 echo "Executing \"$post_exec\" ..."
58                 sh -c $post_exec
59                 if [ $? -ne 0 ]; then
60                         echo "Warning: post-exec command failed!"
61                 fi
62                 echo
63         fi
64         rm -f "$PIDFILE"
65 }
66
67 GotSignal() {
68         echo
69         echo "--> Got break signal, cleaning up & aborting ..."
70         echo
71         CleanUp
72         echo -n "Aborted: "; date
73         echo
74         exit 9
75 }
76
77 ExecJob() {
78         what="$1"
79         cmd="$2"
80
81         echo "Running job ${what}-exec command ..."
82         [ "$local" -eq 0 ] \
83                 && cmd="$ssh_cmd ${user}@${system} $cmd"
84         echo -n "Start date (${what}-exec): "; date
85         echo "$cmd"
86         if [ "$DRYRUN" -eq 0 ]; then
87                 $SHELL -c "$cmd"; ret=$?
88         else
89                 echo " *** Trial run, not executing ${what}-exec command!"
90                 ret=0
91         fi
92         [ $ret -eq 0 ] \
93                 && echo "The ${what}-exec command completed with status 0, OK." \
94                 || echo "The ${what}-exec command completed with ERRORS, code $ret!"
95         return $ret
96 }
97
98 while [ $# -gt 0 ]; do
99         case "$1" in
100           "-n"|"--dry-run")
101                 DRYRUN=1; shift
102                 ;;
103           "-p"|"--progress")
104                 VERBOSE=1; shift
105                 ;;
106           "-"*)
107                 Usage
108                 ;;
109           *)
110                 break
111         esac
112 done
113
114 trap GotSignal SIGINT
115
116 echo -n "Started: "; date
117
118 for conf in "/etc/backup-script.conf" "${conf_d}/backup-script.conf"; do
119         if [ -r "$conf" ]; then
120                 echo "Reading configuration: \"$conf\" ..."
121                 source "$conf"
122         fi
123 done
124 echo
125
126 if [ $# -ge 1 ]; then
127         for s in $@; do
128                 if [ ! -r "${conf_d}/$s" ]; then
129                         echo "$NAME: Can' read \"${conf_d}/$s\"!"
130                         exit 1
131                 fi
132                 sys="$sys ${conf_d}/$s"
133         done
134 else
135         sys=${conf_d}/*
136 fi
137
138 # check and create PID file
139 if [ -e "$PIDFILE" ]; then
140         echo "Lockfile \"$PIDFILE\" already exists."
141         echo "Is an other instance still running?"
142         echo
143         echo -n "Aborted: "; date
144         echo
145         exit 3
146 fi
147 touch "$PIDFILE" 2>/dev/null
148 if [ $? -ne 0 ]; then
149         echo "Warning: can't create PID file \"$PIDFILE\"!"
150         echo
151 else
152         echo "$$" >>"$PIDFILE"
153 fi
154
155 if [ -n "$pre_exec" ]; then
156         echo "Executing \"$pre_exec\" ..."
157         sh -c $pre_exec
158         if [ $? -ne 0 ]; then
159                 echo "Error: pre-exec command failed!"; echo
160                 CleanUp
161                 echo "Aborting backup."; echo
162                 exit 2
163         fi
164         sleep 2
165         echo
166 fi
167
168 for f in $sys; do
169         [ -r "$f" -a -f "$f" ] || continue
170
171         fname=`basename $f`
172         case "$fname" in
173                 "backup-script.conf"|*.sh)
174                         continue
175                         ;;
176         esac
177
178         # Set global defaults
179         system="$fname"
180         user="$default_user"
181         target="$default_target"
182         ssh_args_add="$default_ssh_args_add"
183         rsync_args_add="$default_rsync_args_add"
184         exclude_args_add="$default_exclude_args_add"
185         compress="$default_compress"
186         ping="$default_ping"
187         local="$default_local"
188         generations="$default_generations"
189         job_pre_exec="$default_job_pre_exec"
190         job_post_exec="$default_job_post_exec"
191
192         # Read in system configuration file
193         source "$f"
194
195         # Validate configuration
196         [ "$system" = "localhost" -o "$system" = "127.0.0.1" ] && local=1
197
198         [ "$system" = "$fname" ] \
199                 && systxt="\"$system\"" \
200                 || systxt="\"$fname\" [\"$system\"]"
201         [ "$local" -eq 0 ] \
202                 && echo "Working on $systxt ..." \
203                 || echo "Working on $systxt (local system) ..."
204
205         count_all=$count_all+1
206
207         # Check target directory
208         if [ -z "$target" ]; then
209                 echo "No target directory specified for \"$system\"!? Skipped!"
210                 echo; continue
211         fi
212         if [ ! -d "$target" ]; then
213                 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
214                 echo; continue
215         fi
216
217         sys_target="$target/$fname"
218         sys_root="$sys_target"
219         if [ "$DRYRUN" -eq 0 ]; then
220                 mkdir -p "$sys_target" >/dev/null 2>&1
221                 if [ $? -ne 0 ]; then
222                         echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
223                         echo; continue
224                 fi
225         fi
226
227         if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
228                 # Check if system is alive
229                 ping -c 1 "$system" >/dev/null 2>&1
230                 if [ $? -ne 0 ]; then
231                         echo "Host \"$system\" seems not to be alive!? Skipped."
232                         echo; continue
233                 fi
234                 echo "OK, host \"$system\" seems to be alive."
235         fi
236
237         if [ $generations -gt 0 ]; then
238                 # Make sure no old backup is stored in system directory
239                 if [ -e "$sys_target/.stamp" ]; then
240                         # There seems to be a genearation-less backup in the
241                         # target directory!
242                         echo "Target directory \"$sys_target\" seems to be unclean!? \"$system\" skipped!"
243                         echo; continue
244                 fi
245
246                 # Search directory of last generation, if any
247                 last="`ls -1 "$sys_target" 2>/dev/null | sort -r | head -n1`"
248                 if [ -n "$last" ]; then
249                         last="$sys_target/$last"
250                         if [ ! -d "$last" ]; then
251                                 echo "Last snapshot \"$last\" seems not to be a directory!? \"$system\" skipped!"
252                                 echo; continue
253                         fi
254                 fi
255                 sys_target="$sys_target/`date +%Y%m%d-%H%M%S`"
256
257                 if [ -n "$last" -a ! -e "$last/.stamp" ]; then
258                         # Old backup directory without "stamp file", continue
259                         echo "Found incomplete snapshot in \"$last\", reusing and renaming it ..."
260                         mv "$last" "$sys_target" >/dev/null 2>&1
261                         if [ $? -ne 0 ]; then
262                                 echo "Failed to rename last snapshot \"$last\" to \"$sys_target\"!? \"$system\" skipped!"
263                                 echo; continue
264                         fi
265                 elif [ -n "$last" ]; then
266                         # Old backup directory found, create new snapshot
267                         echo "Found last snapshot in \"$last\"."
268                         if [ "$DRYRUN" -eq 0 ]; then
269                                 btrfs subvolume snapshot \
270                                   "$last" "$sys_target" >/dev/null 2>&1; r=$?
271                                 if [ $r -ne 0 ]; then
272                                         echo "Can't create btrfs snapshot \"$sys_target\" of \"$last\", code $r!? \"$system\" skipped!"
273                                         echo; continue
274                                 fi
275                                 echo "Created new snapshot in \"$sys_target\"."
276                         else
277                                 echo " *** Trial run, not creating new snapshot in \"$sys_target\"!"
278                         fi
279                 else
280                         # No old backup found, create new subvolume
281                         if [ "$DRYRUN" -eq 0 ]; then
282                                 btrfs subvolume create \
283                                   "$sys_target" >/dev/null 2>&1; r=$?
284                                 if [ $r -ne 0 ]; then
285                                         echo "Can't create btrfs subvolume \"$sys_target\", code $r!? \"$system\" skipped!"
286                                         echo; continue
287                                 fi
288                                 echo "Created new subvolume in \"$sys_target\"."
289                         else
290                                 echo " *** Trial run, not creating new subvolume \"$sys_target\"!"
291                         fi
292                 fi
293         fi
294
295         ssh_cmd="ssh"
296         [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
297
298         # execute job "pre-exec" command, if any
299         if [ -n "$job_pre_exec" ]; then
300                 ExecJob pre "$job_pre_exec" ; ret=$?
301                 if [ $ret -ne 0 ]; then
302                         [ $ret -ne 99 ] && count_started=$count_started+1
303                         echo "Pre-exec command failed, \"$system\" skipped!"
304                         echo; continue
305                 fi
306         fi
307
308         # prepare (remote) command ...
309         cmd="rsync --archive"
310         [ "$compress" -ne 0 ] && cmd="$cmd --compress"
311         cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse"
312         [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
313         cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
314         cmd="$cmd --exclude=/run --exclude=/tmp --exclude=/var/tmp"
315         cmd="$cmd --exclude=/media --exclude=/mnt --exclude=/net"
316         cmd="$cmd --exclude=/var/cache/apt --exclude=/var/log"
317         [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
318         [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
319
320         [ "$local" -eq 0 ] \
321                 && cmd="$cmd ${user}@${system}:/ $sys_target/" \
322                 || cmd="$cmd / $sys_target/"
323
324         echo "Backing up to \"$sys_target\" ..."
325         echo -n "Start date: "; date
326         echo "$cmd"
327         count_started=$count_started+1
328         ok=0
329         
330         if [ "$DRYRUN" -eq 0 ]; then
331                 rm -f "$sys_target/.stamp"
332                 $SHELL -c "$cmd"; ret=$?
333                 echo "code=$ret" >"$sys_target/.stamp"
334         else
335                 echo " *** Trial run, not executing synchronization command!"
336                 ret=0
337         fi
338
339         if [ $ret -eq 20 ]; then
340                 echo "Backup of \"$system\" interrupted. Aborting ..."
341                 CleanUp
342                 exit 1
343         fi
344
345         echo -n "End date: "; date
346         if [ $ret -eq 0 -o $ret -eq 24 ]; then
347                 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
348
349                 echo "System \"$system\" completed with status $ret, OK."
350                 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
351                 ok=1
352         else
353                 echo "System \"$system\" completed with ERRORS, code $ret!"
354         fi
355
356         # execute job "post-exec" command, if any
357         if [ -n "$job_post_exec" ]; then
358                 ExecJob post "$job_post_exec"
359         fi
360
361         if [ $generations -gt 0 ]; then
362                 # Update "latest" symlink
363                 if [ "$DRYRUN" -eq 0 ]; then
364                         rm -f "$sys_root/latest"
365                         ln -s "$sys_target" "$sys_root/latest"
366                 fi
367                 # Clean up old generations
368                 to_delete=`ls -1t "$sys_root" 2>/dev/null | tail -n+$generations | sort`
369                 if [ -n "$to_delete" -a $ok -eq 1 ]; then
370                         [ "$DRYRUN" -eq 0 ] \
371                                 && echo "Deleting old backup generations:" \
372                                 || echo " *** Trial run, not deleting old generations:"
373                         for delete in $to_delete; do
374                                 dir="$sys_root/$delete"
375                                 if [ ! -e "$dir/.stamp" ]; then
376                                         echo "Not deleting \"$dir\", not a backup directory!?"
377                                         continue
378                                 fi
379                                 last=`stat "$dir/.stamp" 2>/dev/null | grep "^Modify: " \
380                                  | cut -d':' -f2- | cut -d. -f1`
381                                 echo "Removing backup from" $last "..."
382                                 if [ "$DRYRUN" -eq 0 ]; then
383                                         btrfs subvolume delete \
384                                          "$dir" >/dev/null 2>&1
385                                         [ $? -eq 0 ] || \
386                                          echo "Failed to delete \"$dir\"!"
387                                 fi
388                         done
389                         echo -n "Clean up finished: "; date
390                 elif [ -n "$to_delete" ]; then
391                         echo "There have been errors, not cleaning up old generations!"
392                 else
393                         echo "Nothing to clean up."
394                 fi
395         fi
396
397         destinations="$destinations $target"
398         echo
399 done
400
401 sync
402
403 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
404 if [ "$DRYRUN" -eq 0 -a -n "$paths" ]; then
405         df -h $paths
406         echo
407 fi
408
409 CleanUp
410
411 echo -n "Done: "; date
412 echo
413 [ $count_all -eq 1 ] && s="" || s="s"
414 echo " - $count_all job$s defined,"
415 [ $count_started -eq 1 ] && s="" || s="s"
416 echo " - $count_started job$s started,"
417 echo " - $count_ok done without errors."
418 echo
419
420 if [ $count_started -ne $count_ok ]; then
421         echo "----->  THERE HAVE BEEN ERRORS!  <-----"
422         echo
423 fi
424
425 # -eof-