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