Question

I have a shell script that I run in launchd that backs up my MAMP MySQL install. Unfortunately if MAMP isn't running it throws an error and sends it to internal mail. I want any errors to be ignored (sent to /dev/null), but if there isn't an error, to continue on.

I have attempted the following, which correctly creates the backup when MAMP is running, but sends mail if it's not running (stderr):

#!/bin/bash
/Applications/MAMP/Library/bin/mysqldump --opt -u root -pPASSWORDHERE --host=localhost --all-databases > | gzip -9 > /Users/Paul/Dropbox/Development/Shared\ DBs/BACKUPS/all_databases_-`date "+%Y-%m-%d_%H.%M.%S"`.sql.gz

And I also tried this:

#!/bin/bash
/Applications/MAMP/Library/bin/mysqldump --opt -u root -pPASSWORDHERE --host=localhost --all-databases 2> /dev/null 1> | gzip -9 > /Users/Paul/Dropbox/Development/Shared\ DBs/BACKUPS/all_databases_-`date "+%Y-%m-%d_%H.%M.%S"`.sql.gz

But this does nothing at all...

If there's an error I need:

  • Error to be sent to /dev/null
  • No Gzipped file to be created

If there's no error I need:

  • A Gzipped mysqldump to be created

Does anyone know where I'm going wrong?

Thank you in advance for your help.

Edit as per below

I now have:

#!/bin/bash
# Create the backup name
BACKUP_NAME="/Users/Paul/Dropbox/Development/Shared DBs/BACKUPS/all_databases_-$(date "+%Y-%m-%d_%H.%M.%S").sql"
# Run mysqldump and create a raw backup
/Applications/MAMP/Library/bin/mysqldump --opt -u root -proot --host=localhost --all-databases 2> /dev/null > ${BACKUP_NAME}
# If no errors
if [$? = 0]
then
  # Create the gzip backup
  gzip -c9 ${BACKUP_NAME} > ${BACKUP_NAME}.gz
fi
# In any cases remove raw backup
rm -f ${BACKUP_NAME}

but this doesn't produce a file even when there are no errors.

Edit - Finished Code

Using JML's updated code below (many thanks), I was still getting errors, but this was because my path had a space in it, therefore needed quote marks in two places.

Final, working code:

#!/bin/sh

# Create the backup name
BACKUP_NAME="/Users/Paul/Dropbox/Development/Shared DBs/BACKUPS/all_databases_-$(date +%Y-%m-%d_%H.%M.%S).sql"

# Run mysqldump and create a raw backup
/Applications/MAMP/Library/bin/mysqldump --opt --user=root --password=root --host=localhost --all-databases 2> /dev/null >${BACKUP_NAME}

# If no errors, create the gzip backup
test $? = 0 && gzip -c9 "${BACKUP_NAME}" > ${BACKUP_NAME}.gz

# In any cases remove raw backup
rm -f "${BACKUP_NAME}" 
Was it helpful?

Solution

Why not splitting ? Something like this:

#!/bin/sh

# Create the backup name
BACKUP_NAME="/tmp/backup-$(date +%Y-%m-%d_%H.%M.%S).sql"

# Run mysqldump and create a raw backup
mysqldump --opt --user=root --password=root --host=localhost --all-databases 2>/dev/null >"${BACKUP_NAME}"

# If no errors, create the gzip backup
test $? = 0 && gzip -c9 "${BACKUP_NAME}" > "${BACKUP_NAME}.gz"

# In any cases remove raw backup
rm -f "${BACKUP_NAME}"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top