I want to invoke mysqldump from a Spring based webapp and write the output to a file. I want to be able to provide a download link to the user to the file created on the server.
I tried running the following:
Runtime.getRuntime().exec("/usr/bin/mysqldump -u <username> -p<password> <db_name> >" + fileName);
Runtime.getRuntime().exec("mysqldump -u <username> -p<password> db_name >" + fileName);
I tried the following values of fileName.
fileName = "mysql.sql";
fileName = "/tmp/mysql.sql";
fileName = servletContext.getRealPath("") + "/mysql.sql";
But the command is not successful. process.exitValue() returns an error code of 2 or 6.
ProcessBuilder pb = new ProcessBuilder("/usr/bin/mysqldump", " -u ", "<username>", " -p<password>", "<db_name>", " > ", fileName);
pb.redirectErrorStream(true); // equivalent of 2>&1
Process p = pb.start();
p.waitFor();
This does not help as well.
Can someone tell me what is the best way to run such a command from the webapp? Where should the output file be created?
What is wrong with the commands above?
I need to be able to run this on different systems (Linux, Windows). The webapp runs on either system. I can abstract out the mysqldump path in a properties file if the need arises. But how do I make this work?
This is probably not a very good idea. The servlet probably needs a very permissive security environment for this to work at all. If you do a search you will find lots of people having problems with this.
Even if it works the runtime exec might block for some reason and the servlet would timeout, I don’t know the ramifications of that, but they don’t sound good to me.
We had a similar problem where I worked a couple of years ago and wrote a simple multithreaded server that would queue up requests, and run them when it got a chance and handled things like hangs, crashes and restarts. It ran as a jsvc daemon. The servlet sent it a message over a socket.