3 # backup-script system for cloning systems using rsync
4 # Copyright (c)2008-2014 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=""
43 echo "Usage: $NAME [<options>] [<system> [<system> [...]]]"
45 echo " -p, --progress Show progress, see rsync(1)."
46 echo " -n, --dry-run Test run only, don't copy any data."
48 echo "When no <system> is given, all defined systems are used."
54 if [ -n "$post_exec" ]; then
55 echo "Executing \"$post_exec\" ..."
58 echo "Warning: post-exec command failed!"
67 echo "--> Got break signal, cleaning up & aborting ..."
70 echo -n "Aborted: "; date
75 while [ $# -gt 0 ]; do
93 echo -n "Started: "; date
95 for conf in "/etc/backup-script.conf" "${conf_d}/backup-script.conf"; do
96 if [ -r "$conf" ]; then
97 echo "Reading configuration: \"$conf\" ..."
103 if [ $# -ge 1 ]; then
105 if [ ! -r "${conf_d}/$s" ]; then
106 echo "$NAME: Can' read \"${conf_d}/$s\"!"
109 sys="$sys ${conf_d}/$s"
115 # check and create PID file
116 if [ -e "$PIDFILE" ]; then
117 echo "Lockfile \"$PIDFILE\" already exists."
118 echo "Is an other instance still running?"
120 echo -n "Aborted: "; date
124 touch "$PIDFILE" 2>/dev/null
125 if [ $? -ne 0 ]; then
126 echo "Warning: can't create PID file \"$PIDFILE\"!"
129 echo "$$" >>"$PIDFILE"
132 if [ -n "$pre_exec" ]; then
133 echo "Executing \"$pre_exec\" ..."
135 if [ $? -ne 0 ]; then
136 echo "Error: pre-exec command failed!"; echo
138 echo "Aborting backup."; echo
146 [ -r "$f" -a -f "$f" ] || continue
150 "backup-script.conf"|*.sh)
155 # Set global defaults
158 target="$default_target"
159 ssh_args_add="$default_ssh_args_add"
160 rsync_args_add="$default_rsync_args_add"
161 exclude_args_add="$default_exclude_args_add"
162 compress="$default_compress"
164 local="$default_local"
165 generations="$default_generations"
167 # Read in system configuration file
170 # Validate configuration
171 [ "$system" = "localhost" -o "$system" = "127.0.0.1" ] && local=1
173 [ "$system" = "$fname" ] \
174 && systxt="\"$system\"" \
175 || systxt="\"$fname\" [\"$system\"]"
177 && echo "Working on $systxt ..." \
178 || echo "Working on $sytxts (local system) ..."
180 count_all=$count_all+1
182 # Check target directory
183 if [ -z "$target" ]; then
184 echo "No target directory specified for \"$system\"!? Skipped!"
187 if [ ! -d "$target" ]; then
188 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
192 sys_target="$target/$fname"
193 if [ "$DRYRUN" -eq 0 ]; then
194 mkdir -p "$sys_target" >/dev/null 2>&1
195 if [ $? -ne 0 ]; then
196 echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
201 if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
202 # Check if system is alive
203 ping -c 1 "$system" >/dev/null 2>&1
204 if [ $? -ne 0 ]; then
205 echo "Host \"$system\" seems not to be alive!? Skipped."
208 echo "OK, host \"$system\" seems to be alive."
211 if [ $generations -gt 0 ]; then
212 # Make sure no old backup is stored in system directory
213 if [ -e "$sys_target/.stamp" ]; then
214 # There seems to be a genearation-less backup in the
216 echo "Target directory \"$sys_target\" seems to be unclean!? \"$system\" skipped!"
220 # Search directory of last generation, if any
221 last="`ls -1 "$sys_target" 2>/dev/null | sort -r | head -n1`"
222 if [ -n "$last" ]; then
223 last="$sys_target/$last"
224 if [ ! -d "$last" ]; then
225 echo "Last snapshot \"$last\" seems not to be a directory!? \"$system\" skipped!"
229 sys_target="$sys_target/`date +%Y%m%d-%H%M%S`"
231 if [ -n "$last" -a ! -e "$last/.stamp" ]; then
232 # Old backup directory without "stamp file", continue
233 echo "Found incomplete snapshot in \"$last\", reusing and renaming it ..."
234 mv "$last" "$sys_target" >/dev/null 2>&1
235 if [ $? -ne 0 ]; then
236 echo "Failed to rename last snapshot \"$last\" to \"$sys_target\"!? \"$system\" skipped!"
239 elif [ -n "$last" ]; then
240 # Old backup directory found, create new snapshot
241 echo "Found last snapshot in \"$last\"."
242 if [ "$DRYRUN" -eq 0 ]; then
243 btrfs subvolume snapshot \
244 "$last" "$sys_target" >/dev/null 2>&1; r=$?
245 if [ $r -ne 0 ]; then
246 echo "Can't create btrfs snapshot \"$sys_target\" of \"$last\", code $r!? \"$system\" skipped!"
249 echo "Created new snapshot in \"$sys_target\"."
251 echo " *** Trial run, not creating new snapshot in \"$sys_target\"!"
254 # No old backup found, create new subvolume
255 if [ "$DRYRUN" -eq 0 ]; then
256 btrfs subvolume create \
257 "$sys_target" >/dev/null 2>&1; r=$?
258 if [ $r -ne 0 ]; then
259 echo "Can't create btrfs subvolume \"$sys_target\", code $r!? \"$system\" skipped!"
262 echo "Created new subvolume in \"$sys_target\"."
264 echo " *** Trial run, not creating new subvolume \"$sys_target\"!"
270 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
272 cmd="rsync --archive"
273 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
274 cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse"
275 [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
276 cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
277 cmd="$cmd --exclude=/run --exclude=/tmp --exclude=/var/tmp"
278 cmd="$cmd --exclude=/media --exclude=/mnt --exclude=/net"
279 cmd="$cmd --exclude=/var/cache/apt --exclude=/var/log"
280 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
281 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
284 && cmd="$cmd ${user}@${system}:/ $sys_target/" \
285 || cmd="$cmd / $sys_target/"
287 echo "Backing up to \"$sys_target\" ..."
288 echo -n "Start date: "; date
290 count_started=$count_started+1
293 if [ "$DRYRUN" -eq 0 ]; then
294 rm -f "$sys_target/.stamp"
295 $SHELL -c "$cmd"; ret=$?
296 echo "code=$ret" >"$sys_target/.stamp"
298 echo " *** Trial run, not executing synchronization command!"
302 if [ $ret -eq 20 ]; then
303 echo "Backup of \"$system\" interrupted. Aborting ..."
308 echo -n "End date: "; date
309 if [ $ret -eq 0 -o $ret -eq 24 ]; then
310 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
312 echo "System \"$system\" completed with status $ret, OK."
313 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
316 echo "System \"$system\" completed with ERRORS, code $ret!"
319 # Clean up old generations
320 if [ $generations -gt 0 ]; then
321 sys_target="$target/$fname"
322 to_delete=`ls -1t "$sys_target" 2>/dev/null | tail -n+$generations | sort`
323 if [ -n "$to_delete" -a $ok -eq 1 ]; then
324 [ "$DRYRUN" -eq 0 ] \
325 && echo "Deleting old backup generations:" \
326 || echo " *** Trial run, not deleting old generations:"
327 for delete in $to_delete; do
328 dir="$sys_target/$delete"
329 if [ ! -e "$dir/.stamp" ]; then
330 echo "Not deleting \"$dir\", not a backup directory!?"
333 last=`stat "$dir/.stamp" 2>/dev/null | grep "^Modify: " \
334 | cut -d':' -f2- | cut -d. -f1`
335 echo "Removing backup from" $last "..."
336 if [ "$DRYRUN" -eq 0 ]; then
337 btrfs subvolume delete \
338 "$dir" >/dev/null 2>&1
340 echo "Failed to delete \"$dir\"!"
343 echo -n "Clean up finished: "; date
344 elif [ -n "$to_delete" ]; then
345 echo "There have been errors, not cleaning up old generations!"
347 echo "Nothing to clean up."
351 destinations="$destinations $target"
357 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
358 if [ "$DRYRUN" -eq 0 -a -n "$paths" ]; then
365 echo -n "Done: "; date
367 [ $count_all -eq 1 ] && s="" || s="s"
368 echo " - $count_all job$s defined,"
369 [ $count_started -eq 1 ] && s="" || s="s"
370 echo " - $count_started job$s started,"
371 echo " - $count_ok done without errors."
374 if [ $count_started -ne $count_ok ]; then
375 echo "-----> THERE HAVE BEEN ERRORS! <-----"