super-cool error handling

This commit is contained in:
Vincent Riquer 2012-02-09 17:08:34 +01:00
parent c87f11a3f8
commit ea72b4d2dd

View File

@ -72,11 +72,35 @@ do
esac
done
declare -a failedBackups
declare -A failedWith
failRsync() {
nameFailed="$1"
code=$2
path="$3"
failedBackups+=( "$name" )
if [ -d "$3" ]
then
touch "$3/unfinished.remote-backup"
failedWith["$name"]="RSYNC_PARTIAL $code"
else
failedWith["$name"]="RSYNC_FAILED $code"
fi
}
openLog() {
exec 3>&1
exec 1>> /var/log/remote-backup
exec 2>&1
}
doBackup() {
config="$1"
if [ ! -e "$config" ]
then
echo "No backups defined, dying." >&2
echo "No backups defined, dying." >&3
exit 128
fi
source /etc/remote-backup/defaults.conf
@ -91,6 +115,8 @@ doBackup() {
if (( $? ))
then
echo "snapshot: could not remount $SNAPSHOT_RW readwrite" >&2
failedBackups+=( "$NAME" )
failedReason["$NAME"]="REMOUNT_RW $MOUNT_DEVICE"
return 1
fi
fi
@ -114,8 +140,7 @@ doBackup() {
$MOUNT -o remount,ro "$MOUNT_DEVICE" "$SNAPSHOT_RW"
if (( $? ))
then
echo "snapshot: could not remount $SNAPSHOT_RW readonly"
exit
echo "snapshot: could not remount $SNAPSHOT_RW readonly" >&2
fi
fi
@ -188,28 +213,25 @@ runBackup() {
$ADDITIONAL_RSYNC_OPTS \
"${REMOTE_LOCATION}" \
"$SNAPSHOT_RW/$NAME/$1"
if (( $? == 2 ))
then
if (( proto29 ))
then
echo "Still failing, giving up"
else
echo "Rsync protocol mismatch, trying with only one --link-dest"
runBackup "$1" PROTOCOLMISMATCH
fi
elif (( $? == 10 )) \
|| (( $? == 12 )) \
|| (( $? == 20 )) \
|| (( $? == 21 )) \
|| (( $? == 22 )) \
|| (( $? == 23 )) \
|| (( $? == 30 ))
then
if [ -d "$SNAPSHOT_RW/$NAME/$1" ]
then
touch "$SNAPSHOT_RW/$NAME/$1/unfinished.remote-backup"
fi
fi
returncode=$?
case $returncode in
0)
:
;;
2)
if (( proto29 ))
then
fail "$NAME" PROTO_MISMATCH
else
echo "Rsync protocol mismatch, trying with only one --link-dest"
runBackup "$1" PROTOCOLMISMATCH
fi
;;
*)
failRsync "$NAME" $returncode "$SNAPSHOT_RW/$NAME/$1"
;;
esac
# step 5: update the mtime of daily.0 to reflect the snapshot time
$TOUCH "$SNAPSHOT_RW/$NAME/$1"
@ -225,6 +247,8 @@ then
exit
fi
openLog
if [ -n "$EMERG" ]
then
doBackup /etc/remote-backup/"${EMERG//\//_}"/config emerg
@ -234,3 +258,34 @@ else
doBackup "$config"
done
fi
if (( ${#failedBackups} ))
then
cat <<-EOF
Some backups exited non-zero.
See the logfile for more detail.
EOF
for backup in ${failedBackups[@]}
do
echo -n "$backup failed: "
read info more_info <<< "${failedWith["$backup"]}"
case $info in
RSYNC_PARTIAL)
echo "Partial transfer, will *NOT* get rotated - rsync returned $more_info."
;;
RSYNC_FAILED)
echo "No files transferred - rsync returned $more_info."
;;
PROTO_MISMATCH)
echo "No files transferred - Protocol mismatch - Upgrade rsync daemon"
;;
REMOUNT_RW)
echo "Target device $more_info could not be mounted ReadWrite."
;;
*)
echo "$info $moreinfo"
;;
esac
done
fi