3 # backup-script system for cloning systems using rsync
4 # Copyright (c)2008-2013 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 CONF_D="/etc/backup-script.d"
15 PIDFILE="/var/run/$NAME.pid"
22 declare -i count_all=0
23 declare -i count_started=0
25 declare -i count_ok_vanished=0
29 # Default settings, can be overwritten in backup-script.conf:
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 if [ ! -r "${CONF_D}/$s" ]; then
94 echo "$NAME: Can' read \"${CONF_D}/$s\"!"
97 sys="$sys ${CONF_D}/$s"
103 trap GotSignal SIGINT
105 echo -n "Started: "; date
108 [ -r "${CONF_D}/backup-script.conf" ] && source "${CONF_D}/backup-script.conf"
110 # check and create PID file
111 if [ -e "$PIDFILE" ]; then
112 echo "Lockfile \"$PIDFILE\" already exists."
113 echo "Is an other instance still running?"
115 echo -n "Aborted: "; date
119 touch "$PIDFILE" 2>/dev/null
120 if [ $? -ne 0 ]; then
121 echo "Warning: can't create PID file \"$PIDFILE\"!"
124 echo "$$" >>"$PIDFILE"
127 if [ -n "$pre_exec" ]; then
128 echo "Executing \"$pre_exec\" ..."
130 if [ $? -ne 0 ]; then
131 echo "Error: pre-exec command failed!"; echo
133 echo "Aborting backup."; echo
141 [ -r "$f" -a -f "$f" ] || continue
145 "backup-script.conf"|*.sh)
150 # Set global defaults
153 target="$default_target"
154 ssh_args_add="$default_ssh_args_add"
155 rsync_args_add="$default_rsync_args_add"
156 exclude_args_add="$default_exclude_args_add"
157 compress="$default_compress"
159 local="$default_local"
160 generations="$default_generations"
162 # Read in system configuration file
165 # Validate configuration
166 [ "$system" = "localhost" -o "$system" = "127.0.0.1" ] && local=1
168 [ "$system" = "$fname" ] \
169 && systxt="\"$system\"" \
170 || systxt="\"$fname\" [\"$system\"]"
172 && echo "Working on $systxt ..." \
173 || echo "Working on $sytxts (local system) ..."
175 count_all=$count_all+1
177 # Check target directory
178 if [ -z "$target" ]; then
179 echo "No target directory specified for \"$system\"!? Skipped!"
182 if [ ! -d "$target" ]; then
183 echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
187 sys_target="$target/$fname"
188 if [ "$DRYRUN" -eq 0 ]; then
189 mkdir -p "$sys_target" >/dev/null 2>&1
190 if [ $? -ne 0 ]; then
191 echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
196 if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
197 # Check if system is alive
198 ping -c 1 "$system" >/dev/null 2>&1
199 if [ $? -ne 0 ]; then
200 echo "Host \"$system\" seems not to be alive!? Skipped."
203 echo "OK, host \"$system\" seems to be alive."
206 if [ $generations -gt 0 ]; then
207 # Make sure no old backup is stored in system directory
208 if [ -e "$sys_target/.stamp" ]; then
209 # There seems to be a genearation-less backup in the
211 echo "Target directory \"$sys_target\" seems to be unclean!? \"$system\" skipped!"
215 # Search directory of last generation, if any
216 last="`ls -1 "$sys_target" 2>/dev/null | sort -r | head -n1`"
217 if [ -n "$last" ]; then
218 last="$sys_target/$last"
219 if [ ! -d "$last" ]; then
220 echo "Last snapshot \"$last\" seems not to be a directory!? \"$system\" skipped!"
224 sys_target="$sys_target/`date +%Y%m%d-%H%M%S`"
226 if [ -n "$last" -a ! -e "$last/.stamp" ]; then
227 # Old backup directory without "stamp file", continue
228 echo "Found incomplete snapshot in \"$last\", reusing and renaming it ..."
229 mv "$last" "$sys_target" >/dev/null 2>&1
230 if [ $? -ne 0 ]; then
231 echo "Failed to rename last snapshot \"$last\" to \"$sys_target\"!? \"$system\" skipped!"
234 elif [ -n "$last" ]; then
235 # Old backup directory found, create new snapshot
236 echo "Found last snapshot in \"$last\"."
237 if [ "$DRYRUN" -eq 0 ]; then
238 btrfs subvolume snapshot \
239 "$last" "$sys_target" >/dev/null 2>&1; r=$?
240 if [ $r -ne 0 ]; then
241 echo "Can't create btrfs snapshot \"$sys_target\" of \"$last\", code $r!? \"$system\" skipped!"
244 echo "Created new snapshot in \"$sys_target\"."
246 echo " *** Trial run, not creating new snapshot in \"$sys_target\"!"
249 # No old backup found, create new subvolume
250 if [ "$DRYRUN" -eq 0 ]; then
251 btrfs subvolume create \
252 "$sys_target" >/dev/null 2>&1; r=$?
253 if [ $r -ne 0 ]; then
254 echo "Can't create btrfs subvolume \"$sys_target\", code $r!? \"$system\" skipped!"
257 echo "Created new subvolume in \"$sys_target\"."
259 echo " *** Trial run, not creating new subvolume \"$sys_target\"!"
265 [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
267 cmd="rsync --archive"
268 [ "$compress" -ne 0 ] && cmd="$cmd --compress"
269 cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse"
270 [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
271 cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
272 cmd="$cmd --exclude=/run --exclude=/tmp --exclude=/var/tmp"
273 cmd="$cmd --exclude=/media --exclude=/mnt --exclude=/net"
274 cmd="$cmd --exclude=/var/cache/apt --exclude=/var/log"
275 [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
276 [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
279 && cmd="$cmd ${user}@${system}:/ $sys_target/" \
280 || cmd="$cmd / $sys_target/"
282 echo "Backing up to \"$sys_target\" ..."
283 echo -n "Start date: "; date
285 count_started=$count_started+1
287 if [ "$DRYRUN" -eq 0 ]; then
288 rm -f "$sys_target/.stamp"
289 $SHELL -c "$cmd"; ret=$?
290 echo "code=$ret" >"$sys_target/.stamp"
292 echo " *** Trial run, not executing synchronization command!"
296 if [ $ret -eq 20 ]; then
297 echo "Backup of \"$system\" interrupted. Aborting ..."
302 echo -n "End date: "; date
303 if [ $ret -eq 0 -o $ret -eq 24 ]; then
304 [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
306 echo "System \"$system\" completed with status $ret, OK."
307 [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
309 echo "System \"$system\" completed with ERRORS, code $ret!"
312 destinations="$destinations $target"
318 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
319 if [ "$DRYRUN" -eq 0 -a -n "$paths" ]; then
326 echo -n "Done: "; date
328 [ $count_all -eq 1 ] && s="" || s="s"
329 echo " - $count_all job$s defined,"
330 [ $count_started -eq 1 ] && s="" || s="s"
331 echo " - $count_started job$s started,"
332 echo " - $count_ok done without errors."
335 if [ $count_started -ne $count_ok ]; then
336 echo "-----> THERE HAVE BEEN ERRORS! <-----"