It is usual that we need to backup the database in software development (specially in Enterprise Software,web applications ) Database is very important which contains all the information about the organization. Last night I have coded a auto backup script in both JAVA and PHP. Here I am gonna post them below with comment.<\/p>\n
Code Written in JAVA<\/strong><\/p>\n Code Written in PHP<\/strong><\/p>\n It is usual that we need to backup the database in software development (specially in Enterprise Software,web applications ) Database is very important which contains all the information about the organization. Last night I have coded a auto backup script in both JAVA and PHP. Here I am gonna post them below with comment. Code … <\/p>\n\r\n\/*\r\n * To change this template, choose Tools | Templates\r\n * and open the template in the editor.\r\n *\/\r\nimport java.io.BufferedWriter;\r\nimport java.io.FileWriter;\r\nimport java.io.IOException;\r\nimport java.io.*;\r\nimport java.sql.*;\r\nimport java.util.logging.Level;\r\nimport java.util.logging.Logger;\r\n\/**\r\n *\r\n * @author Ujjal Suttra Dhar\r\n *\/\r\n\r\nclass create_backup{\r\n\r\n String database = \"your_database_name\", user = \"root\", password = \"pass\";\r\n Connection m_Connection = null;\r\n Statement m_Statement = null;\r\n ResultSet m_ResultSet;\r\n String m_Driver = \"com.mysql.jdbc.Driver\";\r\n String m_Url = \"jdbc:mysql:\/\/localhost:3306\/\" + database;\r\n\r\n public create_backup(){\r\n\r\n \/*Eshtablishment of Connection of java code with MySQL using JDBC\r\n You must read my previous post to know further about this. \r\n *\/\r\n try {\r\n Class.forName(m_Driver);\r\n m_Connection = DriverManager.getConnection(m_Url, user, password);\r\n \/\/Create Statement object\r\n m_Statement = m_Connection.createStatement();\r\n\r\n } catch (Exception ex) {\r\n ex.printStackTrace();\r\n }\r\n\r\n try {\r\n \/\/Database will be stored in filename.sql which can be import on MySQl as backup\r\n BufferedWriter out = new BufferedWriter(new FileWriter("kernel_school.sql"));\r\n\r\n\r\n \/\/Now here is the informations about the database of which backup will be created\r\n String dump = "C:\/Program Files\/MySQL\/MySQL Server 5.0\/bin\/mysqldump " \/\/Path to mysql\r\n + \"--host=localhost\" \/\/Mysql hostname\r\n + \"--port=3306\" \/\/Mysql portnumber\r\n + \"--user=root\" \/\/Mysql username\r\n + \"--password=pass\" \/\/Mysql password\r\n + \"--add-drop-table\" \/\/Add a DROP TABLE statement before each CREATE TABLE statement\r\n + \"--add-drop-database\" \/\/Add a DROP DATABASE statement before each CREATE DATABASE statement\r\n + \"--complete-insert\" \/\/Use complete INSERT statements that include column names.\r\n + \"--extended-insert\" \/\/Use multiple-row INSERT syntax that include several VALUES lists\r\n + \"kernel_school\"; \/\/Mysql databasename\r\n\r\n\r\n \/\/executing the command through process.\r\n Process run = Runtime.getRuntime().exec(dump);\r\n\r\n\r\n \/*resultant SQL informations are here on the br which can be read line by line .\r\n And at the same time it will be written on another sql file.*\/\r\n InputStream in = run.getInputStream();\r\n InputStreamReader isr = new InputStreamReader(in);\r\n BufferedReader br = new BufferedReader(isr);\r\n String line = null;\r\n\r\n while ((line = br.readLine())!=null) {\r\n out.write(line);\r\n out.newLine();\r\n }\r\n\r\n out.flush(); \/\/ must flush or close the file after completing the task.\r\n \/\/ int exitVal = run.waitFor();\/\/when exitVal is ,then the process is completed\r\n\r\n } catch (Throwable t) {\r\n t.printStackTrace();\r\n }\r\n\r\n }\/\/constructor\r\n } \/\/ class\r\n\r\n\r\n\r\npublic class autobackup {\r\n public static void main(String a[]){\r\n new create_backup();\r\n }\r\n}\r\n<\/pre>\n
\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"