6

I'm new to mysql. My requirement is to create a shell script to import a sql dump file into mysql in linux and this script should be called by java program for the restoration to take on a button click.

Please advice me on this.

Regards,

Chandu.

Chandu
  • 871
  • 4
  • 11
  • 18

2 Answers2

6

It can be done by using mysql

mysql --user=USERNAME --password=PASSWORD DATABASE < DATABASE.sql

EDIT:

To place this in a script:

file loaddb.sh:

mysql --user=USERNAME --password=PASSWORD DATABASE < $1.sql

add execute-permission by

chmod +x loaddb.sh

you would call it:

loaddb.sh YOURDBNAME

stacker
  • 68,052
  • 28
  • 140
  • 210
  • Hi stacker, Thanks for ur reply. But my requirement is to create a shell script that do this job as specified in the description. Please advice me – Chandu Apr 14 '10 at 09:32
  • Chandu, add `#!/bin/bash` as the first script line, replace `DATABASE.sql` with `$1` and you're all set. – Tomislav Nakic-Alfirevic Apr 14 '10 at 09:34
  • Hi Tomislav, Thank for your reply. I'm not clear about $1 can you explain me on this? If I specified $1 then where I have to keep dump file path for restoring. – Chandu Apr 14 '10 at 09:37
  • $1 is a placeholder for a parameter passed to you script – stacker Apr 14 '10 at 10:45
  • Thanks Stacker, so where should I place the execute permission "chmod +x loaddb.sh" because I want to run this script from a java program & explain me the way about how to call this script from java. – Chandu Apr 14 '10 at 11:38
  • chmod is only done once, to start this from java you don't need that script see http://stackoverflow.com/questions/2071682/how-to-execute-sql-script-file-in-java and accept the answer ;-) – stacker Apr 14 '10 at 12:14
1

See this; it might be beneficial:

#!/bin/sh
echo "ENTER DATA BASE NAME:"
read dbname
echo "ENTER DATABASE USER NAME:"
read dbuser
echo "ENTER DATASE PASSWORD:"
read dbpassword
mysqldump -u $dbuser -p$dbpassword $dbname>$dbname".sql"