From 7cf0d11ae5307dd408a1b0a393e075ba5a97fcf3 Mon Sep 17 00:00:00 2001 From: Guiorgy Date: Thu, 8 Feb 2024 16:10:16 +0400 Subject: [PATCH 1/2] handle every error also changed the "Volume $VOLUME_NAME does not exist, creating..." message into a warning --- vackup | 79 +++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 29 deletions(-) diff --git a/vackup b/vackup index 168a963..ffd8695 100755 --- a/vackup +++ b/vackup @@ -6,11 +6,17 @@ set -Eeo pipefail handle_error() { - exit_code=$? + case $# in + 1) LINE_NUMBER=$1; EXIT_CODE=$? ;; + 2) LINE_NUMBER=$1; EXIT_CODE=$2 ;; + *) LINE_NUMBER=$LINENO; EXIT_CODE=1 ;; + esac + if [ -n "${VACKUP_FAILURE_SCRIPT}" ]; then - /bin/bash "${VACKUP_FAILURE_SCRIPT}" "$1" $exit_code + /bin/bash "${VACKUP_FAILURE_SCRIPT}" "$LINE_NUMBER" $EXIT_CODE fi - exit $exit_code + + exit $EXIT_CODE } trap 'handle_error $LINENO' ERR @@ -43,6 +49,35 @@ vackup load IMAGE VOLUME EOF } +error() { + if [ "$1" == 'u' ] || [ "$1" == 'usage' ]; then + USAGE=1 + MESSAGE=$2 + CODE=$3 + else + USAGE=0 + MESSAGE=$1 + CODE=$2 + fi + + if [ -z "$MESSAGE" ]; then + echo 'Error' + else + echo "Error: $MESSAGE" + fi + + if [ $USAGE -eq 1 ]; then + usage + fi + + if [ -z "$CODE" ]; then + CODE=1 + fi + + LINE_NUMBER=$(caller | awk '{ print $1 }') + handle_error $LINE_NUMBER $CODE +} + if [ -z "$1" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then usage exit 0 @@ -53,15 +88,12 @@ cmd_export() { FILE_NAME="$3" if [ -z "$VOLUME_NAME" ] || [ -z "$FILE_NAME" ]; then - echo "Error: Not enough arguments" - usage - exit 1 + error usage 'Not enough arguments' fi if ! docker volume inspect --format '{{.Name}}' "$VOLUME_NAME"; then - echo "Error: Volume $VOLUME_NAME does not exist" - exit 1 + error "Volume $VOLUME_NAME does not exist" fi # TODO: check if file exists on host, if it does @@ -75,8 +107,7 @@ cmd_export() { busybox \ tar -zcvf /vackup/"$FILE_NAME" /vackup-volume; then - echo "Error: Failed to start busybox backup container" - exit 1 + error 'Failed to start busybox backup container' fi echo "Successfully tar'ed volume $VOLUME_NAME into file $FILE_NAME" @@ -87,14 +118,12 @@ cmd_import() { VOLUME_NAME="$3" if [ -z "$VOLUME_NAME" ] || [ -z "$FILE_NAME" ]; then - echo "Error: Not enough arguments" - usage - exit 1 + error usage 'Not enough arguments' fi if ! docker volume inspect --format '{{.Name}}' "$VOLUME_NAME"; then - echo "Error: Volume $VOLUME_NAME does not exist" + echo "Warning: Volume $VOLUME_NAME does not exist, creating..." docker volume create "$VOLUME_NAME" fi @@ -109,8 +138,7 @@ cmd_import() { busybox \ tar -xvzf /vackup/"$FILE_NAME" -C /; then - echo "Error: Failed to start busybox container" - exit 1 + error 'Failed to start busybox container' fi echo "Successfully unpacked $FILE_NAME into volume $VOLUME_NAME" @@ -121,15 +149,12 @@ cmd_save() { IMAGE_NAME="$3" if [ -z "$VOLUME_NAME" ] || [ -z "$IMAGE_NAME" ]; then - echo "Error: Not enough arguments" - usage - exit 1 + error usage 'Not enough arguments' fi if ! docker volume inspect --format '{{.Name}}' "$VOLUME_NAME"; then - echo "Error: Volume $VOLUME_NAME does not exist" - exit 1 + error "Volume $VOLUME_NAME does not exist" fi if ! docker run \ @@ -137,8 +162,7 @@ cmd_save() { busybox \ cp -Rp /mount-volume/. /volume-data/; then - echo "Error: Failed to start busybox container" - exit 1 + error 'Failed to start busybox container' fi CONTAINER_ID=$(docker ps -lq) @@ -155,14 +179,12 @@ cmd_load() { VOLUME_NAME="$3" if [ -z "$VOLUME_NAME" ] || [ -z "$IMAGE_NAME" ]; then - echo "Error: Not enough arguments" - usage - exit 1 + error usage 'Not enough arguments' fi if ! docker volume inspect --format '{{.Name}}' "$VOLUME_NAME"; then - echo "Volume $VOLUME_NAME does not exist, creating..." + echo "Warning: Volume $VOLUME_NAME does not exist, creating..." docker volume create "$VOLUME_NAME" fi @@ -171,8 +193,7 @@ cmd_load() { "$IMAGE_NAME" \ cp -Rp /volume-data/. /mount-volume/; then - echo "Error: Failed to start container from $IMAGE_NAME" - exit 1 + error "Failed to start container from $IMAGE_NAME" fi echo "Successfully copied /volume-data from $IMAGE_NAME into volume $VOLUME_NAME" From 0efd1dac113e5fda30c41a135e6b5e0a371fd541 Mon Sep 17 00:00:00 2001 From: Guiorgy Date: Thu, 8 Feb 2024 16:38:34 +0400 Subject: [PATCH 2/2] redirect error messages to stderr --- vackup | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vackup b/vackup index ffd8695..4526018 100755 --- a/vackup +++ b/vackup @@ -61,13 +61,13 @@ error() { fi if [ -z "$MESSAGE" ]; then - echo 'Error' + echo 1>&2 'Error' else - echo "Error: $MESSAGE" + echo 1>&2 "Error: $MESSAGE" fi if [ $USAGE -eq 1 ]; then - usage + usage 1>&2 fi if [ -z "$CODE" ]; then