Friday, 27 September 2013

BACKUP MYSQL DATABASE WITHOUT USING MYSQL COMMAND PROMPT



<%@page import="java.sql.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         <title>BACKUP DATABASE</title>
        <script type="text/javascript" src="tabcontent.js"></script>
    </head>
    <body>
       
    <center>
        <div style="border-radius: 25px; width:800px; height:650px;background:#f0D0D0 ">
<h3>BackUp Database</h3>
<%
String database = null;
String username = "root";
String password = "root";
String urlmysql = "jdbc:mysql://localhost:9999";  //CHANGE ACCORDINGLY


String dbtype = null;
Connection connection = null;
Statement statement = null;
ResultSet rset = null;
int result = -1;

try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();

} catch(ClassNotFoundException e) {
    out.println("Class not found: "+ e.getMessage());
    return;
}

if (request.getParameter("dbtype") != null) { dbtype = request.getParameter("dbtype"); };

try {

    connection = DriverManager.getConnection(urlmysql, username, password);
    statement = connection.createStatement();


   //To Display List of  Databases Existed in the Mysql Database

    out.println("<b>List of MySQL databases accessible by user " + username + ":</b><br/>");
    rset = statement.executeQuery("SHOW DATABASES");
    while (rset.next())   {
        out.println(rset.getString(1) + "<br/>");
    }
    rset.close();
    statement.close();
    connection.close();
    out.println("<hr>");


    if (request.getParameter("database") != null) {
        database = (String)request.getParameter("database");
        if (request.getParameter("Backup") != null &&
            request.getParameter("Backup").equals("Backup")) {

            String executeCmd = "mysqldump -u " + username + " -p" + password + " --add-drop-database -B " + database + " -r " + database + ".sql";
         

            Process runtimeProcess;
            try {
                 runtimeProcess = Runtime.getRuntime().exec(executeCmd);
                int processComplete = runtimeProcess.waitFor();
                 if (processComplete == 0) {
                    out.println("Backup created successfully");
                     } else {
                    out.println("Could not create the backup");
                 }
              } catch (Exception ex) {
                ex.printStackTrace();
            }
         }
    }
 

%>
<form action="backup.jsp" method="post"><table>
<tr><td align="left">Database name to backup : <input type="text" name="database" size="20"></td></tr>
<tr><td><input type="radio" name="dbtype" value="mysql" checked="checked">MySQL<br>
</tr>
<tr><td align="left"><input type="submit" name="Backup" value="Backup">
<input type="reset" name="Reset" value="Reset"></td></tr>
</table>
</form>
<%
} catch (SQLException e) {
    out.println(e.getMessage());
} finally {
    try {
        if(connection != null) connection.close();
    } catch(SQLException e) {}
}
%>

        </div>
    </center>
     
    </body>
</html>



No comments:

Post a Comment