I import data into MySQL over and over again. Always from .sql scripts. I have tried over and over to come up with a script that will

  1. Ask for a database name
  2. Sign into MySQL
  3. Drop the Database named in step 1
  4. Create a new database as named in step 1
  5. Import the file I right-clicked on (like source /home/me/Desktop/database.sql)

Any help would be appreciated.

Recommended Answers

All 4 Replies

Shell is probably not the ideal tool for an interactive (right click to choose) program. Shell is better suited to a command line interface, where for instance, you would type in (maybe with auto-completion) the name of the .sql file.

In this case, I'd likely create a temporary sql file that does steps 3, 4 an 5 and have the MySQL interpreter run that file from the command line.

Shell is probably not the ideal tool for an interactive (right click to choose) program. Shell is better suited to a command line interface, where for instance, you would type in (maybe with auto-completion) the name of the .sql file.

In this case, I'd likely create a temporary sql file that does steps 3, 4 an 5 and have the MySQL interpreter run that file from the command line.

I figured it out with some help. All right click actions in Linux are shell scripts, they are normally kept in places like ~/.gnome2/nautilus-scripts. As a matter of fact almost everything you do in linux is either a compiled binary or a shell script, even while in Gnome or KDE, with the vast majority of them being scripts of some kind.

This is the script I came up with

#!/bin/bash
# Get domain name
_zenity="/usr/bin/zenity"
_out="/tmp/whois.output.$$"
database=$(${_zenity} --title  "Enter Database Name" \
	            --entry --text "Enter the name of the Database you want this data imported into." )
 
if [ $? -eq 0 ]
then
  # Import into the database
echo "DROP DATABASE IF EXISTS $database;CREATE DATABASE $database;USE $database;" > /home/ken/tmp.sql
notify-send -t 1000 -i gtk-dialog-info "Preparing to Import Database: $database"
mysql -u root -pPASSWORD -h localhost < /home/ken/tmp.sql
notify-send -t 1000 -i gtk-dialog-info "Starting Import to Database: $database"
mysql -v -u root -pPASSWORD -h localhost $database < $1 | tee >(zenity --progress --pulsate --auto-close --auto-kill) > ~/mysql_import.txt
notify-send  -t 1000 -i gtk-dialog-info "Updating Admin user"
echo "UPDATE $database.users SET user_name = 'admin',user_hash = MD5('sugarcrm') ,is_admin = '1',status = 'Active',employee_status = 'Active' WHERE users.id = '1';"  > /home/ken/tmp.sql 
mysql -u root -pPASSWORD -h localhost < /home/ken/tmp.sql
notify-send  -t 1000 -i gtk-dialog-info "Finished Import to Database: $database"
else
  ${_zenity} --error \
	     --text="No input provided"
fi

<<mistake>>

Now the progress meter actuly show progress instead of just activity.

#!/bin/bash
# Get domain name
_zenity="/usr/bin/zenity"
_out="/tmp/mysql.output.$$"
database=$(${_zenity} --title  "Enter Database Name" \
	            --entry --text "Enter the name of the Database you want this data imported into." )
 
if [ $? -eq 0 ]
then
  # Import into the database
echo "DROP DATABASE IF EXISTS $database;CREATE DATABASE $database;USE $database;" > ~/tmp.sql
notify-send -t 1000 -i gtk-dialog-info "Preparing to Import Database: $database"
mysql -u root -pPASSWORD -h localhost < ~/tmp.sql
notify-send -t 1000 -i gtk-dialog-info "Starting Import to Database: $database"
(pv -n $1 | mysql -u root -pPASSWORD -D $database) 2>&1 | zenity --width 550 --progress --auto-close --auto-kill --title "Importing into MySQL" --text "Importing into $database"
notify-send  -t 1000 -i gtk-dialog-info "Finished Import to Database: $database"
else
  ${_zenity} --error \
	     --text="No input provided"
fi
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.