LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


View Poll Results: Do you think this script is helpful?
Yes, Sure it's.. and it's a good idea..!! 1 25.00%
No, it's such a waste of time..!! 1 25.00%
Maybe.. Don't Know.. Don't Care..!! 2 50.00%
Voters: 4. You may not vote on this poll

Reply
  Search this Thread
Old 09-24-2009, 07:10 AM   #1
WhisperiN
Member
 
Registered: Jun 2009
Location: Middle East
Distribution: Slackware 13.1, CentOS 5.5
Posts: 137

Rep: Reputation: 17
Cool Shell Scripting Geeks, advice and assist needed :-)


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

Last edited by WhisperiN; 09-24-2009 at 07:29 AM.
 
Old 09-24-2009, 08:21 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by WhisperiN View Post
The Question is, how can I PREVENT the script of revealing the password when it's being terminated?
Code:
		if [[ "$dbpassword" = "" ]]; then
			echo -n 'Please, enter DB Password: '
			if read -s -t 30  dbpassword; then
				echo ''
			else
				while read -n 1 -t 1
                                do
                                        :
                                done
                                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
It's such a minimal loop you might want to turn it into a one liner.
 
Old 09-24-2009, 08:30 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by WhisperiN View Post
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:
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.
Code:
mysqldump_err="$(mysqldump -u"$dbusername" -p"$dbpassword" "$1" 2>&1 > $fname/$dname)"
rc=$?
if [[ $rc -ne 0 || "$mysqldump_err" != '' ]]; then
    echo "ERROR-1: Something went wrong with mysqldump.
    Return code: $?
    Error message: $mysqldump_err"
fi
 
Old 09-24-2009, 08:38 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by WhisperiN View Post
Code:
		echo 'ERROR-5: Fatal Error Somewhere..!!' >@2
Also, please.. any other Ideas are Extremely Welcomed and Appreciated.
That @ should be an &

Nice looking script. One way of making it more robust would be to ensure it uses standard shell built-ins and not any aliases of the same name by prefixing them with a \, thus exit becomes \exit. Other built-ins used are "echo" and "read".
 
Old 09-24-2009, 05:46 PM   #5
WhisperiN
Member
 
Registered: Jun 2009
Location: Middle East
Distribution: Slackware 13.1, CentOS 5.5
Posts: 137

Original Poster
Rep: Reputation: 17
A Typo..

Quote:
Originally Posted by catkin View Post
That @ should be an &

Nice looking script. One way of making it more robust would be to ensure it uses standard shell built-ins and not any aliases of the same name by prefixing them with a \, thus exit becomes \exit. Other built-ins used are "echo" and "read".
Yup, that was a typo..

Humm..

Alright, for the prefix you pointed to..
Dose that apply to all the commands?
Like, echo, sleep, if, else ...etc ??

Or there is kinda a rule to follow?


CatKin,
Allow me to say, you are amazing..
You Rock like a Star :P.

You really added great additions..
That's not odd when we are talking about Indians


I repeat my thanks..
 
Old 09-24-2009, 11:29 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Code:
mysqldump_err="$(mysqldump -u"$dbusername" -p"$dbpassword" "$1" 2>&1 > $fname/$dname)"
rc=$?
if [[ $rc -ne 0 || "$mysqldump_err" != '' ]]; then

    echo "ERROR-1: Something went wrong with mysqldump.
    Return code: $?
That (2nd) $? is the rtn code of echo; not what you wanted I think. Try

echo $rc

 
Old 09-25-2009, 01:02 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by WhisperiN View Post
Alright, for the prefix you pointed to..
Dose that apply to all the commands?
Like, echo, sleep, if, else ...etc ??

Or there is kinda a rule to follow?
Yep. Only the built-in commands, so that does not include "if" and "else" which are keywords. Mostly it's a nicety and might never cause a problem but I once had a hard to find bug caused by an alias for exit.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
needed help in finding a book ,pls assist wrapster Solaris / OpenSolaris 4 07-05-2008 10:53 AM
Needed jdk install help, kinda urgent pls assist!!!!! wrapster Linux - Software 2 06-02-2008 08:19 AM
Bash Scripting newb.. Advice needed. trey85stang Linux - General 5 09-28-2006 12:05 PM
shell scripting - help needed jonhewer Linux - Newbie 54 09-09-2005 02:58 PM
Shell scripting and background processes - help needed. trafalgar Programming 3 06-08-2003 09:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration