Java對MySql數據庫進行備份與還原


數據庫的備份與還原對項目而言是無疑是對安全考慮的好方法,MySQL 數據庫備份原理: Navicat等數據庫界面軟件通用的數據庫備份原理就是直接調用MYSQL本身的系統命令。
那么Java是如何來實現備份與還原mysql的呢:這里JavaApi里面給出了Runtime.getRuntime().exe("")方法,它可以執行系統目錄下bin下的cmd命令比如windows下的
mysql.exe或者linux下的mysql/bin/mysql命令
MySql數據庫備份

 1     public static String comman="C:/Program Files/MySQL/MySQL Server 5.5/bin/mysql.exe -uroot -proot test";
 2     public static void back(String mySqlBackupName,String mysqlBackupPath, String command){
 3         
 4         String fPath=mysqlBackupPath+"/"+new Date().getTime()+".sql";
 5         
 6         Runtime rt = Runtime.getRuntime();
 7         try {
 8             Process child = rt.exec(command);
 9             InputStream in = child.getInputStream();
10             InputStreamReader input = new InputStreamReader(in,"utf8");
11             
12             String inStr;
13             StringBuffer sb = new StringBuffer("");
14             String outStr;
15             
16             BufferedReader br = new BufferedReader(input);
17             while ((inStr = br.readLine()) != null) {     
18                 sb.append(inStr + "\r\n");     
19             }     
20             outStr = sb.toString(); 
21             
22             FileOutputStream fout = new FileOutputStream(fPath);
23             OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8");    
24             writer.write(outStr);
25             writer.flush();   
26             
27             in.close();     
28             input.close();     
29             br.close();     
30             writer.close();     
31             fout.close();     
32             
33             logger.info("MYSQL備份成功");
34         } catch (IOException e) {
35             e.printStackTrace();
36         }
37     }

MySql數據庫備份(這里用Servlet實現)

 1     private static final Logger logger = Logger.getLogger(CBSDAddServlet.class);
 2     public static String fPath="D:/apache-tomcat-6.0.30/webapps/test/databaseSql/2014-04-15.sql";
 3     public static String comman=" C:/Program Files/MySQL/MySQL Server 5.5/bin/mysql -uroot -proot --default-character-set=utf8 test ";
 4        
 5     private static final long serialVersionUID = -3323607254010948405L;
 6 
 7     public void doGet(HttpServletRequest request, HttpServletResponse response)
 8             throws ServletException, IOException {
 9 
10         doPost(request, response);
11     }
12 
13     public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
14 
15         boolean flag=false;
16         try {
17             /***
18              * 關閉當前項目與數據庫的連接
19              * 這里很重要,如果是hibernate等連接池的session與數據庫保持連接的話,因為還原會直接覆蓋當前的數據庫
20              * 而覆蓋之前當前的session與數據庫保持了一份連接,所以點擊備份時會直至卡在那里...這里得 關閉closeSession();操作
21              * ***/
22             BaseDao.closeSession(new BaseDao().getSession());
23             hy(request, response);
24             flag = true;
25             logger.info("還原成功");
26         } catch (Exception e) {
27             e.printStackTrace();
28             logger.info("開始失敗...");
29         }
30         
31         response.setCharacterEncoding("UTF-8");
32         response.getWriter().print(flag);
33     }
34 
35 
36     public void hy(HttpServletRequest request, HttpServletResponse response) throws IOException{
37         Runtime rt = Runtime.getRuntime(); 
38         Process child = rt.exec(comman);
39 
40         InputStreamReader in =new InputStreamReader(new FileInputStream(fPath),"UTF-8");
41         BufferedReader br =new BufferedReader(in);
42       
43         String inStr =null;
44         StringBuffer sb = new StringBuffer("");
45         String outStr;
46         while((inStr=br.readLine())!=null){
47              sb.append(inStr + "\r\n"); 
48         }
49         outStr = sb.toString();
50         
51         logger.info(outStr);
52 
53         OutputStream out = child.getOutputStream();
54         OutputStreamWriter writer =new OutputStreamWriter(out,"UTF-8");
55         writer.write(outStr);
56         writer.flush();
57         
58         out.flush();
59         out.close();     
60         br.close();     
61         writer.close();  
62         logger.info("數據回復成功!");
63     }

還原成功...
參考博客:
http://blog.csdn.net/hguisu/article/details/7325124
http://www.blogjava.net/Nirvana/archive/2012/09/07/387226.html
http://blog.csdn.net/djl100/article/details/9000409


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM