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

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-09-23 14:12:37


点击(此处)折叠或打开


  1.     /**用于执行某表的备份(内部类)线程
  2.      * @from fhadmin.cn
  3.      * Callable 有返回值的线程接口
  4.      */
  5.     class DbBackUpCallable implements Callable<Object>{
  6.         String tableName = null;
  7.         public DbBackUpCallable(String tableName){
  8.             this.tableName = tableName;
  9.         }
  10.         @Override
  11.         public Object call() {
  12.             try {
  13.                 String remoteDB = pros.getProperty("remoteDB");            //是否远程备份数据库 yes or no
  14.                 String DBSeverport = pros.getProperty("DBSeverport");    //远程服务器备份程序端口
  15.                 String dbtype = pros.getProperty("dbtype");                //数据库类型
  16.                 String username = pros.getProperty("username");            //用户名
  17.                 String password = pros.getProperty("password");            //密码
  18.                 String address = pros.getProperty("dbAddress");            //数据库连接地址
  19.                 String databaseName = pros.getProperty("databaseName");    //数据库名
  20.                 String dbpath = pros.getProperty("dbpath");            //数据库的安装路径
  21.                 String sqlpath = pros.getProperty("sqlFilePath");        //存储路径
  22.                 String ffilename = DateUtil.getSdfTimes();
  23.                 String commandStr = "";

  24.                 if(!"sqlserver".equals(dbtype)){
  25.                     sqlpath = sqlpath+DateUtil.getDays()+"/";            //日期当路径分支
  26.                     if("yes".equals(remoteDB)){//数据库另外一台服务器上(和tomcat不在同一台服务器上)
  27.                         commandStr = DbFH.getExecStr(dbtype,dbpath,"localhost",username,password,sqlpath,tableName,databaseName,ffilename); //命令语句
  28.                         Socket ss = null;
  29.                         DataOutputStream bb = null;
  30.                         DataInputStream dat = null;
  31.                         ss = new Socket(address, Integer.parseInt(DBSeverport));//连接远程服务器数据库备份程序
  32.                         bb = new DataOutputStream(ss.getOutputStream());
  33.                         dat = new DataInputStream(ss.getInputStream());
  34.                         bb.writeUTF("FHQ313596790"+commandStr+"FH313596790"+sqlpath);    //发送指令给服务端
  35.                         bb.flush();
  36.                         Boolean llm = true;
  37.                         while(llm){
  38.                             String returnstr = dat.readUTF();
  39.                             if("errer".equals(returnstr)){
  40.                                 return returnstr;    //远程服务器备份失败或超时
  41.                             }
  42.                             llm = false;
  43.                             ss.close();
  44.                             bb.close();
  45.                             dat.close();
  46.                         }
  47.                     }else{                            //数据库在本地(和tomcat在同一台服务器上)
  48.                         FileUtil.createDir(sqlpath+"/fh.fh");
  49.                         commandStr = DbFH.getExecStr(dbtype,dbpath,address,username,password,sqlpath,tableName,databaseName,ffilename); //命令语句
  50.                         Runtime cmd = Runtime.getRuntime();
  51.                         Process p = cmd.exec(commandStr);
  52.                         p.waitFor();                 // 该语句用于标记,如果备份没有完成,则该线程持续等待
  53.                     }
  54.                 }else{//当数据库为sqlserver时 只能备份整库,不能单表备份
  55.                     String spath = sqlpath + databaseName + "_"+ffilename + ".bak";// name文件名
  56.          String bakSQL = "backup database "+databaseName+" to disk=? with init";// SQL语句
  57.          PreparedStatement bak = DbFH.getFHCon().prepareStatement(bakSQL);
  58.          bak.setString(1, spath);// path必须是绝对路径
  59.          bak.execute();             // 备份数据库
  60.          bak.close();
  61.                 }
  62.                 String fileType=".bak";
  63.                 if("mysql".equals(dbtype)){
  64.                     fileType=".sql";
  65.                 }else if("oracle".equals(dbtype)){
  66.                     fileType=".DMP";
  67.                 }
  68.                 if("".equals(tableName)){
  69.                     return sqlpath+databaseName+"_"+ffilename+fileType;
  70.                 }else{
  71.                     return sqlpath+tableName+"_"+ffilename+fileType;
  72.                 }
  73.             } catch (Exception e) {
  74.                 logger.error("备份操作出现问题", e);
  75.                 return "errer";
  76.             }finally{
  77.                 backUpTableList.remove(tableName); // 最终都将解除
  78.             }
  79.         }
  80.     }
  81.     
  82.     /**数据库备份命令字符串
  83.      * @param dbtype 数据库类型
  84.      * @param dbpaths 数据库的路径
  85.      * @param address 数据库连接地址
  86.      * @param username 用户名
  87.      * @param password 密码
  88.      * @param sqlpath 存储路径
  89.      * @param tableName 表名
  90.      * @param databaseName 数据库名
  91.      * @param ffilename 日期当路径和保存文件名的后半部分
  92.      * @return 完整的命令字符串
  93.      */
  94.     public static String getExecStr(String dbtype,String dbpath,String address,String username,String password,String sqlpath,String tableName,String databaseName,String ffilename){
  95.         StringBuffer sb = new StringBuffer();
  96.         if("mysql".equals(dbtype)){
  97.             address = "localhost";
  98.             sb.append(dbpath);
  99.             sb.append("mysqldump ");
  100.             sb.append("--opt ");
  101.             sb.append("-h ");
  102.             sb.append(address);
  103.             sb.append(" ");
  104.             sb.append("--user=");
  105.             sb.append(username);
  106.             sb.append(" ");
  107.             sb.append("--password=");
  108.             sb.append(password);
  109.             sb.append(" ");
  110.             sb.append("--lock-all-tables=true ");
  111.             sb.append("--result-file=");
  112.             sb.append(sqlpath);
  113.             sb.append(("".equals(tableName)?databaseName+"_"+ffilename:tableName+"_"+ffilename)+".sql");
  114.             sb.append(" ");
  115.             sb.append("--default-character-set=utf8 ");
  116.             sb.append(databaseName);
  117.             sb.append(" ");
  118.             sb.append(tableName);//当tableName为“”时,备份整库
  119.         }else if("oracle".equals(dbtype)){
  120.             if("".equals(tableName)){//备份整库
  121.                 sb.append("EXP "+username+"/"+password+" BUFFER=880000 FILE="+sqlpath+username+"_"+ffilename+".DMP LOG="+sqlpath+username+"_"+ffilename+".LOG OWNER="+username);
  122.             }else{//备份单表
  123.                 sb.append("EXP "+username+"/"+password+" BUFFER=880000 FILE="+sqlpath+tableName+"_"+ffilename+".DMP LOG="+sqlpath+tableName+"_"+ffilename+".LOG TABLES=("+username+"."+tableName+")");
  124.             }
  125.         }
  126.         return sb.toString();
  127.     }


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