Reputation: 1323
I m creating cron job that takes backup of my entire DB. For that I used following code
*/5 * * * * mysqldump -u mydbuser -p mypassword mydatabase | gzip > /home/myzone/public_html/test.com/newfolder/dbBackup/backup.sql.gz
But instead of getting backup I m getting error as "syntax error near unexpected token `)' ". In my password there is round bracket included is this happening because of this. Please Help me..
Thanks in advance.
Upvotes: 0
Views: 1855
Reputation: 31453
As I suggested in my commentary, move this in external script, and include the script in cron.daily. I've given below the basic skeleton for such a script. This way you gain a couple of advantages => you can test the script, you can easily reuse it, it's also configurable. I don't know if you do this for administration or personal usage. My suggestion is more towards "I do it for administration" :)...
#!/bin/bash
# Backup destination directory
DIR_BACKUP=/your/backup/directory
# Timestamp format for filenames
TIMESTAMP=`date +%Y%m%d-%H%M%S`
# Database name
DB_NAME=your_database_name
# Database user
DB_USER=your_database_user
# Database password
DB_PASSWD=your_database_password
# Database export file name
DB_EXPORT=your_database_export_filename.sql
# Backup file path
BKFILE=$DIR_BACKUP/your-backup-archive-name-$TIMESTAMP.tar
# Format for time recordings
TIME="%E"
###########################################
# Create the parent backup directory if it does not exist
if [ ! -e $DIR_BACKUP ]
then
echo "=== Backup directory not found, creating it ==="
mkdir $DIR_BACKUP
fi
# Create the backup tar file
echo "=== Creating the backup archive ==="
touch $BKFILE
# Export the database
echo "=== Exporting YOUR DATABASE NAME database ==="
time bash -c "mysqldump --user $DB_USER --password=$DB_PASSWD $DB_NAME > $DIR_BACKUP/$DB_EXPORT"
# Add the database export to the tar file, remove after adding
echo "=== Adding the database export to the archive ==="
time tar rvf $BKFILE $DIR_BACKUP/$DB_EXPORT --remove-files
# Compress the tar file
echo "=== Compressing the archive ==="
time gzip $BKFILE
# All done
DATE=`date`
echo "=== $DATE: Backup complete ==="
Upvotes: 0
Reputation: 4347
try to remove spaces between -u mydbuser and -p mypassword..
-umydbuser -pmypassword
Upvotes: 0
Reputation: 263267
)
is a special character for the shell (and crontab uses the shell to execute commands).
Add single quotes around your password:
*/5 * * * * mysqldump -u mydbuser -p 'mypassword' mydatabase | ...
Upvotes: 1