{"id":170,"date":"2012-05-02T21:35:55","date_gmt":"2012-05-02T15:35:55","guid":{"rendered":"http:\/\/ujjalruet.wordpress.com\/?p=170"},"modified":"2015-02-14T10:38:34","modified_gmt":"2015-02-14T10:38:34","slug":"creating-autoback-up-of-database-using-java","status":"publish","type":"post","link":"https:\/\/blog.ujjal.net\/?p=170","title":{"rendered":"Creating AutoBack Up of MySQL Database using JAVA OR PHP ( \u09aa\u09bf\u098f\u099a\u09aa\u09bf \u0985\u09a5\u09ac\u09be \u099c\u09be\u09ad\u09be \u09a4\u09c7 \u0995\u09bf\u09ad\u09be\u09ac\u09c7 \u09ae\u09be\u0987\u098f\u09b8\u0995\u09bf\u0989\u098f\u09b2 \u09a1\u09be\u099f\u09be\u09ac\u09c7\u099c \u098f\u09b0 \u0985\u099f\u09c7\u09be\u09ae\u09c7\u099f\u09bf\u0995 \u09ac\u09cd\u09af\u09be\u0995\u0986\u09aa \u0995\u09b0\u09a4\u09c7 \u09b9\u09df )"},"content":{"rendered":"<p>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<p><strong>Code Written in JAVA<\/strong><\/p>\n<pre class=\"lang:java decode:true\" >\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(&amp;quot;kernel_school.sql&amp;quot;));\r\n\r\n\r\n            \/\/Now here is the informations about the database of which backup will be created\r\n            String dump = &amp;quot;C:\/Program Files\/MySQL\/MySQL Server 5.0\/bin\/mysqldump &amp;quot; \/\/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<p><strong>Code Written in PHP<\/strong><\/p>\n<pre class=\"lang:php decode:true \" >\r\n<?php\r\n\r\nbackup_tables($host,$user,$password,$database_name,'*');\r\n\r\n\/* backup the db OR just a table *\/\r\nfunction backup_tables($host,$user,$pass,$name,$tables = '*')\r\n{\r\n\r\n\t$link = mysql_connect($host,$user,$pass);\r\n\tmysql_select_db($name,$link);\r\n\r\n\t\/\/get all of the tables\r\n\tif($tables == '*')\r\n\t{\r\n\t\t$tables = array();\r\n\t\t$result = mysql_query('SHOW TABLES');\r\n\t\twhile($row = mysql_fetch_row($result))\r\n\t\t{\r\n\t\t\t$tables[] = $row[0];\r\n\t\t}\r\n\t}\r\n\telse\r\n\t{\r\n\t\t$tables = is_array($tables) ? $tables : explode(',',$tables);\r\n\t}\r\n\r\n\t\/\/cycle through\r\n\tforeach($tables as $table)\r\n\t{\r\n\t\t$result = mysql_query('SELECT * FROM '.$table);\r\n\t\t$num_fields = mysql_num_fields($result);\r\n\r\n\t\t$return.= 'DROP TABLE '.$table.';';\r\n\t\t$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));\r\n\t\t$return.= \"nn\".$row2[1].\";nn\";\r\n\r\n\t\tfor ($i = 0; $i < $num_fields; $i++)\r\n\t\t{\r\n\t\t\twhile($row = mysql_fetch_row($result))\r\n\t\t\t{\r\n\t\t\t\t$return.= 'INSERT INTO '.$table.' VALUES(';\r\n\t\t\t\tfor($j=0; $j<$num_fields; $j++)\r\n\t\t\t\t{\r\n\t\t\t\t\t$row[$j] = addslashes($row[$j]);\r\n\t\t\t\t\t$row[$j] = ereg_replace(\"n\",\"\\n\",$row[$j]);\r\n\t\t\t\t\tif (isset($row[$j])) { $return.= '\"'.$row[$j].'\"' ; } else { $return.= '\"\"'; }\r\n\t\t\t\t\tif ($j<($num_fields-1)) { $return.= ','; }\r\n\t\t\t\t}\r\n\t\t\t\t$return.= \");n\";\r\n\t\t\t}\r\n\t\t}\r\n\t\t$return.=\"nnn\";\r\n\t}\r\n\r\n\t\/\/save file\r\n\t$handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');\r\n\tfwrite($handle,$return);\r\n\tfclose($handle);\r\n}\r\n?><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.ujjal.net\/?p=170\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Creating AutoBack Up of MySQL Database using JAVA OR PHP ( \u09aa\u09bf\u098f\u099a\u09aa\u09bf \u0985\u09a5\u09ac\u09be \u099c\u09be\u09ad\u09be \u09a4\u09c7 \u0995\u09bf\u09ad\u09be\u09ac\u09c7 \u09ae\u09be\u0987\u098f\u09b8\u0995\u09bf\u0989\u098f\u09b2 \u09a1\u09be\u099f\u09be\u09ac\u09c7\u099c \u098f\u09b0 \u0985\u099f\u09c7\u09be\u09ae\u09c7\u099f\u09bf\u0995 \u09ac\u09cd\u09af\u09be\u0995\u0986\u09aa \u0995\u09b0\u09a4\u09c7 \u09b9\u09df )&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,18,9,10],"tags":[25,26,73,81,82,97,101,124,32,33,34,37],"class_list":["post-170","post","type-post","status-publish","format-standard","hentry","category-database","category-php","category-programming-with-java","category-tutorial","tag-auto-backup","tag-autobackup-of-database-in-php","tag-httpujjalruet-wordpress-com20120502creating-autoback-up-of-database-using-java","tag-java-2","tag-java-sql","tag-mysql","tag-php-2","tag-sql","tag-32","tag-33","tag-34","tag-37"],"_links":{"self":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts\/170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=170"}],"version-history":[{"count":3,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts\/170\/revisions"}],"predecessor-version":[{"id":347,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts\/170\/revisions\/347"}],"wp:attachment":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}