Chinaunix首页 | 论坛 | 博客
  • 博客访问: 604385
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-07-09 13:00:03


点击(此处)折叠或打开


  1. package com.itenp.gen.action;

  2. import java.io.BufferedReader;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStreamWriter;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;

  13. import javax.annotation.Resource;

  14. import org.apache.log4j.Logger;
  15. import org.springframework.context.annotation.Scope;
  16. import org.springframework.stereotype.Controller;

  17. import com.itenp.gen.common.Constants;
  18. import com.itenp.gen.common.ParameterUtil;
  19. import com.itenp.gen.service.i.BackUpServiceI;
  20. import com.itenp.gen.system.PropertiesCache;
  21. import com.itenp.gen.vo.BackUpVO;
  22. import com.itenp.gen.vo.UserVO;

  23. /** 说明:数据备份还原
  24.  * 作者:fhadmin
  25.  * from fhadmin.cn
  26. */
  27. @SuppressWarnings("serial")
  28. @Scope("prototype")
  29. @Controller("backupAction")
  30. public class BackUpAction extends BaseAction {
  31.     
  32.      private static Logger log = Logger.getLogger(BackUpAction.class);
  33.     
  34.      private String databaseDirectory = PropertiesCache.getInstance().getDatabaseDirectory(); //数据库bin路径
  35.      private String databaseName = PropertiesCache.getInstance().getDatabaseName();//数据库名
  36.      private String userName = PropertiesCache.getInstance().getUserName();
  37.      private String userPwd = PropertiesCache.getInstance().getUserPwd();
  38.      private String copyDbCmd = PropertiesCache.getInstance().getCopyDbCmd();//数据库备份命令
  39.      private String copyprocCmd = PropertiesCache.getInstance().getCopyprocCmd();//存储工程备份命令
  40.      private String copyedFilePath = PropertiesCache.getInstance().getCopyedFilePath(); //备份后的文件存放位置
  41.      private String restoreCmd = PropertiesCache.getInstance().getRestoreCmd();        //数据库还原命令
  42.     
  43.      private String msg = "";
  44.      private String msg1 = "";
  45.     
  46.     
  47.     
  48.     
  49.     private BackUpServiceI service;
  50.     
  51.     @Resource(name="backupService")
  52.     public void setBackupService(BackUpServiceI service){
  53.         
  54.         this.service=service;
  55.     }
  56.     
  57.     @SuppressWarnings("unchecked")
  58.     public String list() throws Exception
  59.     {
  60.         //System.out.println("copyedFilePath"+copyedFilePath);
  61.         HashMap map = new HashMap();
  62.         
  63.         //得到符合条件的记录List
  64.         List<BackUpVO> list = (List)service.findList(map);
  65.     
  66.         request.setAttribute("list", list);

  67.         log.debug("####"+PropertiesCache.getInstance().getTemplateDirectory());
  68.     //    System.out.println(PropertiesCache.getInstance().getTemplateDirectory());
  69.         return "success";
  70.     }
  71.     
  72.     @SuppressWarnings("unchecked")
  73.     public String backup() throws Exception
  74.     {
  75.                     Runtime rt = Runtime.getRuntime();
  76.         
  77.          // 调用 mysql 的 cmd:
  78.     
  79.                     
  80.                     
  81.                     /*System.out.println("数据库bin路径"+databaseDirectory);
  82.                     System.out.println("数据库名"+databaseName);
  83.                     System.out.println("用户名"+userName);
  84.                     System.out.println("密码"+userPwd);
  85.                     System.out.println("数据库备份命令"+copyDbCmd);
  86.                     System.out.println("存储工程备份命令"+copyprocCmd);*/
  87.                     
  88.                     String str = databaseDirectory+"/"+copyDbCmd+" --no-defaults "+"-u"+userName+" "+"-p"+userPwd+" "+copyprocCmd+" "+"--set-charset=utf-8"+" "+databaseName;
  89.                     
  90.                     //str = "D:\SOFT\MySQL\MySQL Server 5.5\bin/mysqldump -uroot -proot -R --set-charset=utf-8 nlnk";
  91.                     
  92.                     //System.out.println(str);
  93.                     
  94.          // Process child = rt.exec(databaseDirectory+"/"+copyDbCmd+" "+"-u"+userName+" "+"-p"+userPwd+" "+copyprocCmd+" "+"--set-charset=utf-8"+" "+databaseName);// 设置导出编码为utf8。这里必须是utf8
  95.                     Process child = rt.exec(str);// 设置导出编码为utf8。这里必须是utf8
  96.                     
  97.          //调用mysql的cmd:备份某个表
  98.          // Process childtable = rt
  99.                     // .exec("D:/db/mysql/mysql-5.0.45-win32/bin/mysqldump -uroot -pmysql --set-charset=utf8 ibtts t_a_dbbak");// 设置导出编码为utf8。这里必须是utf8
  100.                     // Process childtable = rt
  101.                     // .exec(databaseDirectory+"/"+copyDbCmd+" "+"-u"+userName+" "+"-p"+userPwd+" "+"--set-charset=utf8"+" "+databaseName+" "+"t_a_dbbak");// 设置导出编码为utf8。这里必须是utf8
  102.         
  103.          // 把进程执行中的控制台输出信息写入.sql文件,即生成了备份文件。注:如果不对控制台信息进行读出,则会导致进程堵塞无法运行
  104.          InputStream in = child.getInputStream();// 控制台的输出信息作为输入流
  105.         
  106.          InputStreamReader xx = new InputStreamReader(in, "utf8");// 设置输出流编码为utf8。这里必须是utf8,否则从流中读入的是乱码
  107.         
  108.          String inStr;
  109.          StringBuffer sb = new StringBuffer("");
  110.          String outStr;
  111.          // 组合控制台输出信息字符串
  112.          BufferedReader br = new BufferedReader(xx);
  113.         
  114.          // System.out.println("------------"+br.readLine());
  115.         
  116.          while ((inStr = br.readLine()) != null) {
  117.          sb.append(inStr + "\r\n");
  118.          }
  119.          outStr = sb.toString();
  120.         
  121.          //判断是否保存成功
  122.          if(outStr != null && !outStr.trim().equals("")){
  123.               msg="ok";
  124.          }
  125.         
  126.         
  127.          //System.out.println("-----"+outStr);
  128.              //创建文件名称
  129.          SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
  130.          String today=format.format(new Date());
  131.         
  132.              //String targetDirectory = scontext.getRealPath("/WEB-INF/DBbackup");
  133.                  //    System.out.println(targetDirectory);
  134.          // 要用来做导入用的sql目标文件:D:/ibtts.sql
  135.         
  136.          FileOutputStream fout = new FileOutputStream(
  137.                   copyedFilePath+"/"+today+"ibtts.sql" );
  138.          OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8");
  139.          writer.write(outStr);
  140.          // 注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免
  141.          writer.flush();
  142.         
  143.          // 别忘记关闭输入输出流
  144.          in.close();
  145.          xx.close();
  146.          br.close();
  147.          writer.close();
  148.          fout.close();
  149.         
  150.          // System.out.println("/* Output OK! */");


  151.          //数据添加到数据库
  152.          HashMap map = new HashMap();
  153.         
  154.          map.put("db_nm", "ibtts");
  155.          map.put("bk_nm", today+"ibtts.sql");
  156.          map.put("bk_user",((UserVO)session.get(Constants.S_LOGIN_USER)).getUser_id());
  157.         
  158.          service.create(map);
  159.         
  160.          // copytable();
  161.         
  162.          return list();
  163.          }
  164.     
  165.     /**
  166.      * 数据的恢复
  167.      */
  168.     @SuppressWarnings("unchecked")
  169.     public String load() throws Exception
  170.     {        
  171.         copytable();
  172.         
  173.         //得到页面所有参数
  174.         Map<String, String> map = ParameterUtil.getStringMap(request,session);
  175.         //String targetDirectory = scontext.getRealPath("/WEB-INF/DBbackup");
  176.         
  177.         //获取id后进行查询
  178.         BackUpVO vo=(BackUpVO) service.findById(map);
  179.         
  180.         String fPath=copyedFilePath+"/"+vo.getBk_nm();
  181.         
  182.         System.out.println(fPath);
  183.         
  184.         //map.put("Id", vo.getId());

  185.         //String fPath = "D:/ibtts.sql";
  186.         Runtime rt = Runtime.getRuntime();

  187.         // 调用 mysql 的 cmd:
  188.         //Process child = rt.exec("D:/db/mysql/mysql-5.0.45-win32/bin/mysql -uroot -pmysql ibtts");
  189.         Process child = rt.exec(databaseDirectory+"/"+restoreCmd+" "+"-u"+userName+" "+"-p"+userPwd+" "+databaseName);
  190.         
  191.         //System.out.println(databaseDirectory+"/"+restoreCmd+" "+"-u"+userName+" "+"-p"+userPwd+" "+databaseName);
  192.         
  193.         java.io.OutputStream out =child.getOutputStream();//控制台的输入信息作为输出流
  194.         String inStr;
  195.         StringBuffer sb = new StringBuffer("");
  196.         String outStr;
  197.         BufferedReader br = new BufferedReader(new InputStreamReader(
  198.         new FileInputStream(fPath), "utf8"));
  199.         while ((inStr = br.readLine()) != null) {
  200.         sb.append(inStr + "\r\n");
  201.         }
  202.         outStr = sb.toString();

  203.         OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
  204.         writer.write(outStr);
  205.         // 注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免
  206.         writer.flush();
  207.         // 别忘记关闭输入输出流
  208.         out.close();
  209.         br.close();
  210.         writer.close();
  211.         
  212.         //获取还原用户
  213.         map.put("restore_user", ((UserVO)session.get(Constants.S_LOGIN_USER)).getUser_id());
  214.         
  215.         //service.modify(map);
  216.         
  217.         restoretable(map);            //---------------
  218.         
  219.         //System.out.println(map);

  220.         //System.out.println("恢复成功");
  221.         
  222.         request.getSession().setAttribute("dbmap", map);
  223.         
  224.         msg1 = "tiaozhuan";
  225.         
  226.         return "success";
  227.         }
  228.     
  229.     
  230.     //对特定表的处理
  231.     public void copytable() throws Exception
  232.     {
  233.         Runtime rt = Runtime.getRuntime();
  234.         
  235.         // 调用 mysql 的 cmd:
  236.        
  237.          //调用mysql的cmd:备份某个表
  238.          //Process child = rt
  239.         // .exec("D:/db/mysql/mysql-5.0.45-win32/bin/mysqldump -uroot -pmysql --set-charset=utf8 ibtts t_a_dbbak");// 设置导出编码为utf8。这里必须是utf8
  240.          
  241.          Process child = rt.exec(databaseDirectory+"/"+copyDbCmd+" --no-defaults "+"-u"+userName+" "+"-p"+userPwd+" "+"--set-charset=utf8"+" "+databaseName+" "+"t_a_dbbak");// 设置导出编码为utf8。这里必须是utf8
  242.          // 把进程执行中的控制台输出信息写入.sql文件,即生成了备份文件。注:如果不对控制台信息进行读出,则会导致进程堵塞无法运行
  243.          InputStream in = child.getInputStream();// 控制台的输出信息作为输入流
  244.       
  245.         // System.out.println(databaseDirectory+"/"+copyDbCmd+" --no-defaults "+"-u"+userName+" "+"-p"+userPwd+" "+"--set-charset=utf8"+" "+databaseName+" "+"t_a_dbbak");
  246.          
  247.          InputStreamReader xx = new InputStreamReader(in, "utf8");// 设置输出流编码为utf8。这里必须是utf8,否则从流中读入的是乱码
  248.         
  249.          
  250.          String inStr;
  251.          StringBuffer sb = new StringBuffer("");
  252.          String outStr;
  253.          // 组合控制台输出信息字符串
  254.          BufferedReader br = new BufferedReader(xx);
  255.         while ((inStr = br.readLine()) != null) {
  256.              sb.append(inStr + "\r\n");
  257.          }
  258.          outStr = sb.toString();
  259.          

  260.          
  261.          //String targetDirectory = scontext.getRealPath("/WEB-INF/DBbackup");
  262.   
  263.          // 要用来做导入用的sql目标文件:D:/ibtts.sql
  264.        
  265.          FileOutputStream fout = new FileOutputStream(
  266.                  copyedFilePath+"/"+"ibtts_talbe.sql" );
  267.          OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8");
  268.          writer.write(outStr);
  269.         // 注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免
  270.          writer.flush();

  271.          // 别忘记关闭输入输出流
  272.          in.close();
  273.          xx.close();
  274.          br.close();
  275.          writer.close();
  276.          fout.close();
  277.  
  278.     }
  279.     
  280.     public void restoretable(Map map) throws Exception
  281.     {
  282.         
  283.         String fPath=copyedFilePath+"/"+"ibtts_talbe.sql";
  284.         

  285.         //String fPath = "D:/ibtts.sql";
  286.         Runtime rt = Runtime.getRuntime();

  287.         // 调用 mysql 的 cmd:
  288.         //Process child = rt.exec("D:/db/mysql/mysql-5.0.45-win32/bin/mysql -uroot -pmysql ibtts");
  289.         Process child = rt.exec(databaseDirectory+"/"+restoreCmd+" "+"-u"+userName+" "+"-p"+userPwd+" "+databaseName);
  290.         
  291.         System.out.println(databaseDirectory+"/"+restoreCmd+" "+"-u"+userName+" "+"-p"+userPwd+" "+databaseName);
  292.         
  293.         java.io.OutputStream out =child.getOutputStream();//控制台的输入信息作为输出流
  294.         String inStr;
  295.         StringBuffer sb = new StringBuffer("");
  296.         String outStr;
  297.         BufferedReader br = new BufferedReader(new InputStreamReader(
  298.         new FileInputStream(fPath), "utf8"));
  299.         while ((inStr = br.readLine()) != null) {
  300.         sb.append(inStr + "\r\n");
  301.         }
  302.         outStr = sb.toString();

  303.         OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
  304.         writer.write(outStr);
  305.         // 注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免
  306.         writer.flush();
  307.         // 别忘记关闭输入输出流
  308.         

  309.         
  310.         out.close();
  311.         br.close();
  312.         writer.close();
  313.         
  314.         
  315.         //System.out.println(map);
  316.         
  317.         /*int sign=0;
  318.         BackUpVO vo=null;
  319.         while(vo==null){
  320.             vo=(BackUpVO) service.findById(map);
  321.             if(vo!=null||sign==100){
  322.                 break;
  323.             }
  324.             sign++;
  325.         }
  326.     
  327.     
  328.         service.modify(map);*/
  329.         
  330.     }
  331.     
  332.     @SuppressWarnings("unchecked")
  333.     public String upbak() throws Exception{
  334.         HashMap map = new HashMap();
  335.         map = (HashMap)request.getSession().getAttribute("dbmap");
  336.         
  337.         int sign=0;
  338.         BackUpVO vo=null;
  339.         while(vo==null){
  340.             vo=(BackUpVO) service.findById(map);
  341.             if(vo!=null||sign==100){
  342.                 break;
  343.             }
  344.             sign++;
  345.         }
  346.     
  347.     
  348.         service.modify(map);
  349.         
  350.         return list();
  351.     }

  352.     public String getMsg() {
  353.         return msg;
  354.     }

  355.     public void setMsg(String msg) {
  356.         this.msg = msg;
  357.     }

  358.     public String getMsg1() {
  359.         return msg1;
  360.     }

  361.     public void setMsg1(String msg1) {
  362.         this.msg1 = msg1;
  363.     }
  364.     
  365. }


阅读(1913) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~