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