ccollect.sh 13.4 KB
Newer Older
Nico Schottelius's avatar
Nico Schottelius committed
1
#!/bin/sh
Nico Schottelius's avatar
Nico Schottelius committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# 
# 2005-2008 Nico Schottelius (nico-ccollect at schottelius.org)
# 
# This file is part of ccollect.
#
# ccollect is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# ccollect is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with ccollect. If not, see <http://www.gnu.org/licenses/>.
#
Nico Schottelius's avatar
Nico Schottelius committed
20
# Initially written for SyGroup (www.sygroup.ch)
Nico Schottelius's avatar
Nico Schottelius committed
21 22
# Date: Mon Nov 14 11:45:11 CET 2005

23
#
24
# where to find our configuration and temporary file
25
#
26
CCOLLECT_CONF=${CCOLLECT_CONF:-/etc/ccollect}
Nico Schottelius's avatar
Nico Schottelius committed
27 28 29 30
CSOURCES=${CCOLLECT_CONF}/sources
CDEFAULTS=${CCOLLECT_CONF}/defaults
CPREEXEC="${CDEFAULTS}/pre_exec"
CPOSTEXEC="${CDEFAULTS}/post_exec"
31

Nico Schottelius's avatar
Nico Schottelius committed
32
TMP=$(mktemp "/tmp/$(basename $0).XXXXXX")
33
VERSION=0.7.0
Nico Schottelius's avatar
Nico Schottelius committed
34
RELEASE="2008-03-XX"
Nico Schottelius's avatar
Nico Schottelius committed
35 36
HALF_VERSION="ccollect ${VERSION}"
FULL_VERSION="ccollect ${VERSION} (${RELEASE})"
37 38 39

#
# CDATE: how we use it for naming of the archives
40
# DDATE: how the user should see it in our output (DISPLAY)
41
#
Nico Schottelius's avatar
Nico Schottelius committed
42
CDATE="date +%Y%m%d-%H%M"
Nico Schottelius's avatar
Nico Schottelius committed
43
DDATE="date +%Y-%m-%d-%H:%M:%S"
44

45 46 47 48 49
#
# unset parallel execution
#
PARALLEL=""

50 51 52
#
# catch signals
#
Nico Schottelius's avatar
Nico Schottelius committed
53
trap "rm -f \"${TMP}\"" 1 2 15
Nico Schottelius's avatar
Nico Schottelius committed
54

Nico Schottelius's avatar
Nico Schottelius committed
55 56 57 58
#
# Functions
#

59 60 61
# time displaying echo
_techo()
{
62
   echo "$(${DDATE}): $@"
63 64 65
}

# exit on error
Nico Schottelius's avatar
Nico Schottelius committed
66 67
_exit_err()
{
68
   _techo "$@"
Nico Schottelius's avatar
Nico Schottelius committed
69
   rm -f "${TMP}"
Nico Schottelius's avatar
Nico Schottelius committed
70 71 72
   exit 1
}

73 74
add_name()
{
Nico Schottelius's avatar
Nico Schottelius committed
75
   sed "s:^:\[${name}\] :"
76 77
}

Nico Schottelius's avatar
Nico Schottelius committed
78 79
pcmd()
{
80 81
   if [ "$remote_host" ]; then
      ssh "$remote_host" "$@"
Nico Schottelius's avatar
Nico Schottelius committed
82 83 84 85 86
   else
      "$@"
   fi
}

Nico Schottelius's avatar
Nico Schottelius committed
87 88 89 90 91 92 93 94 95
#
# Version
#
display_version()
{
   echo "${FULL_VERSION}"
   exit 0
}

