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