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