96
#
Nico Schottelius's avatar
Nico Schottelius committed
97
# Tell how to use us
98
#
Nico Schottelius's avatar
Nico Schottelius committed
99 100
usage()
{
101
   echo "$(basename $0): <interval name> [args] <sources to backup>"
Nico Schottelius's avatar
Nico Schottelius committed
102
   echo ""
Nico Schottelius's avatar
Nico Schottelius committed
103
   echo "   ccollect creates (pseudo) incremental backups"
Nico Schottelius's avatar
Nico Schottelius committed
104 105
   echo ""
   echo "   -h, --help:          Show this help screen"
106
   echo "   -p, --parallel:      Parallelise backup processes"
107
   echo "   -a, --all:           Backup all sources specified in ${CSOURCES}"
Nico Schottelius's avatar
Nico Schottelius committed
108
   echo "   -v, --verbose:       Be very verbose (uses set -x)"
Nico Schottelius's avatar
Nico Schottelius committed
109
   echo "   -V, --version:       Print version information"
Nico Schottelius's avatar
Nico Schottelius committed
110
   echo ""
111
   echo "   This is version ${VERSION}, released on ${RELEASE}"
112
   echo "   (the first version was written on 2005-12-05 by Nico Schottelius)."
113
   echo ""
Nico Schottelius's avatar
Nico Schottelius committed
114
   echo "   Retrieve latest ccollect at http://unix.schottelius.org/ccollect/"
Nico Schottelius's avatar
Nico Schottelius committed
115 116 117
   exit 0
}

118
#
119
# need at least interval and one source or --all
120 121
#
if [ $# -lt 2 ]; then
Nico Schottelius's avatar
Nico Schottelius committed
122 123 124 125 126
   if [ "$1" = "-V" -o "$1" = "--version" ]; then
      display_version
   else
      usage
   fi
127
fi
128

129
#
130
# check for configuraton directory
131
#
132
[ -d "${CCOLLECT_CONF}" ] || _exit_err "No configuration found in " \
Nico Schottelius's avatar
Nico Schottelius committed
133
   "\"${CCOLLECT_CONF}\" (is \$CCOLLECT_CONF properly set?)"
134

Nico Schottelius's avatar
Nico Schottelius committed
135 136 137
#
# Filter arguments
#
Nico Schottelius's avatar
Nico Schottelius committed
138
export INTERVAL="$1"; shift
Nico Schottelius's avatar
Nico Schottelius committed
139
i=1
140
no_sources=0
Nico Schottelius's avatar
Nico Schottelius committed
141

142 143 144
#
# Create source "array"
#
145
while [ "$#" -ge 1 ]; do
146
   eval arg=\"\$1\"; shift
Nico Schottelius's avatar
Nico Schottelius committed
147

Nico Schottelius's avatar
Nico Schottelius committed
148 149 150
   if [ "${NO_MORE_ARGS}" = 1 ]; then
        eval source_${no_sources}=\"${arg}\"
        no_sources=$((${no_sources}+1))
Nico Schottelius's avatar
Nico Schottelius committed
151 152 153
        
        # make variable available for subscripts
        eval export source_${no_sources}
154
   else
Nico Schottelius's avatar
Nico Schottelius committed
155
      case "${arg}" in
156 157 158
         -a|--all)
            ALL=1
            ;;
Nico Schottelius's avatar
Nico Schottelius committed
159 160 161
         -v|--verbose)
            VERBOSE=1
            ;;
162
         -p|--parallel)
163
            PARALLEL=1
164 165 166 167 168 169 170 171
            ;;
         -h|--help)
            usage
            ;;
         --)
            NO_MORE_ARGS=1
            ;;
         *)
172 173
            eval source_${no_sources}=\"$arg\"
            no_sources=$(($no_sources+1))
174 175 176
            ;;
      esac
   fi
Nico Schottelius's avatar
Nico Schottelius committed
177

178
   i=$(($i+1))
Nico Schottelius's avatar
Nico Schottelius committed
179 180
done

Nico Schottelius's avatar
Nico Schottelius committed
181 182 183
# also export number of sources
export no_sources

Nico Schottelius's avatar
Nico Schottelius committed
184
#
Nico Schottelius's avatar
Nico Schottelius committed
185
# be really, really, really verbose
Nico Schottelius's avatar
Nico Schottelius committed
186
#
Nico Schottelius's avatar
Nico Schottelius committed
187
if [ "${VERBOSE}" = 1 ]; then
Nico Schottelius's avatar
Nico Schottelius committed
188 189
   set -x
fi
190

