MySQL代码备份
Posted moonsoft
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL代码备份相关的知识,希望对你有一定的参考价值。
package com.dus.utils; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; //数据库备份 public class mysqlDump { public static boolean sqlDump(String cmd,String filePath){ boolean falg = false; try { Runtime run = Runtime.getRuntime(); //cmd 命令:"C:/Program Files/MySQL/MySQL Server 5.1/bin/mysqldump -uroot -proot email" Process p = run.exec(cmd); InputStream is = p.getInputStream();// 控制台的输出信息作为输入流 InputStreamReader isr = new InputStreamReader(is,"UTF-8");//设置输入流编码格式 BufferedReader br = new BufferedReader(isr); //将控制台输入信息写入到文件输出流中 FileOutputStream fos = new FileOutputStream(filePath); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8")); String temp = null; while( (temp = br.readLine()) !=null){ bw.write(temp); bw.newLine(); } bw.flush(); bw.close(); br.close(); falg = true; System.out.println("/* Dump SQL File "+filePath+" OK! */"); } catch (IOException e) { throw new RuntimeException("请将mysql命令添加到path中!",e); } return falg; } //恢复数据库 /** * 导入 * */ public static void sqlLoad(String cmd,String sqlPath) { try { Runtime rt = Runtime.getRuntime(); // 调用 mysql 的 cmd: C:/Program Files/MySQL/MySQL Server 5.1/bin/mysql.exe -uroot -proot email Process child = rt.exec(cmd); OutputStream out = child.getOutputStream();//控制台的输入信息作为输出流 //输入流 BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(sqlPath), "utf8")); //输出流 OutputStreamWriter writer = new OutputStreamWriter(out, "utf8"); String inStr; while ((inStr = br.readLine()) != null) { writer.write(inStr); writer.write(" "); } writer.flush(); // 别忘记关闭输入输出流 out.close(); br.close(); writer.close(); System.out.println("/* Load SQL File "+sqlPath+" OK! */"); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { sqlDump("D:/Program Files/MySQL/MySQL Server 5.0/bin/mysqldump -uroot -proot weddingdb","e:/DBBack/weddingdb.sql"); //sqlLoad("C:/Program Files/MySQL/MySQL Server 5.1/bin/mysql.exe -uroot -proot email","c:/email.sql"); } }
servlet
package com.dus.web; import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.dus.model.Log; import com.dus.service.LogService; import com.dus.utils.AppException; import com.dus.utils.MySQLDump; /** * 备份数据库 servlet */ public class MySQLDumpServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter pw = response.getWriter(); boolean flag = false; // 声明session HttpSession session = null; // 使用request对象取得session session = request.getSession(); Integer userId = (Integer)session.getAttribute("userId"); // 若用户没有登录,则跳转到登录页面 if (userId == null) { response.sendRedirect("toLogin"); } else { flag = MySQLDump.sqlDump("C:/Program Files/MySQL/MySQL Server 5.5/bin/mysqldump -uroot -proot dusdb", "e:/DBBack/dusdb.sql"); if(flag) { pw.print("<script type=‘text/javascript‘> alert(‘备份数据库成功! SQL脚本路径:e:/DBBack/dusdb.sql‘); </script>"); // 添加操作日志 Log log = new Log(); log.setUser_id(userId); log.setContent("数据库备份"); log.setTime(new Date()); LogService logService = new LogService(); try { logService.save(log); } catch (AppException e) { e.printStackTrace(); } } else { pw.print("<script type=‘text/javascript‘> alert(‘备份数据库失败!‘); </script>"); } } } }
以上是关于MySQL代码备份的主要内容,如果未能解决你的问题,请参考以下文章
mysql大网站数据库的备份,怎么进行增量备份和完全备份快速一点,用啥工具吗,还是代码!