Exemple #1
0
  /**
   * 还原数据库
   *
   * @param fileName
   * @param error
   * @return
   */
  public static int recover(String fileName, ErrorInfo error) {
    error.clear();

    if (StringUtils.isBlank(fileName)) {
      error.code = -1;
      error.msg = "恢复文件不能为空";

      return error.code;
    }

    String backupFileName = backup(false, error);

    if (null == backupFileName) {
      return error.code;
    }

    if (0 != createOperation(DBOperationType.RECOVER, backupFileName, error)) {
      return error.code;
    }

    DealDetail.supervisorEvent(
        Supervisor.currSupervisor().id, SupervisorEvent.DB_RECOVER, "还原运营数据", error);

    if (error.code < 0) {
      JPA.setRollbackOnly();

      return error.code;
    }

    String decryptFileName = Constants.SQL_PATH + UUID.randomUUID().toString() + ".sql";

    if (!FileEncrypt.decrypt(fileName, decryptFileName, Constants.ENCRYPTION_KEY)) {
      error.code = -1;
      error.msg = "还原数据库失败";

      return error.code;
    }

    if (0
        != MySQLTool.executeSqlFile(
            username, password, host, port, database, decryptFileName, error)) {
      JPA.setRollbackOnly();

      return error.code;
    }

    if (!new File(decryptFileName).delete()) {
      error.code = -1;
      error.msg = "还原数据库失败";

      return error.code;
    }

    error.code = 0;
    error.msg = "还原数据库成功";

    return error.code;
  }
Exemple #2
0
  /**
   * 备份数据库
   *
   * @param isVisual 是否在数据库操作记录表中可见
   * @param error
   * @return
   */
  public static String backup(boolean isVisual, ErrorInfo error) {
    error.clear();

    String fileName = Constants.SQL_PATH + UUID.randomUUID().toString();
    FileUtil.getStore(Constants.SQL_PATH);

    if (0 != MySQLTool.dumpSqlFile(username, password, host, port, database, fileName, error)) {
      return null;
    }

    if (!FileEncrypt.encrypt(fileName, Constants.ENCRYPTION_KEY)) {
      error.code = -1;
      error.msg = "备份数据库失败";

      return null;
    }

    if (isVisual) {
      if (0 != createOperation(DBOperationType.BACKUP, fileName, error)) {
        return null;
      }

      DealDetail.supervisorEvent(
          Supervisor.currSupervisor().id, SupervisorEvent.DB_BACKUP, "备份数据", error);

      if (error.code < 0) {
        JPA.setRollbackOnly();

        return null;
      }
    }

    error.code = 0;
    error.msg = "备份数据库成功";

    return fileName;
  }