Nico Schottelius's avatar
Nico Schottelius committed
191 192 193
#
# Look, if we should take ALL sources
#
Nico Schottelius's avatar
Nico Schottelius committed
194
if [ "${ALL}" = 1 ]; then
Nico Schottelius's avatar
Nico Schottelius committed
195
   # reset everything specified before
196
   no_sources=0
Nico Schottelius's avatar
Nico Schottelius committed
197

198 199 200
   #
   # get entries from sources
   #
Nico Schottelius's avatar
Nico Schottelius committed
201
   cwd=$(pwd -P)
202
   ( cd "${CSOURCES}" && ls > "${TMP}" ); ret=$?
203

204
   [ "${ret}" -eq 0 ] || _exit_err "Listing of sources failed. Aborting."
Nico Schottelius's avatar
Nico Schottelius committed
205

206
   while read tmp; do
Nico Schottelius's avatar
Nico Schottelius committed
207 208
      eval source_${no_sources}=\"${tmp}\"
      no_sources=$((${no_sources}+1))
209
   done < "${TMP}"
Nico Schottelius's avatar
Nico Schottelius committed
210 211
fi

212 213 214 215 216 217 218 219 220
#
# Need at least ONE source to backup
#
if [ "${no_sources}" -lt 1 ]; then
   usage
else
   _techo "${HALF_VERSION}: Beginning backup using interval ${INTERVAL}"
fi

221 222 223 224
#
# Look for pre-exec command (general)
#
if [ -x "${CPREEXEC}" ]; then
Nico Schottelius's avatar
Nico Schottelius committed
225
   _techo "Executing ${CPREEXEC} ..."
226
   "${CPREEXEC}"; ret=$?
227
   _techo "Finished ${CPREEXEC} (return code: ${ret})."
228

229
   [ "${ret}" -eq 0 ] || _exit_err "${CPREEXEC} failed. Aborting"
230 231
fi

232 233 234 235
#
# check default configuration
#

236 237
D_FILE_INTERVAL="${CDEFAULTS}/intervals/${INTERVAL}"
D_INTERVAL=$(cat "${D_FILE_INTERVAL}" 2>/dev/null)
238

239

240 241 242 243
#
# Let's do the backup
#
i=0
Nico Schottelius's avatar
Nico Schottelius committed
244
while [ "${i}" -lt "${no_sources}" ]; do
Nico Schottelius's avatar
Nico Schottelius committed
245

246
   #
247
   # Get current source
248
   #
Nico Schottelius's avatar
Nico Schottelius committed
249
   eval name=\"\$source_${i}\"
Nico Schottelius's avatar
Nico Schottelius committed
250
   i=$((${i}+1))
251 252 253

   export name

254 255 256
   #
   # start ourself, if we want parallel execution
   #
257 258
   if [ "${PARALLEL}" ]; then
      "$0" "${INTERVAL}" "${name}" &
259 260 261
      continue
   fi

