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.