Skip to content

Commit

Permalink
Provide informative exit codes
Browse files Browse the repository at this point in the history
Blubee continues to backup the items in the 'backup.json' even if there
is an error with some item. This is useful as a partial backup is often
better than no backup. However, when there are alot of items to backup
the error might be hidden in the output and the user may not be aware
that something went wrong. Especially if one is using blubee in a
script, as the final exit code of blubee will be 0. To avoid this we
make sure to detect when ever an item fails to backup and exit with an
approperiate exit code, to notify the caller that something went wrong.

To make it easier to find the error we also provide some logs that may
help the user identify the issue.
  • Loading branch information
christian-eriksson committed Nov 21, 2023
1 parent e86e3e5 commit 5c30126
Showing 1 changed file with 70 additions and 22 deletions.
92 changes: 70 additions & 22 deletions backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,37 @@ backup_version="$(date '+%Y%m%d_%H%M%S')"

while getopts ":s:d:r:xh:u:v:p:" option; do
case "${option}" in
d)
destination_root=${OPTARG};;
s)
source_paths=${OPTARG};;
r)
source_root=${OPTARG};;
x)
dry_run="--dry-run";;
u)
user=${OPTARG};;
h)
host=${OPTARG};;
p)
port=${OPTARG};;
v)
backup_version=${OPTARG};;
:)
echo "Missing argument for option $OPTARG"
exit 1;;
?)
echo "Unrecognized option '$OPTARG'";;
d)
destination_root=${OPTARG}
;;
s)
source_paths=${OPTARG}
;;
r)
source_root=${OPTARG}
;;
x)
dry_run="--dry-run"
;;
u)
user=${OPTARG}
;;
h)
host=${OPTARG}
;;
p)
port=${OPTARG}
;;
v)
backup_version=${OPTARG}
;;
:)
echo "Missing argument for option $OPTARG"
exit 1
;;
?)
echo "Unrecognized option '$OPTARG'"
;;
esac
done

Expand Down Expand Up @@ -68,31 +78,69 @@ test_nonexistent_link "$latest_link" "$message" "$host" "$user" "$port"

index=0
source_path="$(dequote_string "$(get_list_item "$source_paths" "$index")")"
if [ "$?" -ne "0" ]; then
echo "Could not get source paths from ${source_paths}, json may be malformed"
exit 4
fi

exit_code=0
while [ "$source_path" != "null" ]; do
source_suffix=$(trim_right_slash "$(trim_left_slash "$source_path")")
backup_path_suffix=$(trim_right_slash "$(trim_to_first_right_slash "$source_suffix")")
destination=$(trim_right_slash "$backup_path/$backup_path_suffix")
[ -z "$dry_run" ] && create_directory "$destination" "$host" "$user" "$port"

if [ "$?" -ne "0" ]; then
echo "Could not not create directory ${destination}"
exit_code=10
fi

source="$source_root/$source_suffix"

if [ -z "$port" ]; then
rsync -aE --protect-args --progress --delete $dry_run \
--link-dest "$latest_link/$backup_path_suffix" \
"$source" \
"$remote_prefix$destination"

if [ "$?" -ne "0" ]; then
echo "Unable to backup ${source} to ${remote_prefix}${destination}"
exit_code=10
fi
else
rsync -aE --protect-args --progress --delete $dry_run --rsh="ssh -p $port" \
--link-dest "$latest_link/$backup_path_suffix" \
"$source" \
"$remote_prefix$destination"

if [ "$?" -ne "0" ]; then
echo "Unable to backup ${source} to ${remote_prefix}${destination}"
exit_code=10
fi
fi

index=$((index + 1))
source_path="$(dequote_string "$(get_list_item "$source_paths" "$index")")"
if [ "$?" -ne "0" ]; then
echo "Could not get source paths from ${source_paths}"
exit_code=15
fi
done

if [ "$exit_code" -ne "0" ]; then
exit $exit_code
fi

if [ -z "$dry_run" ]; then
remove_path "$latest_link" "$host" "$user" "$port"
if [ "$?" -ne "0" ]; then
echo "Could not remove previous link to latest backup"
exit 20
fi

create_link "$backup_path" "$latest_link" "$host" "$user" "$port"
if [ "$?" -ne "0" ]; then
echo "Could not link to latest backup"
exit 30
fi
fi

0 comments on commit 5c30126

Please sign in to comment.