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