Strange error in bash script

Howdy all, I have a scritp that does a sqldump. But for some goofy reason, a certain part of it behaves uber strange.
It does a daily dump of my sql, according to parameters I enter in hardcode.

The script is:

#!/bin/bash

APP_NAME="app_com_site"
wikiname="wiki_com_site"

BACKUP_DIR="/home/app/backup/DAILY"      

DB_NAME="app_com_db"
DB_USER="app_com_admi"
DB_PASS="notgonnatellyou"

# Number of Backup files to save
FILES_TO_KEEP=2

##################
# END OF OPTIONS #
##################

timestamp=`date +%Y-%m-%d`

dbdump="$BACKUP_DIR/$APP_NAME-$timestamp.sql.gz"
filedump="$BACKUP_DIR/$APP_NAME-$timestamp.files.tgz"

#Create a MySQL dump of the database
echo "creating database dump:" $dbdump
mysqldump --database $DB_NAME -u $DB_USER -p$DB_PASS --add-drop-table -B | gzip > test.gz
#mysqldump --database $DB_NAME -u $DB_USER -p$DB_PASS --add-drop-table -B  | gzip >  "$dbdump"

The thing is that the commented line is NOT creating the file, but the uncommented one IS working. I do want this to be dynamically.

I couldn't find any errors. can anyone have a clue please?

Hello ,

Add 'set -x' to the beginning of the script

Regards,
Gaurav.

Try to use braces around the variable names on these lines:

dbdump="$BACKUP_DIR/$APP_NAME-${timestamp}.sql.gz"
filedump="$BACKUP_DIR/$APP_NAME-${timestamp}.files.tgz"

Thanks for your ideas

set -x did gave me the output, but no apparent error what so ever

+ . ../SCRIPTS/test.sh
++ set -x
++ APP_NAME=app_com_site
++ wikiname=app_com_site
++ BACKUP_DIR=/home/app/backup/DAILY
++ DB_NAME=app_com_db
++ DB_USER=app_com_admi
++ DB_PASS=ermno
++ FILES_TO_KEEP=2
+++ date +%Y-%m-%d
++ timestamp=2010-01-25
++ dbdump=/home/app/backup/DAILY/app_com_site-2010-01-25.sql.gz
++ filedump=/home/app/backup/DAILY/app_com_site-2010-01-25.files.tgz
++ echo 'creating database dump:' /home/app/backup/DAILY/app_com_site-2010-01-25.sql.gz
creating database dump: /home/optitex/backup/DAILY/app_com_site-2010-01-25.sql.gz
++ gzip
++ mysqldump --database app_com_db -u appx_com_admi -permpass --add-drop-table -B
++ pushd /home/app/public_html
~/public_html ~/backup/DAILY
++ popd
~/backup/DAILY
++ echo 'Done!'
Done!

Adding the brackets to my source code, didn't change the result.

I started to think that it's permission issue.
The target folder is 755, and owned by the same user that is running the script (other user that runs the cron job is root - at night run). so I don't think it's permissions.

Why is it that if I put a direct file name it works, but using a parameter it doesn't?

---------- Post updated at 03:09 PM ---------- Previous update was at 02:42 PM ----------

I had another try, to see if I can make this work in a different way:

These two lines are working, the name of the output sql file and gz file are hardcoded.

mysqldump --database $DB_NAME -u $DB_USER -p$DB_PASS --add-drop-table -B > "$BACKUP_DIR"/dump.sql

gzip "$BACKUP_DIR"/dump.sql

These 2 lines are NOT working

mysqldump --database $DB_NAME -u $DB_USER -p$DB_PASS --add-drop-table -B > "$BACKUP_DIR"/"$APP_NAME-$timestamp.sql.gz"

gzip "$BACKUP_DIR"/"$APP_NAME-$timestamp.sql.gz"

My guess is that something is weird with the timestamp naming? how can that be?

Hi try this ,

mysqldump --database $DB_NAME -u $DB_USER -p$DB_PASS --add-drop-table -B  | gzip >  "${dbdump}.gz"

Regards,
Gaurav.

Also make sure you are able to create file in /home/app/backup/DAILY/ folder

Thanks all for your comments.

I had to try and see where the problem, so I creeated a new var: sqlfile="/home/app/backup/DAILY/dump.sql"
and added that to the script.

sqlfile="/home/app/backup/DAILY/file.sql"

#Create a MySQL dump of the database
echo "creating database dump:" $dbdump
#mysqldump --database $DB_NAME -u $DB_USER -p$DB_PASS --add-drop-table -B | gzip > "$BACKUP_DIR"/"$APP_NAME-$timestamp.sql.gz"
mysqldump --database $DB_NAME -u $DB_USER -p$DB_PASS --add-drop-table -B  | gzip >  "${sqlfile}.gz"

amazingly everything works (@xoops = that answers te Q if I can write to DAILY folder, thanks)

So now I am left with the puzzle, why when I use the following:

dbdump="$BACKUP_DIR/$APP_NAME-${timestamp}.sql"

the script is not working.

printing out the value of $dbdump gives the correct folder. but nothing is saved there, that is weird with a capital 'W' !!!

---------- Post updated at 03:30 PM ---------- Previous update was at 03:27 PM ----------

The command:

echo "creating database dump:"$dbdump":"

outputs the following:

creating database dump:/home/app/backup/DAILY/app_com_site-2010-01-25.sql:

but again, there is nothing there.

Maybe "mysqldump" reacts to the setting of ${dbdump} ? Just a guess.

Thanks for your idea. I renamed dbdump to filedump. that didn't change the result.

Still no file is saved.