262 263 264 265 266 267 268 269 270
#
# Start subshell for easy log editing
#
(
   #
   # Stderr to stdout, so we can produce nice logs
   #
   exec 2>&1

271
   #
272
   # Configuration
273
   #
274 275 276 277 278 279 280 281 282 283
   backup="${CSOURCES}/${name}"
   c_source="${backup}/source"
   c_dest="${backup}/destination"
   c_exclude="${backup}/exclude"
   c_verbose="${backup}/verbose"
   c_vverbose="${backup}/very_verbose"
   c_rsync_extra="${backup}/rsync_options"
   c_summary="${backup}/summary"
   c_pre_exec="${backup}/pre_exec"
   c_post_exec="${backup}/post_exec"
284
   c_incomplete="${backup}/delete_incomplete"
Nico Schottelius's avatar
Nico Schottelius committed
285
   c_remote_host="${backup}/remote_host"
286 287 288 289 290

   #
   # Marking backups: If we abort it's not removed => Backup is broken
   #
   c_marker=".ccollect-marker"
291

Nico Schottelius's avatar
Nico Schottelius committed
292
   #
293
   # Times
Nico Schottelius's avatar
Nico Schottelius committed
294
   #
295 296
   begin_s=$(date +%s)

Nico Schottelius's avatar
Nico Schottelius committed
297 298 299 300 301 302 303 304
   #
   # unset possible options
   #
   EXCLUDE=""
   RSYNC_EXTRA=""
   SUMMARY=""
   VERBOSE=""
   VVERBOSE=""
305
   DELETE_INCOMPLETE=""
Nico Schottelius's avatar
Nico Schottelius committed
306

307
   _techo "Beginning to backup"
308

309 310 311
   #
   # Standard configuration checks
   #
Nico Schottelius's avatar
Nico Schottelius committed
312
   if [ ! -e "${backup}" ]; then
Nico Schottelius's avatar
Nico Schottelius committed
313
      _exit_err "Source does not exist."
314
   fi
Nico Schottelius's avatar
Nico Schottelius committed
315

Nico Schottelius's avatar
Nico Schottelius committed
316 317 318
   #
   # configuration _must_ be a directory
   #
Nico Schottelius's avatar
Nico Schottelius committed
319
   if [ ! -d "${backup}" ]; then
Nico Schottelius's avatar
Nico Schottelius committed
320
      _exit_err "\"${name}\" is not a cconfig-directory. Skipping."
321
   fi
322

Nico Schottelius's avatar
Nico Schottelius committed
323 324 325 326
   #
   # first execute pre_exec, which may generate destination or other
   # parameters
   #
Nico Schottelius's avatar
Nico Schottelius committed
327
   if [ -x "${c_pre_exec}" ]; then
328
      _techo "Executing ${c_pre_exec} ..."
Nico Schottelius's avatar
Nico Schottelius committed
329
      "${c_pre_exec}"; ret="$?"
330
      _techo "Finished ${c_pre_exec} (return code ${ret})."
Nico Schottelius's avatar
Nico Schottelius committed
331

Nico Schottelius's avatar
Nico Schottelius committed
332
      if [ "${ret}" -ne 0 ]; then
333
         _exit_err "${c_pre_exec} failed. Skipping."
Nico Schottelius's avatar
Nico Schottelius committed
334 335 336
      fi
   fi

337
   #
338
   # interval definition: First try source specific, fallback to default
339
   #
Nico Schottelius's avatar
Nico Schottelius committed
340
   c_interval="$(cat "${backup}/intervals/${INTERVAL}" 2>/dev/null)"
341

Nico Schottelius's avatar
Nico Schottelius committed
342 343
   if [ -z "${c_interval}" ]; then
      c_interval="${D_INTERVAL}"
344

Nico Schottelius's avatar
Nico Schottelius committed
345
      if [ -z "${c_interval}" ]; then
346
         _exit_err "No definition for interval \"${INTERVAL}\" found. Skipping."
347 348 349 350
      fi
   fi

   #
Nico Schottelius's avatar
Nico Schottelius committed
351
   # Source checks
352
   #
Nico Schottelius's avatar
Nico Schottelius committed
353
   if [ ! -f "${c_source}" ]; then
354
      _exit_err "Source description \"${c_source}\" is not a file. Skipping."
355
   else
Nico Schottelius's avatar
Nico Schottelius committed
356 357
      source=$(cat "${c_source}"); ret="$?"
      if [ "${ret}" -ne 0 ]; then
358
         _exit_err "Source ${c_source} is not readable. Skipping."
359
      fi
360
   fi
361

Nico Schottelius's avatar
Nico Schottelius committed
362
   #
Nico Schottelius's avatar
Nico Schottelius committed
363
   # Destination is a path
Nico Schottelius's avatar
Nico Schottelius committed
364
   #
365 366 367
   if [ ! -f "${c_dest}" ]; then
      _exit_err "Destination ${c_dest} is not a file. Skipping."
   else
Nico Schottelius's avatar
Nico Schottelius committed
368
      ddir=$(cat "${c_dest}"); ret="$?"
369 370 371
      if [ "${ret}" -ne 0 ]; then
         _exit_err "Destination ${c_dest} is not readable. Skipping."
      fi
372
   fi
Nico Schottelius's avatar
Nico Schottelius committed
373

Nico Schottelius's avatar
Nico Schottelius committed
374 375 376 377 378 379 380 381 382 383 384
   #
   # do we backup to a remote host? then set pre-cmd
   #
   if [ -f "${c_remote_host}" ]; then
      # adjust ls and co
      remote_host=$(cat "${c_remote_host}"); ret="$?"
      if [ "${ret}" -ne 0 ]; then
         _exit_err "Remote host file ${c_remote_host} exists, but is not readable. Skipping."
      fi
      destination="${remote_host}:${ddir}"
   else
385
      remote_host=""
Nico Schottelius's avatar
Nico Schottelius committed
386 387
      destination="${ddir}"
   fi
388
   export remote_host
Nico Schottelius's avatar
Nico Schottelius committed
389 390 391 392

   #
   # check for existence / use real name
   #
393
   pcmd cd "$ddir" || _exit_err "Cannot change to ${ddir}. Skipping."
Nico Schottelius's avatar
Nico Schottelius committed
394 395


396 397 398 399 400 401 402
   #
   # Check whether to delete incomplete backups
   #
   if [ -f "${c_incomplete}" ]; then
      DELETE_INCOMPLETE="yes"
   fi

Nico Schottelius's avatar
Nico Schottelius committed
403
   # NEW method as of 0.6:
404 405 406
   # - insert ccollect default parameters
   # - insert options
   # - insert user options
407
   
Nico Schottelius's avatar
Nico Schottelius committed
408 409 410
   #
   # rsync standard options
   #
411

Nico Schottelius's avatar
Nico Schottelius committed
412
   set -- "$@" "--archive" "--delete" "--numeric-ids" "--relative"   \
413
               "--delete-excluded" "--sparse" 
Nico Schottelius's avatar
Nico Schottelius committed
414

415
   #
Nico Schottelius's avatar
Nico Schottelius committed
416
   # exclude list
417
   #
418
   if [ -f "${c_exclude}" ]; then
Nico Schottelius's avatar
Nico Schottelius committed
419
      set -- "$@" "--exclude-from=${c_exclude}"
420
   fi
Nico Schottelius's avatar
Nico Schottelius committed
421

Nico Schottelius's avatar
Nico Schottelius committed
422
   #
Nico Schottelius's avatar
Nico Schottelius committed
423
   # Output a summary
Nico Schottelius's avatar
Nico Schottelius committed
424
   #
Nico Schottelius's avatar
Nico Schottelius committed
425
   if [ -f "${c_summary}" ]; then
Nico Schottelius's avatar
Nico Schottelius committed
426
      set -- "$@" "--stats"
Nico Schottelius's avatar
Nico Schottelius committed
427
   fi
Nico Schottelius's avatar
Nico Schottelius committed
428

Nico Schottelius's avatar
Nico Schottelius committed
429
   #
Nico Schottelius's avatar
Nico Schottelius committed
430
   # Verbosity for rsync
Nico Schottelius's avatar
Nico Schottelius committed
431
   #
Nico Schottelius's avatar
Nico Schottelius committed
432 433 434 435
   if [ -f "${c_vverbose}" ]; then
      set -- "$@" "-vv"
   elif [ -f "${c_verbose}" ]; then
      set -- "$@" "-v"
Nico Schottelius's avatar
Nico Schottelius committed
436
   fi
Nico Schottelius's avatar
Nico Schottelius committed
437

Nico Schottelius's avatar
Nico Schottelius committed
438
   #
Nico Schottelius's avatar
Nico Schottelius committed
439
   # extra options for rsync provided by the user
Nico Schottelius's avatar
Nico Schottelius committed
440
   #
Nico Schottelius's avatar
Nico Schottelius committed
441 442 443 444
   if [ -f "${c_rsync_extra}" ]; then
      while read line; do
         set -- "$@" "$line"
      done < "${c_rsync_extra}"
445
   fi
Nico Schottelius's avatar
Nico Schottelius committed
446

Nico Schottelius's avatar
Nico Schottelius committed
447 448 449
   #
   # Check for incomplete backups
   #
450
   pcmd ls -1 "$ddir/${INTERVAL}"*".${c_marker}" > "${TMP}" 2>/dev/null
Nico Schottelius's avatar
Nico Schottelius committed
451

452
   i=0
Nico Schottelius's avatar
Nico Schottelius committed
453
   while read incomplete; do
454 455 456 457 458 459
      eva incomplete_$i=$(echo ${incomplete} | sed "s/\\.${c_marker}\$//")
      i=$(($i+1))
   done < "${TMP}"

   while [ $i -gt 0 ]; do
      eval realincomplete=\"incomplete_$i\"
Nico Schottelius's avatar
Nico Schottelius committed
460 461 462
      _techo "Incomplete backup: ${realincomplete}"
      if [ "${DELETE_INCOMPLETE}" = "yes" ]; then
         _techo "Deleting ${realincomplete} ..."
463
         pcmd rm $VVERBOSE -rf "${ddir}/${realincomplete}" || \
464
            _exit_err "Removing ${realincomplete} failed."
Nico Schottelius's avatar
Nico Schottelius committed
465
      fi
466
   done
467

468
   #
469
   # check if maximum number of backups is reached, if so remove
Nico Schottelius's avatar
Nico Schottelius committed
470
   # use grep and ls -p so we only look at directories
471
   #
472
   count=$(pcmd ls -p1 "${ddir}" | grep "^${INTERVAL}\..*/\$" | wc -l \
Nico Schottelius's avatar
Nico Schottelius committed
473 474 475 476 477 478 479
      | sed 's/^ *//g')  || _exit_err "Counting backups failed"

   _techo "Existing backups: ${count} Total keeping backups: ${c_interval}"
   
   if [ "${count}" -ge "${c_interval}" ]; then
      substract=$((${c_interval} - 1))
      remove=$((${count} - ${substract}))
Nico Schottelius's avatar
Nico Schottelius committed
480
      _techo "Removing ${remove} backup(s)..."
Nico Schottelius's avatar
Nico Schottelius committed
481

482 483
      pcmd ls -p1 "$ddir" | grep "^${INTERVAL}\..*/\$" | \
        sort -n | head -n "${remove}" > "${TMP}"
484

485
      i=0
486
      while read to_remove; do
487 488 489 490 491 492
         eval remove_$i=\"${to_remove}\"
         i=$(($i+1))
      done < "${TMP}"

      while [ $i -gt 0 ]; do
         eval to_remove=\"remove_$i\"
493
         _techo "Removing ${to_remove} ..."
494
         pcmd echo rm ${VVERBOSE} -rf "${to_remove}" || \
495
            _exit_err "Removing ${to_remove} failed."
496
      done
Nico Schottelius's avatar
Nico Schottelius committed
497 498
   fi

499

Nico Schottelius's avatar
Nico Schottelius committed
500
   #
Nico Schottelius's avatar
Nico Schottelius committed
501 502 503 504
   # Check for backup directory to clone from: Always clone from the latest one!
   #
   # Use ls -1c instead of -1t, because last modification maybe the same on all
   # and metadate update (-c) is updated by rsync locally.
Nico Schottelius's avatar
Nico Schottelius committed
505
   #
506
   last_dir="$(pcmd ls -tcp1 "${ddir}" | grep '/$' | head -n 1)" || \
Nico Schottelius's avatar
Nico Schottelius committed
507
      _exit_err "Failed to list contents of ${ddir}."
508
   
509
   #
510
   # clone from old backup, if existing
511
   #
512 513 514
   if [ "${last_dir}" ]; then
      set -- "$@" "--link-dest=${last_dir}"
      _techo "Hard linking from ${last_dir}"
515 516 517
   fi
      

518 519
   # set time when we really begin to backup, not when we began to remove above
   destination_date=$(${CDATE})
520 521
   destination_dir="${ddir}/${INTERVAL}.${destination_date}.$$"
   destination_full="${destination}/${INTERVAL}.${destination_date}.$$"
Nico Schottelius's avatar
Nico Schottelius committed
522

523
   # give some info
524
   _techo "Beginning to backup, this may take some time..."
525

Nico Schottelius's avatar
Nico Schottelius committed
526
   _techo "Creating ${destination_dir} ..."
527
   pcmd mkdir ${VVERBOSE} "${destination_dir}" || \
528
      _exit_err "Creating ${destination_dir} failed. Skipping."
529

530
   #
531
   # added marking in 0.6 (and remove it, if successful later)
532
   #
533
   pcmd touch "${destination_dir}.${c_marker}"
Nico Schottelius's avatar
Nico Schottelius committed
534

Nico Schottelius's avatar
Nico Schottelius committed
535 536 537
   #
   # the rsync part
   #
Nico Schottelius's avatar
Nico Schottelius committed
538

Nico Schottelius's avatar
Nico Schottelius committed
539
   _techo "Transferring files..."
540
   rsync "$@" "${source}" "${destination_full}"; ret=$?
541

542
   #
Nico Schottelius's avatar
Nico Schottelius committed
543
   # remove marking here
544
   #
545 546
   pcmd rm "${destination_dir}.${c_marker}" || \
      _exit_err "Removing ${destination_dir}/${c_marker} failed."
Nico Schottelius's avatar
Nico Schottelius committed
547 548

   _techo "Finished backup (rsync return code: $ret)."
Nico Schottelius's avatar
Nico Schottelius committed
549
   if [ "${ret}" -ne 0 ]; then
550 551
      _techo "Warning: rsync exited non-zero, the backup may be broken (see rsync errors)."
   fi
552

553 554 555
   #
   # post_exec
   #
Nico Schottelius's avatar
Nico Schottelius committed
556 557 558 559
   if [ -x "${c_post_exec}" ]; then
      _techo "Executing ${c_post_exec} ..."
      "${c_post_exec}"; ret=$?
      _techo "Finished ${c_post_exec}."
560

Nico Schottelius's avatar
Nico Schottelius committed
561 562
      if [ ${ret} -ne 0 ]; then
         _exit_err "${c_post_exec} failed."
563
      fi
564
   fi
565

Nico Schottelius's avatar
Nico Schottelius committed
566
   # Calculation
567 568
   end_s=$(date +%s)

Nico Schottelius's avatar
Nico Schottelius committed
569
   full_seconds=$((${end_s} - ${begin_s}))
570 571 572
   hours=$((${full_seconds} / 3600))
   seconds=$((${full_seconds} - (${hours} * 3600)))
   minutes=$((${seconds} / 60))
Nico Schottelius's avatar
Nico Schottelius committed
573
   seconds=$((${seconds} - (${minutes} * 60)))
574

Nico Schottelius's avatar
Nico Schottelius committed
575
   _techo "Backup lasted: ${hours}:${minutes}:${seconds} (h:m:s)"
576

577
) | add_name
578
done
579

Nico Schottelius's avatar
Nico Schottelius committed
580 581 582
#
# Be a good parent and wait for our children, if they are running wild parallel
#
Nico Schottelius's avatar
Nico Schottelius committed
583
if [ "${PARALLEL}" ]; then
584
   _techo "Waiting for children to complete..."
Nico Schottelius's avatar
Nico Schottelius committed
585 586 587
   wait
fi

588 589 590
#
# Look for post-exec command (general)
#
Nico Schottelius's avatar
Nico Schottelius committed
591 592
if [ -x "${CPOSTEXEC}" ]; then
   _techo "Executing ${CPOSTEXEC} ..."
593 594
   "${CPOSTEXEC}"; ret=$?
   _techo "Finished ${CPOSTEXEC} (return code: ${ret})."
Nico Schottelius's avatar
Nico Schottelius committed
595

Nico Schottelius's avatar
Nico Schottelius committed
596
   if [ ${ret} -ne 0 ]; then
Nico Schottelius's avatar
typo  
Nico Schottelius committed
597
      _techo "${CPOSTEXEC} failed."
598
   fi
599 600
fi

Nico Schottelius's avatar
Nico Schottelius committed
601 602
rm -f "${TMP}"
_techo "Finished ${WE}"