class DbBackUpCallable implements Callable<Object>{
String tableName = null;
public DbBackUpCallable(String tableName){
this.tableName = tableName;
}
@Override
public Object call() {
try {
String remoteDB = pros.getProperty("remoteDB");
String DBSeverport = pros.getProperty("DBSeverport");
String dbtype = pros.getProperty("dbtype");
String username = pros.getProperty("username");
String password = pros.getProperty("password");
String address = pros.getProperty("dbAddress");
String databaseName = pros.getProperty("databaseName");
String dbpath = pros.getProperty("dbpath");
String sqlpath = pros.getProperty("sqlFilePath");
String ffilename = DateUtil.getSdfTimes();
String commandStr = "";
if(!"sqlserver".equals(dbtype)){
sqlpath = sqlpath+DateUtil.getDays()+"/";
if("yes".equals(remoteDB)){
commandStr = DbFH.getExecStr(dbtype,dbpath,"localhost",username,password,sqlpath,tableName,databaseName,ffilename);
Socket ss = null;
DataOutputStream bb = null;
DataInputStream dat = null;
ss = new Socket(address, Integer.parseInt(DBSeverport));
bb = new DataOutputStream(ss.getOutputStream());
dat = new DataInputStream(ss.getInputStream());
bb.writeUTF("FHQ313596790"+commandStr+"FH313596790"+sqlpath);
bb.flush();
Boolean llm = true;
while(llm){
String returnstr = dat.readUTF();
if("errer".equals(returnstr)){
return returnstr;
}
llm = false;
ss.close();
bb.close();
dat.close();
}
}else{
FileUtil.createDir(sqlpath+"/fh.fh");
commandStr = DbFH.getExecStr(dbtype,dbpath,address,username,password,sqlpath,tableName,databaseName,ffilename);
Runtime cmd = Runtime.getRuntime();
Process p = cmd.exec(commandStr);
p.waitFor();
}
}else{
String spath = sqlpath + databaseName + "_"+ffilename + ".bak";
String bakSQL = "backup database "+databaseName+" to disk=? with init";
PreparedStatement bak = DbFH.getFHCon().prepareStatement(bakSQL);
bak.setString(1, spath);
bak.execute();
bak.close();
}
String fileType=".bak";
if("mysql".equals(dbtype)){
fileType=".sql";
}else if("oracle".equals(dbtype)){
fileType=".DMP";
}
if("".equals(tableName)){
return sqlpath+databaseName+"_"+ffilename+fileType;
}else{
return sqlpath+tableName+"_"+ffilename+fileType;
}
} catch (Exception e) {
logger.error("備份操作出現問題", e);
return "errer";
}finally{
backUpTableList.remove(tableName);
}
}
}
public static String getExecStr(String dbtype,String dbpath,String address,String username,String password,String sqlpath,String tableName,String databaseName,String ffilename){
StringBuffer sb = new StringBuffer();
if("mysql".equals(dbtype)){
address = "localhost";
sb.append(dbpath);
sb.append("mysqldump ");
sb.append("--opt ");
sb.append("-h ");
sb.append(address);
sb.append(" ");
sb.append("--user=");
sb.append(username);
sb.append(" ");
sb.append("--password=");
sb.append(password);
sb.append(" ");
sb.append("--lock-all-tables=true ");
sb.append("--result-file=");
sb.append(sqlpath);
sb.append(("".equals(tableName)?databaseName+"_"+ffilename:tableName+"_"+ffilename)+".sql");
sb.append(" ");
sb.append("--default-character-set=utf8 ");
sb.append(databaseName);
sb.append(" ");
sb.append(tableName);
}else if("oracle".equals(dbtype)){
if("".equals(tableName)){
sb.append("EXP "+username+"/"+password+" BUFFER=880000 FILE="+sqlpath+username+"_"+ffilename+".DMP LOG="+sqlpath+username+"_"+ffilename+".LOG OWNER="+username);
}else{
sb.append("EXP "+username+"/"+password+" BUFFER=880000 FILE="+sqlpath+tableName+"_"+ffilename+".DMP LOG="+sqlpath+tableName+"_"+ffilename+".LOG TABLES=("+username+"."+tableName+")");
}
}
return sb.toString();
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結