DB Dump using JAVA

package dbsync;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.CodeSource;
import javax.swing.JOptionPane;

/**
*
* @author mr
*/
public class dbsync {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("javaapplication1.JavaApplication1.main()");
Backupdbtosql();
}
public static void Backupdbtosql() {
Process p = null;
String dbName = "dbname";
String dbUser = "root";
String dbPass = "password";
try {
CodeSource codeSource = dbsync.class.getProtectionDomain().getCodeSource();
File jarFile = new File(codeSource.getLocation().toURI().getPath());
String jarDir = jarFile.getParentFile().getPath();
String folderPath = jarDir + "/backup";
File f1 = new File(folderPath);
f1.mkdir();
String savePath = f1.getAbsolutePath() + "/backup.sql";

Runtime runtime = Runtime.getRuntime();
//for linux or where mysql is global access
//p = runtime.exec("mysqldump -u"+dbUser+" -p"+dbPass+" –add-drop-database -B "+dbName+" -r " +savePath);
//for windows
p = runtime.exec("C:\\xampp\\mysql\\bin\\mysqldump.exe -u"+dbUser+" -p"+dbPass+" –add-drop-database -B "+dbName+" -r " +savePath);
//change the dbpass and dbname with your dbpass and dbname
int processComplete = p.waitFor();

if (processComplete == 0) {

System.out.println("Backup created successfully!");

} else {
System.out.println("Could not create the backup");
}

} catch (Exception e) {
e.printStackTrace();
}
}

}

Advertisements