Search code examples
mysqllinuxbashbackup

Bash script backup full MySQL


The next script apparently executes successfully cause I get the backup but when I modify some parameters for checking the output of mysqldump all time send an email with the messages "El respaldo diario de la base de datos se ha realizado correctamente" (the backup executed successfully)

`#!/bin/bash -x
SERVIDOR="$(hostname)"
PASSWORD='PASSWORD'
FECHA=`/bin/date +\%Y\%m\%d`
NOTIFICADOS=notificados@hotmail.com
STATUSFILE="/tmp/statusfile.$FECHA"

echo "Backup report from $FECHA" > $STATUSFILE
mysqldump  --databases prueba --no-tablespaces --skip-comments --single-transaction --default-character-set=UTF8 --insert-ignore --complete-insert  --add-locks --triggers --routines --events --disable-keys --lock-tables=false --set-gtid-purged=OFF --user=backup -p$PASSWORD > /tmp/$SERVIDOR-DBFLBASEPRUEBA-$FECHA.sql
echo "Muestra la salida de mysqldump: $?" >> $STATUSFILE

if [ $? -eq 0 ]
then
   echo "El respaldo diario de la base de datos "prueba" en el $SERVIDOR se ha realizado correctamente" >> $STATUSFILE
  else
   echo "El respaldo diario de la base de datos "prueba" en el $SERVIDOR no se ha realizado correctamente" >> $STATUSFILE
  fi

gzip -9 /tmp/$SERVIDOR-DBFLBASEPRUEBA-$FECHA.sql`

I executed the script with --no-tablespace the value of mysqldump is 2 but the message said that the backup executed successfully. Can you help me about the correct way to detect an issue, please?

enter image description here

enter image description here


Solution

  • Basically what 'F. Hauri` said in the comments above.

    #!/bin/bash -x
    SERVIDOR="$(hostname)"
    PASSWORD='PASSWORD'
    FECHA=$(/bin/date +\%Y\%m\%d)
    NOTIFICADOS=notificados@hotmail.com
    STATUSFILE="/tmp/statusfile.$FECHA"
    
    echo "Backup report from $FECHA" > "$STATUSFILE"
    mysqldump  --databases prueba --no-tablespaces --skip-comments --single-transaction --default-character-set=UTF8 --insert-ignore --complete-insert  --add-locks --triggers --routines --events --disable-keys --lock-tables=false --set-gtid-purged=OFF --user=backup -p$PASSWORD > /tmp/"$SERVIDOR"-DBFLBASEPRUEBA-"$FECHA".sql
    RESULTADO=$?
    echo "Muestra la salida de mysqldump: ${RESULTADO}" >> "$STATUSFILE"
    
    if [ ${RESULTADO} -eq 0 ]
    then
       echo "El respaldo diario de la base de datos 'prueba' en el $SERVIDOR se ha realizado correctamente" >> "$STATUSFILE"
    else
       echo "El respaldo diario de la base de datos 'prueba' en el $SERVIDOR no se ha realizado correctamente" >> "$STATUSFILE"
    fi
    gzip -9 /tmp/"$SERVIDOR"-DBFLBASEPRUEBA-"$FECHA".sql
    

    Added a few more smaller cleanups, and you don't seem to use $NOTIFICADOS anywhere.