2005-11-17 11:33:49 +00:00
|
|
|
#!/bin/sh
|
|
|
|
# Nico Schottelius
|
2007-08-14 12:33:22 +00:00
|
|
|
# Initially written for SyGroup (www.sygroup.ch)
|
2005-11-17 11:33:49 +00:00
|
|
|
# Date: Mon Nov 14 11:45:11 CET 2005
|
2006-01-13 22:10:23 +00:00
|
|
|
# Last Modified: (See ls -l or git)
|
2005-11-17 11:33:49 +00:00
|
|
|
|
2005-12-06 14:07:13 +00:00
|
|
|
#
|
2005-12-06 14:55:07 +00:00
|
|
|
# where to find our configuration and temporary file
|
2005-12-06 14:07:13 +00:00
|
|
|
#
|
2005-11-17 15:52:44 +00:00
|
|
|
CCOLLECT_CONF=${CCOLLECT_CONF:-/etc/ccollect}
|
2007-08-16 18:17:54 +00:00
|
|
|
CSOURCES=${CCOLLECT_CONF}/sources
|
|
|
|
CDEFAULTS=${CCOLLECT_CONF}/defaults
|
|
|
|
CPREEXEC="${CDEFAULTS}/pre_exec"
|
|
|
|
CPOSTEXEC="${CDEFAULTS}/post_exec"
|
2006-01-22 10:13:22 +00:00
|
|
|
|
2007-08-16 18:17:54 +00:00
|
|
|
TMP=$(mktemp "/tmp/$(basename $0).XXXXXX")
|
2007-08-27 16:56:03 +00:00
|
|
|
VERSION=0.6.2
|
2007-08-20 16:21:54 +00:00
|
|
|
RELEASE="2007-08-20"
|
2007-08-16 18:17:54 +00:00
|
|
|
HALF_VERSION="ccollect ${VERSION}"
|
|
|
|
FULL_VERSION="ccollect ${VERSION} (${RELEASE})"
|
2006-04-29 21:46:29 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# CDATE: how we use it for naming of the archives
|
2007-08-14 13:26:20 +00:00
|
|
|
# DDATE: how the user should see it in our output (DISPLAY)
|
2006-04-29 21:46:29 +00:00
|
|
|
#
|
2007-08-16 18:17:54 +00:00
|
|
|
CDATE="date +%Y%m%d-%H%M"
|
2007-08-14 12:33:22 +00:00
|
|
|
DDATE="date +%Y-%m-%d-%H:%M:%S"
|
2005-12-06 15:36:28 +00:00
|
|
|
|
2006-01-09 12:24:27 +00:00
|
|
|
#
|
|
|
|
# unset parallel execution
|
|
|
|
#
|
|
|
|
PARALLEL=""
|
|
|
|
|
2005-12-06 14:55:07 +00:00
|
|
|
#
|
|
|
|
# catch signals
|
|
|
|
#
|
2007-08-16 18:17:54 +00:00
|
|
|
trap "rm -f \"${TMP}\"" 1 2 15
|
2005-11-17 11:33:49 +00:00
|
|
|
|
2007-01-20 15:41:38 +00:00
|
|
|
#
|
|
|
|
# Functions
|
|
|
|
#
|
|
|
|
|
2007-08-14 13:26:20 +00:00
|
|
|
# time displaying echo
|
|
|
|
_techo()
|
|
|
|
{
|
2007-08-16 18:31:30 +00:00
|
|
|
echo "$(${DDATE}): $@"
|
2007-08-14 13:26:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# exit on error
|
2007-01-20 15:41:38 +00:00
|
|
|
_exit_err()
|
|
|
|
{
|
2007-08-14 13:26:20 +00:00
|
|
|
_techo "$@"
|
2007-08-16 18:17:54 +00:00
|
|
|
rm -f "${TMP}"
|
2007-01-20 15:41:38 +00:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2006-01-09 12:56:09 +00:00
|
|
|
add_name()
|
|
|
|
{
|
2007-08-16 18:17:54 +00:00
|
|
|
sed "s:^:\[${name}\] :"
|
2006-01-09 12:56:09 +00:00
|
|
|
}
|
|
|
|
|
2007-08-17 22:14:10 +00:00
|
|
|
#
|
|
|
|
# Version
|
|
|
|
#
|
|
|
|
display_version()
|
|
|
|
{
|
|
|
|
echo "${FULL_VERSION}"
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
2005-11-17 15:52:44 +00:00
|
|
|
#
|
2005-12-06 12:45:37 +00:00
|
|
|
# Tell how to use us
|
2005-11-17 15:52:44 +00:00
|
|
|
#
|
2005-12-06 12:45:37 +00:00
|
|
|
usage()
|
|
|
|
{
|
2006-04-25 13:35:56 +00:00
|
|
|
echo "$(basename $0): <interval name> [args] <sources to backup>"
|
2005-12-06 12:45:37 +00:00
|
|
|
echo ""
|
2006-01-24 21:36:28 +00:00
|
|
|
echo " ccollect creates (pseudo) incremental backups"
|
2005-12-06 12:45:37 +00:00
|
|
|
echo ""
|
|
|
|
echo " -h, --help: Show this help screen"
|
2006-01-24 21:58:39 +00:00
|
|
|
echo " -p, --parallel: Parallelise backup processes"
|
2007-03-13 07:31:50 +00:00
|
|
|
echo " -a, --all: Backup all sources specified in ${CSOURCES}"
|
2007-03-13 07:47:04 +00:00
|
|
|
echo " -v, --verbose: Be very verbose (uses set -x)"
|
2007-08-17 22:14:10 +00:00
|
|
|
echo " -V, --version: Print version information"
|
2005-12-06 12:45:37 +00:00
|
|
|
echo ""
|
2007-03-13 07:31:50 +00:00
|
|
|
echo " This is version ${VERSION}, released on ${RELEASE}"
|
2006-04-28 14:43:41 +00:00
|
|
|
echo " (the first version was written on 2005-12-05 by Nico Schottelius)."
|
2006-01-24 21:58:39 +00:00
|
|
|
echo ""
|
2006-10-15 19:56:55 +00:00
|
|
|
echo " Retrieve latest ccollect at http://unix.schottelius.org/ccollect/"
|
2005-12-06 12:45:37 +00:00
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
2005-12-07 16:41:20 +00:00
|
|
|
#
|
2006-04-25 13:35:56 +00:00
|
|
|
# need at least interval and one source or --all
|
2005-12-07 16:41:20 +00:00
|
|
|
#
|
|
|
|
if [ $# -lt 2 ]; then
|
2007-08-17 22:14:10 +00:00
|
|
|
if [ "$1" = "-V" -o "$1" = "--version" ]; then
|
|
|
|
display_version
|
|
|
|
else
|
|
|
|
usage
|
|
|
|
fi
|
2005-12-07 16:41:20 +00:00
|
|
|
fi
|
2005-12-06 15:36:28 +00:00
|
|
|
|
2005-12-08 11:18:44 +00:00
|
|
|
#
|
2007-03-13 07:31:50 +00:00
|
|
|
# check for configuraton directory
|
2005-12-08 11:18:44 +00:00
|
|
|
#
|
2007-03-13 07:31:50 +00:00
|
|
|
[ -d "${CCOLLECT_CONF}" ] || _exit_err "No configuration found in " \
|
2007-08-16 18:17:54 +00:00
|
|
|
"\"${CCOLLECT_CONF}\" (is \$CCOLLECT_CONF properly set?)"
|
2005-12-08 11:18:44 +00:00
|
|
|
|
2005-12-06 12:45:37 +00:00
|
|
|
#
|
|
|
|
# Filter arguments
|
|
|
|
#
|
2007-05-31 09:52:33 +00:00
|
|
|
export INTERVAL="$1"; shift
|
2005-12-06 12:45:37 +00:00
|
|
|
i=1
|
2006-04-29 10:58:57 +00:00
|
|
|
no_sources=0
|
2005-12-06 12:45:37 +00:00
|
|
|
|
2007-08-14 12:41:36 +00:00
|
|
|
#
|
|
|
|
# Create source "array"
|
|
|
|
#
|
2007-08-14 13:26:20 +00:00
|
|
|
while [ "$#" -ge 1 ]; do
|
2007-08-14 13:30:00 +00:00
|
|
|
eval arg=\"\$1\"; shift
|
2006-10-25 12:48:48 +00:00
|
|
|
|
2007-08-16 18:17:54 +00:00
|
|
|
if [ "${NO_MORE_ARGS}" = 1 ]; then
|
|
|
|
eval source_${no_sources}=\"${arg}\"
|
|
|
|
no_sources=$((${no_sources}+1))
|
2007-05-31 09:55:37 +00:00
|
|
|
|
|
|
|
# make variable available for subscripts
|
|
|
|
eval export source_${no_sources}
|
2005-12-06 14:07:13 +00:00
|
|
|
else
|
2007-08-16 18:17:54 +00:00
|
|
|
case "${arg}" in
|
2005-12-06 14:07:13 +00:00
|
|
|
-a|--all)
|
|
|
|
ALL=1
|
|
|
|
;;
|
2005-12-08 15:14:46 +00:00
|
|
|
-v|--verbose)
|
|
|
|
VERBOSE=1
|
|
|
|
;;
|
2005-12-06 14:07:13 +00:00
|
|
|
-p|--parallel)
|
2006-11-14 22:51:17 +00:00
|
|
|
PARALLEL=1
|
2005-12-06 14:07:13 +00:00
|
|
|
;;
|
|
|
|
-h|--help)
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
--)
|
|
|
|
NO_MORE_ARGS=1
|
|
|
|
;;
|
|
|
|
*)
|
2006-04-29 10:58:57 +00:00
|
|
|
eval source_${no_sources}=\"$arg\"
|
|
|
|
no_sources=$(($no_sources+1))
|
2005-12-06 14:07:13 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
2005-12-06 12:45:37 +00:00
|
|
|
|
2006-01-22 11:21:35 +00:00
|
|
|
i=$(($i+1))
|
2005-12-06 12:45:37 +00:00
|
|
|
done
|
|
|
|
|
2007-05-31 09:55:37 +00:00
|
|
|
# also export number of sources
|
|
|
|
export no_sources
|
|
|
|
|
2005-12-08 15:14:46 +00:00
|
|
|
#
|
2006-10-15 14:48:51 +00:00
|
|
|
# be really, really, really verbose
|
2005-12-08 15:14:46 +00:00
|
|
|
#
|
2007-08-16 18:17:54 +00:00
|
|
|
if [ "${VERBOSE}" = 1 ]; then
|
2005-12-08 15:14:46 +00:00
|
|
|
set -x
|
|
|
|
fi
|
2005-12-06 15:36:28 +00:00
|
|
|
|
2005-12-06 14:35:29 +00:00
|
|
|
#
|
|
|
|
# Look, if we should take ALL sources
|
|
|
|
#
|
2007-08-16 18:17:54 +00:00
|
|
|
if [ "${ALL}" = 1 ]; then
|
2005-12-06 14:35:29 +00:00
|
|
|
# reset everything specified before
|
2006-04-29 10:58:57 +00:00
|
|
|
no_sources=0
|
2006-10-25 12:48:48 +00:00
|
|
|
|
2005-12-06 14:55:07 +00:00
|
|
|
#
|
|
|
|
# get entries from sources
|
|
|
|
#
|
2006-10-15 14:48:51 +00:00
|
|
|
cwd=$(pwd -P)
|
2007-08-14 13:30:00 +00:00
|
|
|
( cd "${CSOURCES}" && ls > "${TMP}" ); ret=$?
|
2006-10-15 14:50:12 +00:00
|
|
|
|
2007-08-14 13:30:00 +00:00
|
|
|
[ "${ret}" -eq 0 ] || _exit_err "Listing of sources failed. Aborting."
|
2006-10-25 12:48:48 +00:00
|
|
|
|
2005-12-06 14:55:07 +00:00
|
|
|
while read tmp; do
|
2007-08-16 18:17:54 +00:00
|
|
|
eval source_${no_sources}=\"${tmp}\"
|
|
|
|
no_sources=$((${no_sources}+1))
|
2007-03-13 07:31:50 +00:00
|
|
|
done < "${TMP}"
|
2005-12-06 14:35:29 +00:00
|
|
|
fi
|
|
|
|
|
2007-08-14 13:26:20 +00:00
|
|
|
#
|
|
|
|
# Need at least ONE source to backup
|
|
|
|
#
|
|
|
|
if [ "${no_sources}" -lt 1 ]; then
|
|
|
|
usage
|
|
|
|
else
|
|
|
|
_techo "${HALF_VERSION}: Beginning backup using interval ${INTERVAL}"
|
|
|
|
fi
|
|
|
|
|
2007-08-14 12:41:36 +00:00
|
|
|
#
|
|
|
|
# Look for pre-exec command (general)
|
|
|
|
#
|
|
|
|
if [ -x "${CPREEXEC}" ]; then
|
2007-08-16 18:17:54 +00:00
|
|
|
_techo "Executing ${CPREEXEC} ..."
|
2007-08-14 12:41:36 +00:00
|
|
|
"${CPREEXEC}"; ret=$?
|
2007-08-16 19:37:39 +00:00
|
|
|
_techo "Finished ${CPREEXEC} (return code: ${ret})."
|
2007-08-14 12:41:36 +00:00
|
|
|
|
2007-08-16 19:37:39 +00:00
|
|
|
[ "${ret}" -eq 0 ] || _exit_err "${CPREEXEC} failed. Aborting"
|
2007-08-14 12:41:36 +00:00
|
|
|
fi
|
|
|
|
|
2005-12-07 16:41:20 +00:00
|
|
|
#
|
|
|
|
# check default configuration
|
|
|
|
#
|
|
|
|
|
2007-03-13 07:31:50 +00:00
|
|
|
D_FILE_INTERVAL="${CDEFAULTS}/intervals/${INTERVAL}"
|
|
|
|
D_INTERVAL=$(cat "${D_FILE_INTERVAL}" 2>/dev/null)
|
2005-12-07 16:41:20 +00:00
|
|
|
|
2007-08-14 13:26:20 +00:00
|
|
|
|
2005-12-06 15:20:43 +00:00
|
|
|
#
|
|
|
|
# Let's do the backup
|
|
|
|
#
|
|
|
|
i=0
|
2007-08-16 18:17:54 +00:00
|
|
|
while [ "${i}" -lt "${no_sources}" ]; do
|
2005-11-17 11:33:49 +00:00
|
|
|
|
2005-11-17 15:52:44 +00:00
|
|
|
#
|
2006-04-29 10:58:57 +00:00
|
|
|
# Get current source
|
2005-11-17 15:52:44 +00:00
|
|
|
#
|
2006-11-14 23:01:19 +00:00
|
|
|
eval name=\"\$source_${i}\"
|
2007-08-16 18:17:54 +00:00
|
|
|
i=$((${i}+1))
|
2006-01-13 16:07:20 +00:00
|
|
|
|
|
|
|
export name
|
|
|
|
|
2006-01-13 15:37:22 +00:00
|
|
|
#
|
|
|
|
# start ourself, if we want parallel execution
|
|
|
|
#
|
2007-03-13 07:31:50 +00:00
|
|
|
if [ "${PARALLEL}" ]; then
|
|
|
|
"$0" "${INTERVAL}" "${name}" &
|
2006-01-13 15:37:22 +00:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2006-01-13 16:07:20 +00:00
|
|
|
#
|
|
|
|
# Start subshell for easy log editing
|
|
|
|
#
|
|
|
|
(
|
|
|
|
#
|
|
|
|
# Stderr to stdout, so we can produce nice logs
|
|
|
|
#
|
|
|
|
exec 2>&1
|
|
|
|
|
2006-01-13 15:37:22 +00:00
|
|
|
#
|
2007-08-14 13:26:20 +00:00
|
|
|
# Configuration
|
2006-01-13 15:37:22 +00:00
|
|
|
#
|
2007-03-13 07:31:50 +00:00
|
|
|
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"
|
2007-08-16 18:31:30 +00:00
|
|
|
c_incomplete="${backup}/delete_incomplete"
|
|
|
|
|
|
|
|
#
|
|
|
|
# Marking backups: If we abort it's not removed => Backup is broken
|
|
|
|
#
|
|
|
|
c_marker=".ccollect-marker"
|
2006-10-26 08:30:14 +00:00
|
|
|
|
2007-01-20 15:41:38 +00:00
|
|
|
#
|
2007-08-14 13:26:20 +00:00
|
|
|
# Times
|
2007-01-20 15:41:38 +00:00
|
|
|
#
|
2006-01-25 22:37:52 +00:00
|
|
|
begin_s=$(date +%s)
|
|
|
|
|
2006-06-13 10:04:36 +00:00
|
|
|
#
|
|
|
|
# unset possible options
|
|
|
|
#
|
|
|
|
EXCLUDE=""
|
|
|
|
RSYNC_EXTRA=""
|
|
|
|
SUMMARY=""
|
|
|
|
VERBOSE=""
|
|
|
|
VVERBOSE=""
|
2007-08-16 18:31:30 +00:00
|
|
|
DELETE_INCOMPLETE=""
|
2006-06-13 10:04:36 +00:00
|
|
|
|
2007-08-14 13:26:20 +00:00
|
|
|
_techo "Beginning to backup"
|
2006-01-13 16:07:20 +00:00
|
|
|
|
2005-12-06 15:36:28 +00:00
|
|
|
#
|
|
|
|
# Standard configuration checks
|
|
|
|
#
|
2007-03-13 07:47:04 +00:00
|
|
|
if [ ! -e "${backup}" ]; then
|
2007-08-16 18:17:54 +00:00
|
|
|
_exit_err "Source does not exist."
|
2005-12-08 09:57:36 +00:00
|
|
|
fi
|
2006-10-25 12:48:48 +00:00
|
|
|
|
2006-06-13 10:04:36 +00:00
|
|
|
#
|
|
|
|
# configuration _must_ be a directory
|
|
|
|
#
|
2007-03-13 07:47:04 +00:00
|
|
|
if [ ! -d "${backup}" ]; then
|
2007-08-16 18:17:54 +00:00
|
|
|
_exit_err "\"${name}\" is not a cconfig-directory. Skipping."
|
2005-11-17 15:52:44 +00:00
|
|
|
fi
|
2005-12-06 15:20:43 +00:00
|
|
|
|
2006-06-13 10:04:36 +00:00
|
|
|
#
|
|
|
|
# first execute pre_exec, which may generate destination or other
|
|
|
|
# parameters
|
|
|
|
#
|
2007-03-13 07:47:04 +00:00
|
|
|
if [ -x "${c_pre_exec}" ]; then
|
2007-08-14 13:26:20 +00:00
|
|
|
_techo "Executing ${c_pre_exec} ..."
|
2007-03-13 07:47:04 +00:00
|
|
|
"${c_pre_exec}"; ret="$?"
|
2007-08-14 13:26:20 +00:00
|
|
|
_techo "Finished ${c_pre_exec} (return code ${ret})."
|
2006-06-13 10:04:36 +00:00
|
|
|
|
2007-03-13 07:47:04 +00:00
|
|
|
if [ "${ret}" -ne 0 ]; then
|
2007-08-14 13:26:20 +00:00
|
|
|
_exit_err "${c_pre_exec} failed. Skipping."
|
2006-06-13 10:04:36 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2005-12-07 16:41:20 +00:00
|
|
|
#
|
2006-04-25 13:35:56 +00:00
|
|
|
# interval definition: First try source specific, fallback to default
|
2005-12-07 16:41:20 +00:00
|
|
|
#
|
2007-03-13 07:47:04 +00:00
|
|
|
c_interval="$(cat "${backup}/intervals/${INTERVAL}" 2>/dev/null)"
|
2005-12-07 16:41:20 +00:00
|
|
|
|
2007-03-13 07:47:04 +00:00
|
|
|
if [ -z "${c_interval}" ]; then
|
|
|
|
c_interval="${D_INTERVAL}"
|
2005-12-07 16:41:20 +00:00
|
|
|
|
2007-03-13 07:47:04 +00:00
|
|
|
if [ -z "${c_interval}" ]; then
|
2007-08-14 13:26:20 +00:00
|
|
|
_exit_err "No definition for interval \"${INTERVAL}\" found. Skipping."
|
2005-12-07 16:41:20 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
#
|
2006-06-13 10:04:36 +00:00
|
|
|
# Source checks
|
2005-12-07 16:41:20 +00:00
|
|
|
#
|
2007-03-13 07:47:04 +00:00
|
|
|
if [ ! -f "${c_source}" ]; then
|
2007-08-14 13:26:20 +00:00
|
|
|
_exit_err "Source description \"${c_source}\" is not a file. Skipping."
|
2005-11-17 15:52:44 +00:00
|
|
|
else
|
2007-08-14 13:26:20 +00:00
|
|
|
source=$(cat "${c_source}"); ret=$?
|
|
|
|
if [ ${ret} -ne 0 ]; then
|
|
|
|
_exit_err "Source ${c_source} is not readable. Skipping."
|
2005-12-06 11:50:36 +00:00
|
|
|
fi
|
2005-11-17 15:52:44 +00:00
|
|
|
fi
|
2005-12-06 15:20:43 +00:00
|
|
|
|
2006-06-13 10:04:36 +00:00
|
|
|
#
|
|
|
|
# destination _must_ be a directory
|
|
|
|
#
|
2007-03-13 07:47:04 +00:00
|
|
|
if [ ! -d "${c_dest}" ]; then
|
2007-08-16 18:08:23 +00:00
|
|
|
_exit_err "Destination ${c_dest} is not a directory. Skipping."
|
2005-11-17 15:52:44 +00:00
|
|
|
fi
|
2006-06-13 10:04:36 +00:00
|
|
|
|
2007-08-16 18:31:30 +00:00
|
|
|
#
|
|
|
|
# Check whether to delete incomplete backups
|
|
|
|
#
|
|
|
|
if [ -f "${c_incomplete}" ]; then
|
|
|
|
DELETE_INCOMPLETE="yes"
|
|
|
|
fi
|
|
|
|
|
2007-08-16 18:08:23 +00:00
|
|
|
# NEW method as of 0.6:
|
2007-08-14 13:26:20 +00:00
|
|
|
# - insert ccollect default parameters
|
|
|
|
# - insert options
|
|
|
|
# - insert user options
|
2007-08-14 13:30:00 +00:00
|
|
|
|
2007-08-14 13:40:23 +00:00
|
|
|
#
|
|
|
|
# rsync standard options
|
|
|
|
#
|
2007-08-14 13:26:20 +00:00
|
|
|
|
2007-08-16 18:08:23 +00:00
|
|
|
set -- "$@" "--archive" "--delete" "--numeric-ids" "--relative" \
|
|
|
|
"--delete-excluded" "--sparse"
|
|
|
|
|
2006-01-22 11:21:35 +00:00
|
|
|
#
|
2006-06-13 10:04:36 +00:00
|
|
|
# exclude list
|
2006-01-22 11:21:35 +00:00
|
|
|
#
|
2007-08-14 13:26:20 +00:00
|
|
|
if [ -f "${c_exclude}" ]; then
|
2007-08-16 18:17:54 +00:00
|
|
|
set -- "$@" "--exclude-from=${c_exclude}"
|
2005-12-06 11:50:36 +00:00
|
|
|
fi
|
2006-10-25 12:48:48 +00:00
|
|
|
|
2006-06-13 10:04:36 +00:00
|
|
|
#
|
2006-01-24 20:46:49 +00:00
|
|
|
# Output a summary
|
2006-06-13 10:04:36 +00:00
|
|
|
#
|
2007-03-13 07:47:04 +00:00
|
|
|
if [ -f "${c_summary}" ]; then
|
2007-08-14 13:40:23 +00:00
|
|
|
set -- "$@" "--stats"
|
2006-01-24 20:46:49 +00:00
|
|
|
fi
|
2006-10-25 12:48:48 +00:00
|
|
|
|
2006-06-13 10:04:36 +00:00
|
|
|
#
|
2006-04-25 13:19:53 +00:00
|
|
|
# Verbosity for rsync
|
2006-06-13 10:04:36 +00:00
|
|
|
#
|
2007-08-14 13:40:23 +00:00
|
|
|
if [ -f "${c_vverbose}" ]; then
|
|
|
|
set -- "$@" "-vv"
|
|
|
|
elif [ -f "${c_verbose}" ]; then
|
|
|
|
set -- "$@" "-v"
|
2006-03-19 00:10:58 +00:00
|
|
|
fi
|
2006-10-25 12:48:48 +00:00
|
|
|
|
2006-06-13 10:04:36 +00:00
|
|
|
#
|
2007-08-14 13:40:23 +00:00
|
|
|
# extra options for rsync provided by the user
|
2006-06-13 10:04:36 +00:00
|
|
|
#
|
2007-08-14 13:40:23 +00:00
|
|
|
if [ -f "${c_rsync_extra}" ]; then
|
|
|
|
while read line; do
|
|
|
|
set -- "$@" "$line"
|
|
|
|
done < "${c_rsync_extra}"
|
2006-01-22 09:40:40 +00:00
|
|
|
fi
|
2006-10-25 12:48:48 +00:00
|
|
|
|
2007-08-16 18:08:23 +00:00
|
|
|
#
|
|
|
|
# Check for incomplete backups
|
|
|
|
#
|
2007-08-27 16:56:03 +00:00
|
|
|
( cd "${c_dest}" 2>/dev/null && ls -1 "${INTERVAL}"*".${c_marker}" > "${TMP}" 2>/dev/null)
|
2007-08-16 19:25:46 +00:00
|
|
|
|
|
|
|
while read incomplete; do
|
2007-08-27 16:56:03 +00:00
|
|
|
realincomplete=$(echo ${incomplete} | sed "s/\\.${c_marker}\$//")
|
2007-08-16 19:25:46 +00:00
|
|
|
_techo "Incomplete backup: ${realincomplete}"
|
|
|
|
if [ "${DELETE_INCOMPLETE}" = "yes" ]; then
|
|
|
|
_techo "Deleting ${realincomplete} ..."
|
2007-08-16 19:37:39 +00:00
|
|
|
rm $VVERBOSE -rf "${c_dest}/${realincomplete}" || \
|
|
|
|
_exit_err "Removing ${c_dest}/${realincomplete} failed."
|
2007-08-16 19:25:46 +00:00
|
|
|
fi
|
|
|
|
done < "${TMP}"
|
2007-08-16 18:08:23 +00:00
|
|
|
|
2007-08-16 19:11:13 +00:00
|
|
|
|
2005-12-07 16:41:20 +00:00
|
|
|
#
|
2005-12-07 17:36:25 +00:00
|
|
|
# check if maximum number of backups is reached, if so remove
|
2007-08-16 18:41:03 +00:00
|
|
|
# use grep and ls -p so we only look at directories
|
2007-08-16 19:11:13 +00:00
|
|
|
#
|
2007-08-16 18:41:03 +00:00
|
|
|
count=$(cd "${c_dest}" && ls -p1 | grep "^${INTERVAL}\..*/\$" | wc -l \
|
|
|
|
| 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}))
|
2007-08-16 19:26:56 +00:00
|
|
|
_techo "Removing ${remove} backup(s)..."
|
2007-08-16 18:41:03 +00:00
|
|
|
|
2007-08-16 19:37:39 +00:00
|
|
|
( cd "${c_dest}" 2>/dev/null && ls -p1 | grep "^${INTERVAL}\..*/\$" | \
|
2007-08-27 16:56:03 +00:00
|
|
|
sort -n | head -n "${remove}" ) > "${TMP}"
|
2007-08-16 19:37:39 +00:00
|
|
|
|
|
|
|
while read to_remove; do
|
|
|
|
_techo "Removing ${to_remove} ..."
|
2007-08-27 16:57:14 +00:00
|
|
|
rm ${VVERBOSE} -rf "${c_dest}/${to_remove}" || \
|
2007-08-16 19:37:39 +00:00
|
|
|
_exit_err "Removing ${to_remove} failed."
|
|
|
|
done < "${TMP}"
|
2007-08-16 18:41:03 +00:00
|
|
|
fi
|
|
|
|
|
2007-08-16 19:11:13 +00:00
|
|
|
|
2007-08-16 18:08:23 +00:00
|
|
|
#
|
2007-08-17 21:31:45 +00:00
|
|
|
# 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.
|
2007-08-16 18:08:23 +00:00
|
|
|
#
|
2007-08-17 21:50:40 +00:00
|
|
|
rel_last_dir="$(cd "${c_dest}" && ls -tcp1 | grep '/$' | head -n 1)" || \
|
2007-08-17 21:31:45 +00:00
|
|
|
_exit_err "Failed to list contents of ${c_dest}."
|
2007-08-17 21:50:40 +00:00
|
|
|
last_dir="${c_dest}/${rel_last_dir}"
|
2007-08-16 19:11:13 +00:00
|
|
|
|
2007-08-16 19:17:25 +00:00
|
|
|
#
|
2007-08-16 19:37:39 +00:00
|
|
|
# clone from old backup, if existing
|
2007-08-16 19:17:25 +00:00
|
|
|
#
|
|
|
|
if [ "${last_dir}" ]; then
|
2007-08-20 10:45:23 +00:00
|
|
|
# must be absolute, otherwise rsync uses it relative to
|
|
|
|
# the destination directory. See rsync(1).
|
2007-08-17 21:31:45 +00:00
|
|
|
abs_last_dir="$(cd "${last_dir}" && pwd -P)" || \
|
|
|
|
_exit_err "Could not change to last dir ${last_dir}."
|
2007-08-16 19:17:25 +00:00
|
|
|
set -- "$@" "--link-dest=${abs_last_dir}"
|
2007-08-17 21:50:40 +00:00
|
|
|
_techo "Hard linking from ${rel_last_dir}"
|
2007-08-16 19:17:25 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
2007-08-16 19:11:13 +00:00
|
|
|
# set time when we really begin to backup, not when we began to remove above
|
|
|
|
destination_date=$(${CDATE})
|
|
|
|
destination_dir="${c_dest}/${INTERVAL}.${destination_date}.$$"
|
2006-10-25 12:48:48 +00:00
|
|
|
|
2005-12-08 13:15:45 +00:00
|
|
|
# give some info
|
2007-08-16 19:11:13 +00:00
|
|
|
_techo "Beginning to backup, this may take some time..."
|
2005-12-08 09:27:39 +00:00
|
|
|
|
2007-08-16 19:45:02 +00:00
|
|
|
_techo "Creating ${destination_dir} ..."
|
2007-08-16 19:11:13 +00:00
|
|
|
mkdir ${VVERBOSE} "${destination_dir}" || \
|
2007-08-16 19:17:25 +00:00
|
|
|
_exit_err "Creating ${destination_dir} failed. Skipping."
|
2006-10-15 15:12:39 +00:00
|
|
|
|
2007-08-16 19:17:25 +00:00
|
|
|
# absulte now, it's existing
|
2007-08-20 15:29:24 +00:00
|
|
|
abs_destination_dir="$(cd "${destination_dir}" && pwd -P)" || \
|
|
|
|
_exit_err "Changing to newly created ${destination_dir} failed. Skipping."
|
2006-10-25 13:16:37 +00:00
|
|
|
|
2006-06-16 22:12:38 +00:00
|
|
|
#
|
2007-08-16 19:17:25 +00:00
|
|
|
# added marking in 0.6 (and remove it, if successful later)
|
2006-06-16 22:12:38 +00:00
|
|
|
#
|
2007-08-14 13:40:23 +00:00
|
|
|
touch "${abs_destination_dir}/${c_marker}"
|
2006-10-15 14:48:51 +00:00
|
|
|
|
2005-12-06 16:08:38 +00:00
|
|
|
#
|
|
|
|
# the rsync part
|
|
|
|
#
|
2006-10-25 12:48:48 +00:00
|
|
|
|
2007-08-14 13:40:23 +00:00
|
|
|
_techo "Transferring files..."
|
2007-08-16 19:11:13 +00:00
|
|
|
rsync "$@" "${source}" "${abs_destination_dir}"; ret=$?
|
|
|
|
|
2006-10-25 13:16:37 +00:00
|
|
|
#
|
2007-08-16 18:08:23 +00:00
|
|
|
# remove marking here
|
2006-10-25 13:16:37 +00:00
|
|
|
#
|
2007-08-16 19:37:39 +00:00
|
|
|
rm -f "${abs_destination_dir}/${c_marker}" || \
|
|
|
|
_exit_err "Removing ${abs_destination_dir}/${c_marker} failed."
|
2007-08-16 18:08:23 +00:00
|
|
|
|
|
|
|
_techo "Finished backup (rsync return code: $ret)."
|
2007-08-20 15:33:09 +00:00
|
|
|
if [ "${ret}" -ne 0 ]; then
|
2007-08-16 19:11:13 +00:00
|
|
|
_techo "Warning: rsync exited non-zero, the backup may be broken (see rsync errors)."
|
|
|
|
fi
|
2006-01-22 11:37:04 +00:00
|
|
|
|
2006-01-22 11:21:35 +00:00
|
|
|
#
|
|
|
|
# post_exec
|
|
|
|
#
|
2007-08-16 18:08:23 +00:00
|
|
|
if [ -x "${c_post_exec}" ]; then
|
|
|
|
_techo "Executing ${c_post_exec} ..."
|
|
|
|
"${c_post_exec}"; ret=$?
|
|
|
|
_techo "Finished ${c_post_exec}."
|
2006-03-19 00:13:38 +00:00
|
|
|
|
2007-08-16 18:08:23 +00:00
|
|
|
if [ ${ret} -ne 0 ]; then
|
|
|
|
_exit_err "${c_post_exec} failed."
|
2006-03-19 00:13:38 +00:00
|
|
|
fi
|
2006-01-22 11:21:35 +00:00
|
|
|
fi
|
2006-01-13 16:07:20 +00:00
|
|
|
|
2007-08-16 18:08:23 +00:00
|
|
|
# Calculation
|
2006-01-25 22:37:52 +00:00
|
|
|
end_s=$(date +%s)
|
|
|
|
|
2006-10-15 19:01:28 +00:00
|
|
|
full_seconds=$((${end_s} - ${begin_s}))
|
2007-08-16 19:17:25 +00:00
|
|
|
hours=$((${full_seconds} / 3600))
|
|
|
|
seconds=$((${full_seconds} - (${hours} * 3600)))
|
|
|
|
minutes=$((${seconds} / 60))
|
2006-10-15 19:01:28 +00:00
|
|
|
seconds=$((${seconds} - (${minutes} * 60)))
|
2006-01-25 22:37:52 +00:00
|
|
|
|
2007-08-16 18:08:23 +00:00
|
|
|
_techo "Backup lasted: ${hours}:${minutes}:${seconds} (h:m:s)"
|
2006-01-25 22:37:52 +00:00
|
|
|
|
2006-01-13 16:07:20 +00:00
|
|
|
) | add_name
|
2005-11-17 15:52:44 +00:00
|
|
|
done
|
2005-12-06 14:55:07 +00:00
|
|
|
|
2005-12-06 16:08:38 +00:00
|
|
|
#
|
|
|
|
# Be a good parent and wait for our children, if they are running wild parallel
|
|
|
|
#
|
2007-08-16 18:08:23 +00:00
|
|
|
if [ "${PARALLEL}" ]; then
|
2007-08-16 19:37:39 +00:00
|
|
|
_techo "Waiting for children to complete..."
|
2005-12-06 16:08:38 +00:00
|
|
|
wait
|
|
|
|
fi
|
|
|
|
|
2006-01-22 10:13:22 +00:00
|
|
|
#
|
|
|
|
# Look for post-exec command (general)
|
|
|
|
#
|
2007-08-16 18:08:23 +00:00
|
|
|
if [ -x "${CPOSTEXEC}" ]; then
|
|
|
|
_techo "Executing ${CPOSTEXEC} ..."
|
2007-08-16 19:37:39 +00:00
|
|
|
"${CPOSTEXEC}"; ret=$?
|
|
|
|
_techo "Finished ${CPOSTEXEC} (return code: ${ret})."
|
2006-10-25 12:48:48 +00:00
|
|
|
|
2007-08-16 18:08:23 +00:00
|
|
|
if [ ${ret} -ne 0 ]; then
|
2007-08-16 19:46:02 +00:00
|
|
|
_techo "${CPOSTEXEC} failed."
|
2006-03-19 00:13:38 +00:00
|
|
|
fi
|
2006-01-22 10:13:22 +00:00
|
|
|
fi
|
|
|
|
|
2007-08-16 18:08:23 +00:00
|
|
|
rm -f "${TMP}"
|
|
|
|
_techo "Finished ${WE}"
|