1

I have written this script that performs db backup

#!/bin/bash

#For taking backup
DIR=/media/storage/backup/db_backup/
DATESTAMP=$(date +%d-%m-%y-%H-%M)
DB_USER=backup
DB_PORT=$2
DB_PASS='secret'
HOST=$1

if [[ `id -u` != 0 ]]; then
    echo "Must be root to run script"
    exit
fi

if test -z "$HOST"
then
  echo "HOST not passed."
  exit 1
fi

if test -z "$DB_PORT"
then
  echo "PORT not passed."
  exit 1
fi

# remove backups older than $DAYS_KEEP
DAYS_KEEP=7
find ${DIR}* -mtime +$DAYS_KEEP -exec rm -f {} \; 2> /dev/null
# create backups securely
umask 006
# list MySQL databases and dump each
DB_LIST=`mysql -h $HOST -u $DB_USER -p"$DB_PASS" -e'show databases;'`
DB_LIST=${DB_LIST##Database}
for DB in $DB_LIST;
do
 if [[ "${DB}" == "performance_schema" ]]
 then
  echo "Skipping database ${DB}."
  continue
 fi
 FILENAME=${DIR}${DB}-${DATESTAMP}.msql.${DB_PORT}.gz
 echo "Initiating backup of $DB for ${HOST} on port ${DB_PORT}"
 mysqldump -h $HOST -P $DB_PORT -u $DB_USER -p"$DB_PASS" $DB --single-transaction | gzip > $FILENAME
done

the script above is called from jenkins job.

How can I exit jenkins job if I get this error:

ERROR 1045 (28000): Access denied for user 'backup'@'172.118.1.21' (using password: YES)
2
  • 1
    I don't use mysql, but seems it's an error from mysqldump. You should be able to test by adding after that, EXITCODE=$?, which should capture the result, then [[ $? != 0 ]] && echo "Error: $EXITCODE" && exit $EXITCODE. Also your exit in not root test should exit 1. Assumes you want top stop job on firat failure.
    – Ian W
    Oct 23, 2021 at 7:16
  • @IanW - $? after the mysqldump | gzip line will reflect gzip exit status.
    – sborsky
    Mar 28, 2023 at 19:39

1 Answer 1

1

The jenkins job will pass or fail based on the exit code of the script. You need to make sure you exit with a non-zero value when mysqldump gives you that error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.