首页 > 代码库 > java实现mysql的备份还原
java实现mysql的备份还原
此文章是基于
1. 搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台
2. jquery+springMVC实现文件上传
3. 自制xml实现SQL动态参数配置
4. web工程常用路径的获取方法
5. 常用文件(夹)处理方法工具类
一. 简介
备份和导入是一个互逆的过程。
备份:程序调用mysql的备份命令,读出控制台输入流信息,写入.sql文件;
导入:程序调用mysql的导入命令,把从.sql文件中读出的信息写入控制台的输出流
注意:用Java执行,每次只能执行一条command命令,重定向符">"和"<"是不能用的
二. 准备工作
1. 点击此下载相关文件,并把文件放到 ims 工程对应的文件夹下
三. 相关程序代码介绍
1. BackupRestoreBSImpl.java
package com.ims.service.sys.impl; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.HashMap; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ims.service.sys.DataBaseBS; import com.ims.common.DateUtil; import com.ims.common.FileUtil; import com.ims.service.sys.BackupRestoreBS; @Service("backupRestoreBS") public class BackupRestoreBSImpl implements BackupRestoreBS{ private static Log logger = LogFactory.getLog(BackupRestoreBSImpl.class); private static final String uploadPath = System.getProperty("webapp.root")+"uploadFile\\backupRestore\\"; @Autowired public DataBaseBS dataBaseBS; /** * 备份单个数据库 * @param dbName 数据库名称 * @return 备份成功或者失败 */ @Override public boolean backup(String dbName){ try { logger.info(dbName + "开始备份!"); String cmd = "\"\" \""+dataBaseBS.getInstallPath() +"bin\\mysqldump\" -hlocalhost -uroot -p123456 " + dbName; Process process = Runtime.getRuntime().exec("cmd /c start /b " + cmd); // 把进程执行中的控制台输出信息写入.sql文件,即生成了备份文件。 // 注:如果不对控制台信息进行读出,则会导致进程堵塞无法运行 InputStream in = process.getInputStream();// 控制台的输出信息作为输入流 InputStreamReader inReader = new InputStreamReader(in, "utf8");// 设置输出流编码为utf8。这里必须是utf8,否则从流中读入的是乱码 String inStr; StringBuffer sb = new StringBuffer(""); String outStr; // 组合控制台输出信息字符串 BufferedReader br = new BufferedReader(inReader); while ((inStr = br.readLine()) != null) { sb.append(inStr + "\r\n"); } outStr = sb.toString(); // 要用来做导入用的sql目标文件: FileOutputStream fout = new FileOutputStream(uploadPath + dbName + ".sql"); OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8"); writer.write(outStr); // 注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免 writer.flush(); // 别忘记关闭输入输出流 in.close(); inReader.close(); br.close(); writer.close(); fout.close(); logger.info(dbName + "备份成功!"); return true; } catch (Exception e) { logger.error(dbName + "备份失败!",e); return false; } } /** * 备份所有的数据库 */ @Override public Map<String, Object> backupAll(){ Map<String, Object> result = new HashMap<String, Object>(); String[] dataBases = dataBaseBS.getDataBases(); if(FileUtil.deleteAll(uploadPath)){ File[] srcfile = new File[dataBases.length]; for(int i=0;i<dataBases.length;i++){ if(backup(dataBases[i])){ srcfile[i] = new File(uploadPath+dataBases[i]+".sql"); }else{ result.put("status", false); result.put("msg", dataBases[i] + "数据备份失败"); return result; } } String filename = DateUtil.getCurrDate() + "_backup.zip"; File zipfile = new File(uploadPath + filename); if(FileUtil.zip(srcfile, zipfile)){ result.put("status", true); result.put("msg", filename); }else{ result.put("status", false); result.put("msg", "文件压缩失败"); } }else{ result.put("status", false); result.put("msg", "文件夹清空失败"); } return result; } /** * 还原单个数据库 * @param dbName 数据库名称 * @return 还原成功或者失败 */ @Override public boolean restore(String dbName){ try { logger.info(dbName + "开始还原!"); // 调用 mysql 的 cmd: String cmd = "\"\" \""+dataBaseBS.getInstallPath() +"bin\\mysql\" -hlocalhost -uroot -p123456 " + dbName; Process process = Runtime.getRuntime().exec("cmd /c start /b " + cmd); OutputStream out = process.getOutputStream();//控制台的输入信息作为输出流 String inStr; StringBuffer sb = new StringBuffer(""); String outStr; BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(uploadPath + dbName + ".sql"), "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(); logger.info(dbName + "还原成功!"); return true; } catch (Exception e) { logger.error(dbName + "还原失败!",e); return false; } } /** * 还原所有的数据库 */ @Override public Map<String, Object> restoreAll(String zipFile){ Map<String, Object> result = new HashMap<String, Object>(); String[] dataBases = dataBaseBS.getDataBases(); if(zipFile.length()>0&&checkFile(zipFile)){ if(FileUtil.unZip(new File(uploadPath+zipFile), uploadPath)){ for(int i=0;i<dataBases.length;i++){ if(!restore(dataBases[i])){ result.put("status", false); result.put("msg", dataBases[i] + "数据还原失败"); return result; } } System.gc();// 强制回收内存垃圾,否则zip文件一直被占用删除不了 result.put("status", true); result.put("msg", "数据还原成功"); }else{ result.put("status", false); result.put("msg", "解压缩包失败"); } }else{ result.put("status", false); result.put("msg", "没有找到可还原的数据压缩文件"); } return result; } /** * 根据文件名验证文件是否合法 * @param fileName * @return */ public boolean checkFile(String fileName){ String[] strs = fileName.split("_"); if(strs.length>1){ String checkStr = strs[strs.length-1]; if("backup.zip".equals(checkStr)){ return true; }else{ return false; } }else{ return false; } } @Override public String getUploadpath() { return uploadPath; } }
2. DataBaseBSImpl.java,可设置要备份的数据库名称,如:iot、mms、sys等
package com.ims.service.sys.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ims.persistence.base.SqlXmlParser; import com.ims.persistence.dao.sys.DataBaseDao; import com.ims.service.sys.DataBaseBS; @Service("dataBaseBS") public class DataBaseBSImpl implements DataBaseBS{ private static final String sqlXml = "sys/dataBase.xml"; private static final String[] dataBases = new String[]{"iot"}; @Autowired private DataBaseDao dataBaseDao; @Override public String getInstallPath() { String installPath = (String)dataBaseDao.findUniqueResultBySql(new SqlXmlParser(sqlXml).parse("installPath", null)); return installPath; } @Override public String[] getDataBases() { return dataBases; } }
3. TestController.java
package com.ims.web.controller; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.servlet.ModelAndView; import com.alibaba.fastjson.JSON; import com.ims.common.FileUtil; import com.ims.service.sys.BackupRestoreBS; @Controller @RequestMapping("test") public class TestController extends BaseController{ @Autowired private BackupRestoreBS backupRestoreBS; @RequestMapping("view") public ModelAndView test(){ ModelAndView view = new ModelAndView("test.jsp"); return view; } @RequestMapping("backupRestore!backup") public void backup(){ Map<String, Object> result = backupRestoreBS.backupAll(); ajaxJson((Boolean)result.get("status")?STATUS_SUCCESS:STATUS_ERROR, result.get("msg")); } @RequestMapping("backupRestore!download") public void download(@RequestParam Map<String, Object> params){ InputStream in=null; OutputStream out=null; String fileName = (String)params.get("fileName"); try{ response.setContentType("application/x-download;charset=GBK"); response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("GBK"),"ISO8859_1")); out=response.getOutputStream(); //将内容写入输出流并把缓存的内容全部发出去 in=new BufferedInputStream(new FileInputStream(backupRestoreBS.getUploadpath()+fileName)); int len = 0; byte[] buffer = new byte[1024]; while((len = in.read(buffer)) > 0) { out.write(buffer,0,len); } out.flush(); }catch(Exception e){ ajaxJson(STATUS_ERROR, "文件下载失败"); }finally{ if(in!=null){ try{ in.close(); }catch(IOException e){ ajaxJson(STATUS_ERROR, "输入流关闭失败"); } } if(out!=null){ try{ out.close(); }catch(IOException e){ ajaxJson(STATUS_ERROR, "输出流关闭失败"); } } } } @RequestMapping("backupRestore!restore") public void restore(@RequestParam Map<String, String> params, MultipartHttpServletRequest multipartRequest){ Map<String, Object> result = new HashMap<String, Object>(); try { if(FileUtil.deleteAll(backupRestoreBS.getUploadpath())){ MultipartFile restoreFile = multipartRequest.getFile("restoreFile"); String fileName = restoreFile.getOriginalFilename(); if(FileUtil.saveFileFromInputStream(restoreFile.getInputStream(), backupRestoreBS.getUploadpath()+fileName)){ Map<String, Object> restoreResult = backupRestoreBS.restoreAll(fileName); result.put("status", STATUS_SUCCESS); result.put("message", restoreResult.get("msg")); }else{ result.put("status", STATUS_ERROR); result.put("message", "文件保存失败"); } }else{ result.put("status", STATUS_ERROR); result.put("message", "文件夹清空失败"); } } catch (IOException e) { result.put("status", STATUS_ERROR); result.put("message", "数据还原失败"); } ajax(JSON.toJSONString(result),"text/html"); } }
4. test.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>测试</title> <%@ include file="/common/basePath.jsp"%> </head> <body> ~~~~~~~~~~~~~~~~~~~~~~mysql数据库的备份与还原~~~~~~~~~~~~~~~~~~~~~~~~ <br><br> 数据备份:<button type="button" onclick="backup();">备份</button> <br><br> 数据选择:<input type="file" id="restoreFile" style="width: 350px;"/> <br><br> 导入还原:<button type="button" onclick="restore();">还原</button> <br><br><br> <script type="text/javascript" src="content/js/jquery/jquery-1.8.1.min.js"></script> <script type="text/javascript" src="content/js/jquery-plugin/fileUpload/jquery.ajaxFileUpload.js"></script> <script type="text/javascript"> function backup(){ $.ajax({ async:true, url: rootPath+"/test/backupRestore!backup.do", success: function (d) { var exporter = com.exporter(); exporter.params.action = rootPath+"/test/backupRestore!download.do?fileName="+d.message; exporter.download(‘zip‘); } }); } function restore() { $.ajaxFileUpload({ url:rootPath+"/test/backupRestore!restore.do", secureuri:false, fileElementId: [‘restoreFile‘], dataType: ‘json‘, success: function (data){ }, error: function(data){ } }); } </script> </body> </html>
四. 测试
访问:http://localhost:8090/ims/test/view.do
1. 备份:点击 备份 按钮,浏览器会下载压缩后的备份文件,格式如:2017-03-16_backup.zip
2. 还原:选择刚下载的压缩后的备份文件,点击 还原 按钮,数据库被成功还原
java实现mysql的备份还原
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。