Skip to content

Instantly share code, notes, and snippets.

@flickerfly
Created May 24, 2018 14:50
Show Gist options
  • Save flickerfly/1da964009ccf3dd80e551157a1ac6cfe to your computer and use it in GitHub Desktop.
Save flickerfly/1da964009ccf3dd80e551157a1ac6cfe to your computer and use it in GitHub Desktop.

Revisions

  1. flickerfly created this gist May 24, 2018.
    51 changes: 51 additions & 0 deletions backup_mysqldump_error_reporting.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    # What to do when mysqldump fails
    function report_mysqldump_fail() {
    cat $scratch/${filename}_raw.err >> $log
    mailx -s "mysqldump failed for DB $db_name on $HOSTNAME!!!" [email protected] < $log
    exit 2
    }

    # How to report a step along the process
    function status_report() {
    message=$1

    if [[ -z $2 ]]; then
    verbose_level=1
    else
    verbose_level=$2
    fi

    if [[ $verbose_level -le $verbosity ]]; then
    now=$(date +"%d-%m-%y %T")
    echo "$orgid $now $message" >> "/var/log/backup_worker/${orgid}.log"
    fi
    }

    # Back something up
    mysqldump -h $db_host --databases $db_name

    # Check the return code and report accordingly
    if [[ $? = 0 ]]; then
    status_report "mysqldump succeeded" 2
    elif [[ $? = 1 ]]; then
    status_report "mysqldump EX_USAGE 1 <-- command syntax issue" 1
    report_mysqldump_fail
    elif [[ $? = 2 ]]; then
    status_report "mysqldump EX_MYSQLERR 2 <-- privilege problem or other issue completing the command" 1
    report_mysqldump_fail
    elif [[ $? = 3 ]]; then
    status_report "mysqldump EX_CONSCHECK 3 <-- consistency check problem" 1
    report_mysqldump_fail
    elif [[ $? = 4 ]]; then
    status_report "mysqldump EX_EOM 4 <-- End of Memory" 1
    report_mysqldump_fail
    elif [[ $? = 5 ]]; then
    status_report "mysqldump EX_EOF 5 <-- Result file problem writing to file, space issue?" 1
    report_mysqldump_fail
    elif [[ $? = 6 ]]; then
    status_report "mysqldump EX_ILLEGAL_TABLE 6" 1
    report_mysqldump_fail
    else
    status_report "Backup presumed failed Unknown exit code $? from mysqldump" 1
    report_mysqldump_fail
    fi