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