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!"
98 while [ $# -gt 0 ]; do
114 trap GotSignal SIGINT
116 echo -n "Started: "; date
118 for conf in "/etc/backup-script.conf" "${conf_d}/backup-script.conf"; do
119 if [ -r "$conf" ]; then
120 echo "Reading configuration: \"$conf\" ..."
126 if [ $# -ge 1 ]; then
128 if [ ! -r "${conf_d}/$s" ]; then
129 echo "$NAME: Can' read \"${conf_d}/$s\"!"
132 sys="$sys ${conf_d}/$s"
138 # check and create PID file
139 if [ -e "$PIDFILE" ]; then
140 echo "Lockfile \"$PIDFILE\" already exists."
141 echo "Is an other instance still running?"
143 echo -n "Aborted: "; date
147 touch "$PIDFILE" 2>/dev/null
148 if [ $? -ne 0 ]; then
149 echo "Warning: can't create PID file \"$PIDFILE\"!"
152 echo "$$" >>"$PIDFILE"
155 if [ -n "$pre_exec" ]; then
156 echo "Executing \"$pre_exec\" ..."
158 if [ $? -ne 0 ]; then
159 echo "Error: pre-exec command failed!"; echo
161 echo "Aborting backup."; echo
169 [ -r "$f" -a -f "$f" ] || continue
173 "backup-script.conf"|*.sh)
178 # Set global defaults
181 target="$default_target"
182 ssh_args_add="$default_ssh_args_add"
183 rsync_args_add="$default_rsync_args_add"
184 exclude_args_add="$default_exclude_args_add"
185 compress="$default_compress"
187 local="$default_local"
188 generations="$default_generations"
189 job_pre_exec="$default_job_pre_exec"
190 job_post_exec="$default_job_post_exec"
192 # Read in system configuration file
195 # Validate configuration
196 [ "$system" = "localhost" -o "$system" = "127.0.0.1" ] && local=1
198 [ "$system" = "$fname" ] \
199 && systxt="\"$system\"" \
200 || systxt="\"$fname\" [\"$system\"]"
202 && echo "Working on $systxt ..." \
203 || echo "Working on $systxt (local system) ..."
205 count_all=$count_all+1
207 # Check target directory
208 if [ -z "$target" ]; then
209 echo "No target directory specified for \"$system\"!? Skipped!"
212 if [ ! -d "$target" ]; then
213 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
217 sys_target="$target/$fname"
218 sys_root="$sys_target"
219 if [ "$DRYRUN" -eq 0 ]; then
220 mkdir -p "$sys_target" >/dev/null 2>&1
221 if [ $? -ne 0 ]; then
222 echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
227 if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
228 # Check if system is alive
229 ping -c 1 "$system" >/dev/null 2>&1
230 if [ $? -ne 0 ]; then
231 echo "Host \"$system\" seems not to be alive!? Skipped."
234 echo "OK, host \"$system\" seems to be alive."
237 if [ $generations -gt 0 ]; then
238 # Make sure no old backup is stored in system directory
239 if [ -e "$sys_target/.stamp" ]; then
240 # There seems to be a genearation-less backup in the
242 echo "Target directory \"$sys_target\" seems to be unclean!? \"$system\" skipped!"
246 # Search directory of last generation, if any
247 last="`ls -1 "$sys_target" 2>/dev/null | sort -r | head -n1`"
248 if [ -n "$last" ]; then
249 last="$sys_target/$last"
250 if [ ! -d "$last" ]; then
251 echo "Last snapshot \"$last\" seems not to be a directory!? \"$system\" skipped!"
255 sys_target="$sys_target/`date +%Y%m%d-%H%M%S`"
257 if [ -n "$last" -a ! -e "$last/.stamp" ]; then
258 # Old backup directory without "stamp file", continue
259 echo "Found incomplete snapshot in \"$last\", reusing and renaming it ..."
260 mv "$last" "$sys_target" >/dev/null 2>&1
261 if [ $? -ne 0 ]; then
262 echo "Failed to rename last snapshot \"$last\" to \"$sys_target\"!? \"$system\" skipped!"
265 elif [ -n "$last" ]; then
266 # Old backup directory found, create new snapshot
267 echo "Found last snapshot in \"$last\"."
268 if [ "$DRYRUN" -eq 0 ]; then
269 btrfs subvolume snapshot \
270 "$last" "$sys_target" >/dev/null 2>&1; r=$?
271 if [ $r -ne 0 ]; then
272 echo "Can't create btrfs snapshot \"$sys_target\" of \"$last\", code $r!? \"$system\" skipped!"
275 echo "Created new snapshot in \"$sys_target\"."
277 echo " *** Trial run, not creating new snapshot in \"$sys_target\"!"
280 # No old backup found, create new subvolume
281 if [ "$DRYRUN" -eq 0 ]; then
282 btrfs subvolume create \
283 "$sys_target" >/dev/null 2>&1; r=$?
284 if [ $r -ne 0 ]; then
285 echo "Can't create btrfs subvolume \"$sys_target\", code $r!? \"$system\" skipped!"
288 echo "Created new subvolume in \"$sys_target\"."
290 echo " *** Trial run, not creating new subvolume \"$sys_target\"!"
296 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
298 # execute job "pre-exec" command, if any
299 if [ -n "$job_pre_exec" ]; then
300 ExecJob pre "$job_pre_exec" ; ret=$?
301 if [ $ret -ne 0 ]; then
302 [ $ret -ne 99 ] && count_started=$count_started+1
303 echo "Pre-exec command failed, \"$system\" skipped!"
308 # prepare (remote) command ...
309 cmd="rsync --archive"
310 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
311 cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse"
312 [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
313 cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
314 cmd="$cmd --exclude=/run --exclude=/tmp --exclude=/var/tmp"
315 cmd="$cmd --exclude=/media --exclude=/mnt --exclude=/net"
316 cmd="$cmd --exclude=/var/cache/apt --exclude=/var/log"
317 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
318 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
321 && cmd="$cmd ${user}@${system}:/ $sys_target/" \
322 || cmd="$cmd / $sys_target/"
324 echo "Backing up to \"$sys_target\" ..."
325 echo -n "Start date: "; date
327 count_started=$count_started+1
330 if [ "$DRYRUN" -eq 0 ]; then
331 rm -f "$sys_target/.stamp"
332 $SHELL -c "$cmd"; ret=$?
333 echo "code=$ret" >"$sys_target/.stamp"
335 echo " *** Trial run, not executing synchronization command!"
339 if [ $ret -eq 20 ]; then
340 echo "Backup of \"$system\" interrupted. Aborting ..."
345 echo -n "End date: "; date
346 if [ $ret -eq 0 -o $ret -eq 24 ]; then
347 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
349 echo "System \"$system\" completed with status $ret, OK."
350 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
353 echo "System \"$system\" completed with ERRORS, code $ret!"
356 # execute job "post-exec" command, if any
357 if [ -n "$job_post_exec" ]; then
358 ExecJob post "$job_post_exec"
361 if [ $generations -gt 0 ]; then
362 # Update "latest" symlink
363 if [ "$DRYRUN" -eq 0 ]; then
364 rm -f "$sys_root/latest"
365 ln -s "$sys_target" "$sys_root/latest"
367 # Clean up old generations
368 to_delete=`ls -1t "$sys_root" 2>/dev/null | tail -n+$generations | sort`
369 if [ -n "$to_delete" -a $ok -eq 1 ]; then
370 [ "$DRYRUN" -eq 0 ] \
371 && echo "Deleting old backup generations:" \
372 || echo " *** Trial run, not deleting old generations:"
373 for delete in $to_delete; do
374 dir="$sys_root/$delete"
375 if [ ! -e "$dir/.stamp" ]; then
376 echo "Not deleting \"$dir\", not a backup directory!?"
379 last=`stat "$dir/.stamp" 2>/dev/null | grep "^Modify: " \
380 | cut -d':' -f2- | cut -d. -f1`
381 echo "Removing backup from" $last "..."
382 if [ "$DRYRUN" -eq 0 ]; then
383 btrfs subvolume delete \
384 "$dir" >/dev/null 2>&1
386 echo "Failed to delete \"$dir\"!"
389 echo -n "Clean up finished: "; date
390 elif [ -n "$to_delete" ]; then
391 echo "There have been errors, not cleaning up old generations!"
393 echo "Nothing to clean up."
397 destinations="$destinations $target"
403 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
404 if [ "$DRYRUN" -eq 0 -a -n "$paths" ]; then
411 echo -n "Done: "; date
413 [ $count_all -eq 1 ] && s="" || s="s"
414 echo " - $count_all job$s defined,"
415 [ $count_started -eq 1 ] && s="" || s="s"
416 echo " - $count_started job$s started,"
417 echo " - $count_ok done without errors."
420 if [ $count_started -ne $count_ok ]; then
421 echo "-----> THERE HAVE BEEN ERRORS! <-----"