天天看点

mysql实现备份与恢复功能

1.备份功能
dataSourse("192.168.1.139","root","root","rocketBudget",filename,"D:/Users");

/**
      * 
      *数据备份
      * 
      * @return
      * @throws Exception
      */
     public String dataSourse(String ip,String name,String password,String database,String filename,String fileway) throws Exception {
         backupDatebase(ip,name,password, database, fileway+"/"+filename);  
         return "success";
     }
     
      /**  
      * 执行dos命令  
      * @param cmd  
      * @return  
      */  
     public String execCmd(String cmd) {  
         StringBuffer sb = new StringBuffer("");  
         StringBuffer str = new StringBuffer();  
         str.append("cmd /c  c:\\").append(cmd);  
         System.out.println(str);        //打印执行的命令  
         Process ls_proc;  
        // "cmd /c c:\\mysqldump -hlocalhost -uroot -p123456  DataPro>D:/test.sql"
         try {  
             ls_proc = Runtime.getRuntime().exec(str.toString());  
             BufferedReader in = new BufferedReader(  
                                     new InputStreamReader(  
                                         new DataInputStream(ls_proc.getInputStream())));  
             String ss = "";  
             while((ss = in.readLine()) != null) {  
                 sb.append(ss).append("\n");  
             }  
             in.close();  
         } catch (IOException e) {  
             e.printStackTrace();  
         }   
   
         return sb.toString();  
     }  
       
     /**  
      * 执行mysql数据库备份  
      * @param ip  
      * @param username  
      * @param password  
      * @param datebaseName  
      * @param filePath  
      * @return  
      */  
     public boolean backupDatebase(String ip, String username, String password,String datebaseName, String filePath) {  
         String strCommand = "mysqldump -h"+ip+" -u" + username + " -p" + password + " "+"--default-character-set=utf8"+ " "+ datebaseName + " > " + filePath;  
         String result = execCmd(strCommand);  
         System.out.println(result);  
         return true;  
     }  
       
     /**  
      * 根据返回结果验证是否成功  
      * @param result  
      * @return  
      */  
     public boolean check(String result) {  
         return true;  
     }  
2.恢复
String filePath=request.getSession().getServletContext().getRealPath("/")+"upload/"+file.getOriginalFilename();
             File uploadFile = new File(request.getSession().getServletContext().getRealPath("/")+"upload/");
             if (!uploadFile.exists()&& !uploadFile.isDirectory()) {
                 uploadFile.mkdir();
             }
             file.transferTo(new File(filePath));
             Runtime rt = Runtime.getRuntime();     
             Process child =  rt.exec( "C:/Program Files/MySQL/MySQL Server 5.6/bin/mysql.exe -uroot -p123456 --default-character-set=utf8 budget");  
             OutputStream out = child.getOutputStream();//控制台的输入信息作为输出流     
             String inStr;     
             StringBuffer sb = new StringBuffer("");     
             String outStr;     
             BufferedReader br = new BufferedReader(new InputStreamReader(     
                     new FileInputStream(filePath), "utf8"));     
             while ((inStr = br.readLine()) != null) {     
                 sb.append(inStr + "\r\n");     
             }     
             outStr = sb.toString();     
     
             OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");     
             writer.write(outStr);     
             // 注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免     
             writer.flush();     
             // 别忘记关闭输入输出流     
             out.close();     
             br.close();     
             writer.close();     
             System.out.println("");     
             addMessage(redirectAttributes, "数据还原成功");      

继续阅读