Mysql Database Backup and Restore in JSF and Primefaces
The first thing you need is adding some libraries to your application.
->mysql-connector-java-x.x-bin.jar
->commons-fileupload-x.x.jar
->commons-io-x.x.jar
->el-api-x.x.jar->el-impl-x.x.jar
->itext->x.x.jar
->apache poi ->x.x.jar
->rome ->x.x.jar
->commons-fileupload-x.x.jar
->commons-io-x.x.jar
->el-api-x.x.jar->el-impl-x.x.jar
->itext->x.x.jar
->apache poi ->x.x.jar
->rome ->x.x.jar
As far as web.xml configuration is concerned you need to declare the PrimeFaces filter in order to enable file uploading
<
filter
>
2.
<
filter-name
>PrimeFaces FileUpload Filter</
filter-name
>
3.
<
filter-class
>org.primefaces.webapp.filter.FileUploadFilter</
filter-class
>
4.
</
filter
>
5.
<
filter-mapping
>
6.
<
filter-name
>PrimeFaces FileUpload Filter</
filter-name
>
7.
<
servlet-name
>Faces Servlet</
servlet-name
>
8.
</
filter-mapping
>
Let's see a example:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form id="form" >
<h:commandButton action="#{backupDB.backup()}" value="BACKUP"/><br/>
<p:fileUpload fileUploadListener="#{restoeDB.upload}" label="Restore" auto="true" style="width: 11%" />
</h:form>
</h:body>
</html>
Backup Controller:
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
/**
*
* @author Qubit
*/
@ManagedBean(name = "backupDB")
public class Backupdb {
ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String basePath = ctx.getRealPath("/");
// private String txtPath = "G:\\New Folder\\";
private String txtPath = basePath + "BackupRestoreMysqldb//";
private String lblMessage;
public void backup() {
if (txtPath.equals("")) {
lblMessage = ("Please choose path to save!");
} else {
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy_HH-mm-ss");
//File file = new File(dateFormat.format(now));
String strFilename = dateFormat.format(now);
long nowLong = now.getTime();
//String strFilename;
//strFilename = nowLong.toString();
//strFilename = String.valueOf(nowLong);
System.out.println(strFilename);
// String command = "C:/Program Files/MySQL/MySQL Server 5.0/bin/mysqldump -u(db user name) -p(db password) --add-drop-database -B (db name) -r " + "\"" + txtPath.getText().toString() + "\\" + strFilename + ".sql\"";
String command = "G:/MYSQL(Installed)/Soft/bin/mysqldump -uroot -proot --add-drop-database -B test_db -r " + "\"" + txtPath.toString() + "\\" + strFilename + ".sql\"";
System.out.println(command);
Process p = null;
try {
Runtime runtime = Runtime.getRuntime();
p = runtime.exec(command);
int processComplete = p.waitFor();
if (processComplete == 0) {
// System.out.println("<html><font color='green'>Backup created successfully!</font></html>");
lblMessage = "Backup created successfully to " + txtPath.toString() + "\\" + strFilename + ".sql";
} else {
lblMessage = "Could not create the backup";
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Restore Controller:
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import org.primefaces.event.FileUploadEvent;
/**
*
* @author Qubit
*/
@ManagedBean(name = "restoeDB")
@SessionScoped
public class RestoreMysqlDatabase implements Serializable {
private String lblMessage;
ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String basePath = ctx.getRealPath("/");
// private String txtPath = "G:\\New Folder\\";
private String txtPath = basePath + "BackupRestoreMysqldb//";
public void upload(FileUploadEvent event) {
if (event.getFile().getFileName().equals("")) {
lblMessage = "Please Select file to restore!";
System.out.println(lblMessage);
} else {
//restoreDB("root", "root", "G:\\New Folder\\" + event.getFile().getFileName());
restoreDB("root", "root", txtPath + event.getFile().getFileName());
System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAA" + event.getFile().getFileName());
}
}
public boolean restoreDB(String dbUserName, String dbPassword, String source) {
// String[] executeCmd = new String[]{"C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysql ", "--user=" + dbUserName, "--password=" + dbPassword, "-e", "source " + source};
String[] executeCmd = new String[]{"G:/MYSQL(Installed)/Soft/bin/mysql ", "--user=" + dbUserName, "--password=" + dbPassword, "-e", "source " + source};
Process runtimeProcess;
try {
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
lblMessage = "Backup restored successfully!";
System.out.println(lblMessage);
return true;
} else {
lblMessage = "Could not restore the backup!";
System.out.println(lblMessage);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
}
Application:
Thanks, helped a lot!
ReplyDelete