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?

1

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

I used Putty to make connection but i dont know how to send command from Java to run.
Use exec to run putty from your java code. Here 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
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

1 Comment

I modified the script and i successfully login to remote machine. But still cannot execute the script, neither linux command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.