Solved [Solved] bash/gzip fail

Absolutely maddening. x(
Trying to write my first script.
Can anyone tell me why I keep ending up with empty gzipped files?

Code:
[root@zap]/root/bin-> cat db.backup
#!/usr/local/bin/bash

user=XXXXXXXX   #Mysql Username
pass=XXXXXXXX   #Mysql Password
bd=/root/test   #Backup Directroy

# Database Names to back up
declare -a dbs=(mysql connect_db)
date=`date +%m-%d-%y`
time=`date '+TIME: %H:%M:%S'`
ld=$bd/log
ed=$bd/error
lf=$ld/backup.log_$date
ef=$ed/error.log_$date
num=${#dbs[@]}

######################## Begin Script #########################

# Create Backup Dir and chmod if not there
if [ ! -d $bd ]; then
        mkdir $bd $ld $ed
        chmod -R 700 $bd
        echo "--- File created $date $time ---" >>$lf
        echo "--- File created $date $time ---" >>$ef
fi

# Write Header to the logfile

echo "===================================================" >>$lf
echo "MYSQL DATABASE BACKUP STARTED AT $date $time" >>$lf

echo "Number of Databases = $num"
for ((i=0 ; i < $num ; i++)); do
        mysqldump -u$user -p$pass --default-character-set=utf8 ${dbs[$i]} > $bd/${dbs[$i]}_RAW
        gzip -q $bd/${dbs[$i]}_RAW > $bd/${dbs[$i]}_$date.gz && rm $bd/${dbs[$i]}_RAW
        done
exit
# Write Footer to dump cycle

echo "MYSQL DATABASE BACKUP COMPLETED AT $date $time" >>$lf

exit


[root@zap]/root/bin->

Code:
[root@zap]/root/bin-> ./db.backup
Number of Databases = 2
mysqldump: Got error: 1033: Table './mysql/general_log' was created with a different version of MySQL and cannot be read when using LOCK TABLES
rm: /root/test/mysql_RAW: No such file or directory
rm: /root/test/connect_db_RAW: No such file or directory
[root@zap]/root/bin->

Code:
[root@zap]/root/bin-> ls -la /root/test
total 172
drwx------  4 root  wheel     512 Jul 18 08:16 .
drwxr-xr-x  4 root  wheel     512 Jul 18 08:15 ..
-rw-r--r--  1 root  wheel       0 Jul 18 08:16 connect_db_07-18-12.gz
-rw-r--r--  1 root  wheel  153282 Jul 18 08:16 connect_db_RAW.gz
drwx------  2 root  wheel     512 Jul 18 08:15 error
drwx------  2 root  wheel     512 Jul 18 08:15 log
-rw-r--r--  1 root  wheel       0 Jul 18 08:16 mysql_07-18-12.gz
-rw-r--r--  1 root  wheel     303 Jul 18 08:16 mysql_RAW.gz
[root@zap]/root/bin->
 
I'm guessing your temporary file never gets created or is empty. The mysqldump error probably has something to do with it.

You can pipe the output of mysqldump straight into gzip. There really isn't a need for a temporary file.
 
I have tried to pipe if directly in. But for some reason, the variable wasn't getting expanded correctly after the pipe.

This is much more trouble than I thought it would be.

the gzip command in the script doesn't produce anything when I run it directly from shell

if I try and gzip anything,
gzip file
I get
Code:
file.gz already exists -- do you wish to overwrite (y or n)?
even though file is the only thing in the dir!
 
The answer is staring you right in the face.

Code:
mysqldump: Got error: 1033: Table './mysql/general_log' was created with a different version of MySQL and cannot be read when using LOCK TABLES

mysqldump errors and therefor never creates the temporary file.
 
But look again at the ls above. The RAW files have data. I looked up that error and it is nothing to be concerned about on this machine since I know the ultimate destination for this script is a machine that wont give that error. When I run gzip on the raw files from shell, i get empty files also.
 
You're not using gzip(1) correctly.

Code:
 When in compression mode, each
     file will be replaced with another file with the suffix, set by the -S
     suffix option, added, if possible.

Code:
dice@vps-2417-1:~/test>ll
total 2
-rw-r--r--  1 dice  dice  1024 Jul 18 15:26 test
dice@vps-2417-1:~/test>gzip test
dice@vps-2417-1:~/test>ll
total 2
-rw-r--r--  1 dice  dice  1052 Jul 18 15:26 test.gz
dice@vps-2417-1:~/test>rm test.gz
dice@vps-2417-1:~/test>
dice@vps-2417-1:~/test>dd if=/dev/random of=test bs=1k count=1
1+0 records in
1+0 records out
1024 bytes transferred in 0.000138 secs (7417906 bytes/sec)
dice@vps-2417-1:~/test>
dice@vps-2417-1:~/test>gzip -c test > test.gz
dice@vps-2417-1:~/test>ll
total 4
-rw-r--r--  1 dice  dice  1024 Jul 18 15:27 test
-rw-r--r--  1 dice  dice  1052 Jul 18 15:27 test.gz
 
I should have never questioned you.
Sorry. It was the 23 hour straight of staring at putty that made me crazy.:p
 
Edit your first post. Click on "Go Advanced" and you'll be able to change the title of the thread and set it to "Solved".
 
Back
Top