3 # backup-script system for cloning systems using rsync
4 # Copyright (c)2008-2015 Alexander Barton <alex@barton.de>
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.
14 PIDFILE="/var/run/$NAME.pid"
21 declare -i count_all=0
22 declare -i count_started=0
24 declare -i count_ok_vanished=0
28 # Default settings, can be overwritten in backup-script.conf:
29 conf_d="/etc/backup-script.d"
34 default_ssh_args_add=""
35 default_rsync_args_add=""
36 default_exclude_args_add=""
41 default_job_pre_exec=""
42 default_job_post_exec=""
45 echo "Usage: $NAME [<options>] [<system> [<system> [...]]]"
47 echo " -p, --progress Show progress, see rsync(1)."
48 echo " -n, --dry-run Test run only, don't copy any data."
50 echo "When no <system> is given, all defined systems are used."
56 if [ -n "$post_exec" ]; then
57 echo "Executing \"$post_exec\" ..."
60 echo "Warning: post-exec command failed!"
69 echo "--> Got break signal, cleaning up & aborting ..."
72 echo -n "Aborted: "; date
81 echo "Running job ${what}-exec command ..."
83 && cmd="$ssh_cmd ${user}@${system} $cmd"
84 echo -n "Start date (${what}-exec): "; date
86 if [ "$DRYRUN" -eq 0 ]; then
87 $SHELL -c "$cmd"; ret=$?
89 echo " *** Trial run, not executing ${what}-exec command!"
93 && echo "The ${what}-exec command completed with status 0, OK." \
94 || echo "The ${what}-exec command completed with ERRORS, code $ret!"
101 btrfs subvolume create "$volume"
108 btrfs subvolume snapshot "$source" "$volume"
115 mv "$source" "$target"
121 btrfs subvolume delete "$volume"
124 while [ $# -gt 0 ]; do
140 trap GotSignal SIGINT
142 echo -n "Started: "; date
144 for conf in "/etc/backup-script.conf" "${conf_d}/backup-script.conf"; do
145 if [ -r "$conf" ]; then
146 echo "Reading configuration: \"$conf\" ..."
152 if [ $# -ge 1 ]; then
154 if [ ! -r "${conf_d}/$s" ]; then
155 echo "$NAME: Can' read \"${conf_d}/$s\"!"
158 sys="$sys ${conf_d}/$s"
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?"
169 echo -n "Aborted: "; date
173 touch "$PIDFILE" 2>/dev/null
174 if [ $? -ne 0 ]; then
175 echo "Warning: can't create PID file \"$PIDFILE\"!"
178 echo "$$" >>"$PIDFILE"
181 if [ -n "$pre_exec" ]; then
182 echo "Executing \"$pre_exec\" ..."
184 if [ $? -ne 0 ]; then
185 echo "Error: pre-exec command failed!"; echo
187 echo "Aborting backup."; echo
195 [ -r "$f" -a -f "$f" ] || continue
199 "backup-script.conf"|*.sh)
204 # Set global defaults
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"
213 local="$default_local"
214 generations="$default_generations"
215 job_pre_exec="$default_job_pre_exec"
216 job_post_exec="$default_job_post_exec"
218 # Read in system configuration file
221 # Validate configuration
222 [ "$system" = "localhost" -o "$system" = "127.0.0.1" ] && local=1
224 [ "$system" = "$fname" ] \
225 && systxt="\"$system\"" \
226 || systxt="\"$fname\" [\"$system\"]"
228 && echo "Working on $systxt ..." \
229 || echo "Working on $systxt (local system) ..."
231 count_all=$count_all+1
233 # Check target directory
234 if [ -z "$target" ]; then
235 echo "No target directory specified for \"$system\"!? Skipped!"
238 if [ ! -d "$target" ]; then
239 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
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!"
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."
260 echo "OK, host \"$system\" seems to be alive."
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
268 echo "Target directory \"$sys_target\" seems to be unclean!? \"$system\" skipped!"
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!"
280 sys_target="$sys_target/`date +%Y%m%d-%H%M%S`"
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!"
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
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!"
300 echo "Created new snapshot in \"$sys_target\"."
302 echo " *** Trial run, not creating new snapshot in \"$sys_target\"!"
305 # No old backup found, create new subvolume
306 if [ "$DRYRUN" -eq 0 ]; then
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!"
313 echo "Created new subvolume in \"$sys_target\"."
315 echo " *** Trial run, not creating new subvolume \"$sys_target\"!"
321 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
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!"
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"
346 && cmd="$cmd ${user}@${system}:/ $sys_target/" \
347 || cmd="$cmd / $sys_target/"
349 echo "Backing up to \"$sys_target\" ..."
350 echo -n "Start date: "; date
352 count_started=$count_started+1
355 if [ "$DRYRUN" -eq 0 ]; then
356 rm -f "$sys_target/.stamp"
357 $SHELL -c "$cmd"; ret=$?
358 echo "code=$ret" >"$sys_target/.stamp"
360 echo " *** Trial run, not executing synchronization command!"
364 if [ $ret -eq 20 ]; then
365 echo "Backup of \"$system\" interrupted. Aborting ..."
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
374 echo "System \"$system\" completed with status $ret, OK."
375 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
378 echo "System \"$system\" completed with ERRORS, code $ret!"
381 # execute job "post-exec" command, if any
382 if [ -n "$job_post_exec" ]; then
383 ExecJob post "$job_post_exec"
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"
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!?"
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
409 "$dir" >/dev/null 2>&1
411 echo "Failed to delete \"$dir\"!"
414 echo -n "Clean up finished: "; date
415 elif [ -n "$to_delete" ]; then
416 echo "There have been errors, not cleaning up old generations!"
418 echo "Nothing to clean up."
422 destinations="$destinations $target"
428 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
429 if [ "$DRYRUN" -eq 0 -a -n "$paths" ]; then
436 echo -n "Done: "; date
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."
445 if [ $count_started -ne $count_ok ]; then
446 echo "-----> THERE HAVE BEEN ERRORS! <-----"