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