MOYUN(/Java/SQL/Linux/DevOps/运维/架构/管理/敏捷/开发)

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

数据库的备份与还原对项目而言是无疑是对安全考虑的好方法,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

春风如贵客,一到便繁华
posted on 2014-04-16 10:13  moyun-  阅读(14506)  评论(2编辑  收藏  举报