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 esac
done 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() { doBackup() {
config="$1" config="$1"
if [ ! -e "$config" ] if [ ! -e "$config" ]
then then
echo "No backups defined, dying." >&2 echo "No backups defined, dying." >&2
echo "No backups defined, dying." >&3
exit 128 exit 128
fi fi
source /etc/remote-backup/defaults.conf source /etc/remote-backup/defaults.conf
@ -91,6 +115,8 @@ doBackup() {
if (( $? )) if (( $? ))
then then
echo "snapshot: could not remount $SNAPSHOT_RW readwrite" >&2 echo "snapshot: could not remount $SNAPSHOT_RW readwrite" >&2
failedBackups+=( "$NAME" )
failedReason["$NAME"]="REMOUNT_RW $MOUNT_DEVICE"
return 1 return 1
fi fi
fi fi
@ -114,8 +140,7 @@ doBackup() {
$MOUNT -o remount,ro "$MOUNT_DEVICE" "$SNAPSHOT_RW" $MOUNT -o remount,ro "$MOUNT_DEVICE" "$SNAPSHOT_RW"
if (( $? )) if (( $? ))
then then
echo "snapshot: could not remount $SNAPSHOT_RW readonly" echo "snapshot: could not remount $SNAPSHOT_RW readonly" >&2
exit
fi fi
fi fi
@ -188,28 +213,25 @@ runBackup() {
$ADDITIONAL_RSYNC_OPTS \ $ADDITIONAL_RSYNC_OPTS \
"${REMOTE_LOCATION}" \ "${REMOTE_LOCATION}" \
"$SNAPSHOT_RW/$NAME/$1" "$SNAPSHOT_RW/$NAME/$1"
if (( $? == 2 ))
then returncode=$?
case $returncode in
0)
:
;;
2)
if (( proto29 )) if (( proto29 ))
then then
echo "Still failing, giving up" fail "$NAME" PROTO_MISMATCH
else else
echo "Rsync protocol mismatch, trying with only one --link-dest" echo "Rsync protocol mismatch, trying with only one --link-dest"
runBackup "$1" PROTOCOLMISMATCH runBackup "$1" PROTOCOLMISMATCH
fi fi
elif (( $? == 10 )) \ ;;
|| (( $? == 12 )) \ *)
|| (( $? == 20 )) \ failRsync "$NAME" $returncode "$SNAPSHOT_RW/$NAME/$1"
|| (( $? == 21 )) \ ;;
|| (( $? == 22 )) \ esac
|| (( $? == 23 )) \
|| (( $? == 30 ))
then
if [ -d "$SNAPSHOT_RW/$NAME/$1" ]
then
touch "$SNAPSHOT_RW/$NAME/$1/unfinished.remote-backup"
fi
fi
# step 5: update the mtime of daily.0 to reflect the snapshot time # step 5: update the mtime of daily.0 to reflect the snapshot time
$TOUCH "$SNAPSHOT_RW/$NAME/$1" $TOUCH "$SNAPSHOT_RW/$NAME/$1"
@ -225,6 +247,8 @@ then
exit exit
fi fi
openLog
if [ -n "$EMERG" ] if [ -n "$EMERG" ]
then then
doBackup /etc/remote-backup/"${EMERG//\//_}"/config emerg doBackup /etc/remote-backup/"${EMERG//\//_}"/config emerg
@ -234,3 +258,34 @@ else
doBackup "$config" doBackup "$config"
done done
fi 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