Here is my script.
I have two problems. I have been reading all day and cannot find the answers. I turn to FreeBSD.org with hat in hand.
The two highlighted snippets of code are wrong, and for the life of me I cannot figure out why.
When I execute my script, I get this:
The mysqldump error doesn't store in the variable. I know this because when I comment the echo line out, it still show on &1:
I have tried every combination of output redirect I can think of, as well as could find examples for and cannot get what I want. I want all output (&>) to go into the variable for reporting.
Problem #2:
For some reason, the echo nested in the if statement will expand the ${dbs[$i]} but not the ${er_arr[$i]}
Any help would be greatly appreciated. Redirects to useful reading appreciated as well, if not more. I have been using Unix now for a few month, and I must say I love it but with caveats. If you need glasses but don't have any, get some before you try your hand at Unix.
Code:
[root@zap]/root/bin-> cat ./backup
#!/usr/local/bin/bash
user=XXXXXXXX #Mysql Username
pass=XXXXXXXX #Mysql Password
bd=/root/test #Backup Directory
# 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
lf=$ld/backup.log_$date
num=${#dbs[@]}
######################## Begin Script #########################
# Create Backup Dir and chmod if not there
if [ ! -d $bd ]; then
mkdir $bd $ld
chmod -R 700 $bd
echo "--- File created $date $time ---" >>$lf
fi
# Write Header to the logfile
echo "===================================================" >>$lf
echo "MYSQL DATABASE BACKUP STARTED AT $date $time" >>$lf
function dump {
mysqldump -u$user -p$pass --default-character-set=utf8 \
${dbs[$i]} > $bd/${dbs[$i]}_$date
}
for ((i=0 ; i < $num ; i++)); do
[color="Red"]er_arr[$i]=$(dump)[/color]
echo ${er_arr[$i]}
gzip -fq $bd/${dbs[$i]}_$date
if [ er_arr[$i] != "" ]; then
echo "DB:${dbs[$i]} failed with error(s): [color="red"]${er_arr[$i]}"[/color]
fi
done
# Write Footer to dump cycle
echo "MYSQL DATABASE BACKUP COMPLETED AT $date $time" >>$lf
exit
I have two problems. I have been reading all day and cannot find the answers. I turn to FreeBSD.org with hat in hand.
The two highlighted snippets of code are wrong, and for the life of me I cannot figure out why.
When I execute my script, I get this:
Code:
[root@zap]/root/bin-> ./backup
mysqldump: Got error: 1033: Table './mysql/general_log' was created with a different version of MySQL and cannot be read when using LOCK TABLES
DB:mysql failed with error(s):
DB:connect_db failed with error(s):
[root@zap]/root/bin->
The mysqldump error doesn't store in the variable. I know this because when I comment the echo line out, it still show on &1:
Code:
for ((i=0 ; i < $num ; i++)); do
er_arr[$i]=$(dump)
# echo ${er_arr[$i]}
gzip -fq $bd/${dbs[$i]}_$date
if [ er_arr[$i] != "" ]; then
echo "DB:${dbs[$i]} failed with error(s): ${er_arr[$i]}"
"backup" 48 lines, 1083 characters
[root@zap]/root/bin-> ./backup
mysqldump: Got error: 1033: Table './mysql/general_log' was created with a different version of MySQL and cannot be read when using LOCK TABLES
DB:mysql failed with error(s):
DB:connect_db failed with error(s):
[root@zap]/root/bin->
I have tried every combination of output redirect I can think of, as well as could find examples for and cannot get what I want. I want all output (&>) to go into the variable for reporting.
Problem #2:
For some reason, the echo nested in the if statement will expand the ${dbs[$i]} but not the ${er_arr[$i]}
Any help would be greatly appreciated. Redirects to useful reading appreciated as well, if not more. I have been using Unix now for a few month, and I must say I love it but with caveats. If you need glasses but don't have any, get some before you try your hand at Unix.