Hello Geeks and Folks,
I hope you are all doing GREAT..!!
In fact, I started this topic at first as
a question to the way I can limit my CPU usage for some script or process.
I think, I got an answer to my question..
But also.. some good members suggested some great things, which motivated me to do extra work on my script.
Well, Since this is the first ever shell script I write, I aimed to turn it into a discussion, where me and other beginners would gain new skills and knowledge and also where we may learn from our mistakes.
My Script aims to create a compressed backup file of any MySQL database.
Here is where I got to with this script:
Code:
#!/bin/bash
#
# This script will generate a compressed backup copy of your MySQL
# Database dated and named same as you original database name.
#
# Writin by: Hasan A. Alsawadi - 2009
# Email: info@top4top.net
# Special thanks to:
# LinuxQuestions.Org
#
#===============================================================
#=============================================
# Check for database name, user and password :
#=============================================
# You can implicitly assign DB Username and DB Password
# in case you don't want to be prompted for them (ex. Cron Job).
# You may use Root and his Password, thus you gain access
# to the entire MySQL Server, But it's not recommended if
# you use this script to back up only one database.
# To do so, uncomment and fill the info below:
#dbusername='DB-Username'
#dbpassword='DB-Password'
if [[ $1 != '' ]]; then
dname="$1-$(date +%F).sql"
fname="$HOME/backups"
if [[ "$dbusername" = "" ]]; then
echo -n 'Please, enter DB Username: '
read dbusername
fi
if [[ "$dbpassword" = "" ]]; then
echo -n 'Please, enter DB Password: '
if read -s -t 30 dbpassword; then
echo ''
else
echo '
ERROR-4:
Sorry, you took too long to enter your Password..
For security reasons, the application was terminated.
Please, run the application again.' >&2
exit 4
fi
fi
else
echo 'ERROR-3:
You have to choose a database..!!
Fire the command like this: backupsql DatabaseName' >&2
exit 3
fi
#=======================================
# Check if backup folder exist, if not create it:
#=======================================
if [[ -e "$fname" ]]; then
echo "Target Directory: $fname"
sleep 2
else
mkdir $fname
echo "Target Directory: $fname"
sleep 2
fi
#====================================
# Generating the database SQL file:
#====================================
echo -n "Generating SQL File from database $1..."
if mysqldump -u"$dbusername" -p"$dbpassword" $1 > $fname/$dname; then
if [[ -r "$fname/$dname" ]]; then
echo ' Done..!!'
sleep 2
#======================================
# Comprissing the file to save disk
# space:
#======================================
echo -n 'Tarring the generated SQL file...'
cd $fname && tar -cf $dname.gz --gunzip $dname
if [[ -r "$fname/$dname.gz" ]]; then
echo ' Done..!!'
sleep 2
else
echo ' ERROR-2: Something went wrong with tarring..!!' >&2
exit 2
fi
#======================================
# Removing the Original SQL file:
#======================================
echo -n 'Removing the Original SQL file...'
rm -f $fname/$dname
echo ' Done..!!'
sleep 2
exit 0
else
echo 'ERROR-5: Fatal Error Somewhere..!!' >@2
exit 5
fi
else
echo ' ERROR-1: Something went wrong with connecting to MySQL Server..
Please, Check your Username and Password.' >&2
exit 1
fi
#=================================
# END
#=================================
Alright, in my opinion this script is mostly completed..
But, I still have 2 bugs, which I need help to solve.
BUG #1:
When the user is prompted for DB Username and Password, the scripts
wait 30 seconds for the user to supply his password.
If he didn't supply his pass, or didn't press Enter, the script gets terminated.
The Problems is, if for some reason, the user entered his password but didn't press ENTER, the script will terminate and reveal the password like below:
Code:
me@myserver# backupsql somedatabase
Please, enter DB Username: user
Please, enter DB Password: (I entered 12345 but didn't press ENTER)
ERROR-4:
Sorry, you took too long to enter your Password..
For security reasons, the application was terminated.
Please, run the application again.
me@myserver# 12345 <== Password revealed..!!
The Question is, how can I PREVENT the script of revealing the password when it's being terminated?
Don't know if there any help for this, I tried
clear but didn't solve it.
BUG #2:
When the command below is issued:
Code:
mysqldump -u"$dbusername" -p"$dbpassword" $1 > $fname/$dname
And, in case the Login information wasn't correct, or for any other error related to MySQL, the error message appear on the screen..
I don't want it to look like this, SO.. is there any way I can assign the error message to some variable, to be printed in a good way?
Here is the problem:
Code:
# backupsql somedatabase
Please, enter DB Username: rrr
Please, enter DB Password:
Target Directory: /root/backups
Generating SQL File from database somedatabase...mysqldump: Got error: 1045: Access denied for user 'rrr'@'localhost' (using password: YES) when trying to connect
ERROR-1: Something went wrong with connecting to MySQL Server..
Please, Check your Username and Password.
There are the only 2 issues I still have..
Also, please.. any other Ideas are Extremely Welcomed and Appreciated.
Opinions Are Treasures..
Thanks