<p>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 <code>mysqldump</code> gives you that error.</p>

CC BY-SA 4.0
2021-10-27T17:20:19.243Z
2021-10-27T17:20:19.243Z
Jesusaur
<p>I have written this script that performs db backup</p>
<pre><code>#!/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 &quot;Must be root to run script&quot;
    exit
fi

if test -z &quot;$HOST&quot;
then
  echo &quot;HOST not passed.&quot;
  exit 1
fi

if test -z &quot;$DB_PORT&quot;
then
  echo &quot;PORT not passed.&quot;
  exit 1
fi

# remove backups older than $DAYS_KEEP
DAYS_KEEP=7
find ${DIR}* -mtime +$DAYS_KEEP -exec rm -f {} \; 2&gt; /dev/null
# create backups securely
umask 006
# list MySQL databases and dump each
DB_LIST=`mysql -h $HOST -u $DB_USER -p&quot;$DB_PASS&quot; -e'show databases;'`
DB_LIST=${DB_LIST##Database}
for DB in $DB_LIST;
do
 if [[ &quot;${DB}&quot; == &quot;performance_schema&quot; ]]
 then
  echo &quot;Skipping database ${DB}.&quot;
  continue
 fi
 FILENAME=${DIR}${DB}-${DATESTAMP}.msql.${DB_PORT}.gz
 echo &quot;Initiating backup of $DB for ${HOST} on port ${DB_PORT}&quot;
 mysqldump -h $HOST -P $DB_PORT -u $DB_USER -p&quot;$DB_PASS&quot; $DB --single-transaction | gzip &gt; $FILENAME
done
</code></pre>
<p>the script above is called from jenkins job.</p>
<p>How can I exit jenkins job if I get this error:</p>
<pre><code>ERROR 1045 (28000): Access denied for user 'backup'@'172.118.1.21' (using password: YES)
</code></pre>

CC BY-SA 4.0
2021-10-23T07:16:39.753Z
I don't use mysql, but seems it's an [error from `mysqldump`](https://serverfault.com/a/249869/189417). 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
CC BY-SA 4.0
2023-03-28T19:39:38.873Z
@IanW - `$?` after the `mysqldump | gzip` line will reflect `gzip` exit status.
sborsky
<jenkins><shell-script>
How to exit a jenkins job if a script executing gets access denied?
Chang Zhao
# How to exit a jenkins job if a script executing gets access denied?

(Asked by: username_0 on 2021-10-22)

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)

**Tags:** jenkins, shell-script

> **Comments:**
> **username_1**: I don't use mysql, but seems it's an [error from `mysqldump`](https://serverfault.com/a/249869/189417). 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.  
> **username_2**: @IanW - `$?` after the `mysqldump | gzip` line will reflect `gzip` exit status.  

---

## 1 answers

### Answer by username_3 on 2021-10-27. Score: 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.


---

