4

I am developing a Java application with MySql as the database. I have to dump the MySql database from my application periodically(let say every day at 10 a.m.) and I have written a batch (.bat) file for dumping the database. The batch file is working fine, but the problem is that it is asking for password each time during its execution.

Is there any way to dump MySql database without prompting for password and achieve it from Java application periodically?

gmhk
  • 14,439
  • 26
  • 82
  • 107
dhiraj
  • 377
  • 1
  • 5
  • 17

4 Answers4

1

You can definitely put the password on the command line, as others have pointed out. As far as getting it to run from Java (though the purists may object), you can use Runtime.exec() to just call your mysql command: http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html.

If you're doing it from within your java app, you might also want to consider not just doing it periodically, though. Depending on your database usage (for instance frequent reads, but infrequent writes), you may find that you can dump only when you know that the database has been changed, or something along those lines.

If you really want to just dump the database periodically though, you might be better off just setting up a cron-job. Here's a previous post on SO regarding the Windows approach to cron: What is the Windows version of cron?.

Community
  • 1
  • 1
Curtis
  • 3,720
  • 1
  • 16
  • 25
  • Thanx a lot for your reply. The mysqldump part is working fine. I have to look for the periodic dump part now. Yes I have used the Runtime.exec() only for that purpose. – dhiraj Jun 11 '10 at 12:38
0

Backup:

/******************************************************/
//Database Properties
/******************************************************/
String dbName = “dbName”;
String dbUser = “dbUser”;
String dbPass = “dbPass”;

/***********************************************************/
// Execute Shell Command
/***********************************************************/
String executeCmd = “”;
executeCmd = “mysqldump -u “+dbUser+” -p”+dbPass+” “+dbName+” -r backup.sql”;
}
Process runtimeProcess =Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if(processComplete == 0){

out.println(“Backup taken successfully”);

} else {

out.println(“Could not take mysql backup”);

}

Restore:

/******************************************************/
//Database Properties
/******************************************************/
String dbName = “dbName”;
String dbUser = “dbUser”;
String dbPass = “dbPass”;

/***********************************************************/
// Execute Shell Command
/***********************************************************/
String executeCmd = “”;

executeCmd = new String[]{“/bin/sh”, “-c”, “mysql -u” + dbUser+ ” -p”+dbPass+” ” + dbName+ ” < backup.sql” };

}
Process runtimeProcess =Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if(processComplete == 0){

out.println(“success”);

} else {

out.println(“restore failure”);

}
Mehdi
  • 4,418
  • 4
  • 27
  • 29
0

You'll want the following option for mysqldump

--password[=password], -p[password]

mysqldump documentation

Mad Scientist
  • 16,689
  • 11
  • 75
  • 97
0

You don't tell how you are actually dumping the database, but I'll assume that you are using mysqldump.

You can specify the database password on the command line using the switch --password=.... Or if you want to be more secure, use a password in an option file. See man mysqldump for more information.

Adam Batkin
  • 47,187
  • 7
  • 120
  • 110