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