3

I have a bash script that restores my database. The database is on a remote Linux server, and my Java code is on Windows. How can I run the script?

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
user506246
  • 301
  • 1
  • 4
  • 15
  • See this: http://stackoverflow.com/questions/2514439/how-to-run-ssh-commands-on-remote-system-through-java-program – mindas Nov 24 '12 at 13:32

2 Answers2

1

What do you mean by restore? If you want just load dump of your DB maybe create backup DB and then just copy rows to target database. If you really need to run this scrip easiest way will be to connect to remote server via ssh and launch that script. Use Putty or some ssh java lib to make a connection and send command to run. More info about putty here

Community
  • 1
  • 1
janisz
  • 6,292
  • 4
  • 37
  • 70
  • I used Putty to make connection but i dont know how to send command from Java to run. – user506246 Nov 26 '12 at 10:27
  • Use `exec` to run putty from your java code. [Here](http://stackoverflow.com/a/11332291/1387612) are more info about it. IMO it's bad solution due to platform dependency. So better idea will be use some plain SSH Java lib maybe [JSch](http://www.jcraft.com/jsch/examples/Exec.java.html) – janisz Nov 26 '12 at 13:17
1

Try something like this:-

 Process p = Runtime.exec("ssh myhost");
 PrintStream out = new PrintStream(p.getOutputStream());
 BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream());

 out.println("ls -l /home/me");
 while (in.ready()) {
 String s = in.readLine();
 System.out.println(s);
 }
 out.println("exit");

p.waitFor();

From the source thread

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • I modified the script and i successfully login to remote machine. But still cannot execute the script, neither linux command. – user506246 Nov 26 '12 at 09:59