Friday, September 28, 2012

Take the MySql DB backup in JAVA

This is very impotent blog post for the most application developers
Here I'm talking about how to backup the mysql databasae through JAVA program.
Download MySqlBackup.java you have to download mysql.jar and log4j.jar

1. Following method will create the data base backup without create and drop database commands
It's only creating tables and included data
When your going to restore the database first you have to create new DB

[sourcecode language="java"]
public boolean backupDataWithOutDatabase(String dumpExePath, String host, String port, String user, String password, String database, String backupPath) {
boolean status = false;
try {
Process p = null;

DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = new Date();
String filepath = "backup(without_DB)-" + database + "-" + host + "-(" + dateFormat.format(date) + ").sql";

String batchCommand = "";
if (password != "") {
//only backup the data not included create database
batchCommand = dumpExePath + " -h " + host + " --port " + port + " -u " + user + " --password=" + password + " " + database + " -r \"" + backupPath + "" + filepath + "\"";
} else {
batchCommand = dumpExePath + " -h " + host + " --port " + port + " -u " + user + " " + database + " -r \"" + backupPath + "" + filepath + "\"";
}

Runtime runtime = Runtime.getRuntime();
p = runtime.exec(batchCommand);
int processComplete = p.waitFor();

if (processComplete == 0) {
status = true;
log.info("Backup created successfully for without DB " + database + " in " + host + ":" + port);
} else {
status = false;
log.info("Could not create the backup for without DB " + database + " in " + host + ":" + port);
}

} catch (IOException ioe) {
log.error(ioe, ioe.getCause());
} catch (Exception e) {
log.error(e, e.getCause());
}
return status;
}

[/sourcecode]

2. This method will create a backup from Specified database with create and drop database commands
When your going to restore the DB you don't need to create the DB it will handle by the script

[sourcecode language="java"]
public boolean backupDataWithDatabase(String dumpExePath, String host, String port, String user, String password, String database, String backupPath) {
boolean status = false;
try {
Process p = null;

DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = new Date();
String filepath = "backup(with_DB)-" + database + "-" + host + "-(" + dateFormat.format(date) + ").sql";

String batchCommand = "";
if (password != "") {
//Backup with database
batchCommand = dumpExePath + " -h " + host + " --port " + port + " -u " + user + " --password=" + password + " --add-drop-database -B " + database + " -r \"" + backupPath + "" + filepath + "\"";
} else {
batchCommand = dumpExePath + " -h " + host + " --port " + port + " -u " + user + " --add-drop-database -B " + database + " -r \"" + backupPath + "" + filepath + "\"";
}

Runtime runtime = Runtime.getRuntime();
p = runtime.exec(batchCommand);
int processComplete = p.waitFor();

if (processComplete == 0) {
status = true;
log.info("Backup created successfully for with DB " + database + " in " + host + ":" + port);
} else {
status = false;
log.info("Could not create the backup for with DB " + database + " in " + host + ":" + port);
}

} catch (IOException ioe) {
log.error(ioe, ioe.getCause());
} catch (Exception e) {
log.error(e, e.getCause());
}
return status;
}

[/sourcecode]

1 comment: