1

I'm wring a script that I plan to schedule by cron for 1AM each morning to backup a mySql DB.

Normally I use this to dump the database:

mysqldump --no-create-db --single-transaction myDB | gzip > ~/my_backup.sql.gz

In my head what I have written should:

  • Dump the DB, Write any errors to database.err
  • Pipe the output to gzip which then zips it up and writes to disk
  • Read the return code, assuming success write the file to a S3 bucket
  • For the purposes of testing writes current state to the shell

#!/bin/bash

# This script will run each night to backup
# the mySql DB then will upload to Amazon S3

DB_NAME="myDB"

S3_BUCKET="my.s3.bucket"

BACKUP_PATH="~/backups/${DB_NAME}.sql.gz"

mysqldump --no-create-db --single-transaction ${DB_NAME} 2> database.err | gzip > ${BACKUP_PATH}
if [ "$?" -eq 0 ]
then
    echo "Database dump complted sucessuflly... wtiting to S3"
    aws s3 cp ${BACKUP_PATH} s3://${S3_BUCKET}/
    if [ "$?" -eq 0 ]
    then
        echo "Backup sucessfully written to S3"
    else
        echo "Error writing to S3"
    fi
else
    echo "Mysqldump encountered a problem look in database.err for information"
fi

What it looks like the script is doing is getting to the mysqldump line, but is unable to differentiate between the parameter where i specify the DB and the 2> (file descriptor I think is the term?). This is the error:

./backup-script: line 12: ~/backups/myDB.sql.gz: No such file or directory
mysqldump: Got error: 1049: Unknown database 'myDB 2' when selecting the database
Mysqldump encountered a problem look in database.err for information

Can anyone suggest what is happening here/what I'm doing wrong.

Itchydon
  • 2,572
  • 6
  • 19
  • 33
Craig
  • 222
  • 3
  • 9
  • `~` isn't expanded in variables or variable expansion. – Kevin Aug 29 '17 at 23:15
  • In other words, use `BACKUP_PATH=~/"backups/${DB_NAME}.sql.gz"` or, better yet, `BACKUP_PATH="$HOME/backups/${DB_NAME}.sql.gz"` – John1024 Aug 29 '17 at 23:37
  • One possibility for the cause of `Unknown database 'myDB 2'` is the space between `${DB_NAME}` and `2>` being something other than a standard ASCII space. Problems of this type happen if one creates scripts in a word processor instead of a programmer's editor. This is more common that you might think. – John1024 Aug 29 '17 at 23:40
  • 1
    I have got myself into a habit or re-typing code that doesn't work. I have found copying from stackoverflow in some cases puts a zero-length space on the clipboard, and copying from hipchat to PhpStorm has given me some issues before as well. I can't remember if I have done it in this instance but will give that a try see if it let me put the command in the order I normally do for the sake of consistencey. – Craig Aug 29 '17 at 23:47

1 Answers1

1

Try putting the database name first

mysqldump "${DB_NAME}" --no-create-db --single-transaction 
Kamran
  • 843
  • 1
  • 8
  • 19
  • 1
    Also thanks to Kevin and John1024 for explaining about ~ and suggesting $HOME. Now whit is working I have realised I have captured the exit code of gzip but found this https://stackoverflow.com/questions/1221833/pipe-output-and-capture-exit-status-in-bash to help me out. Thanks all! – Craig Aug 29 '17 at 23:42