package com.itenp.gen.action;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.itenp.gen.common.Constants;
import com.itenp.gen.common.ParameterUtil;
import com.itenp.gen.service.i.BackUpServiceI;
import com.itenp.gen.system.PropertiesCache;
import com.itenp.gen.vo.BackUpVO;
import com.itenp.gen.vo.UserVO;
@SuppressWarnings("serial")
@Scope("prototype")
@Controller("backupAction")
public class BackUpAction extends BaseAction {
private static Logger log = Logger.getLogger(BackUpAction.class);
private String databaseDirectory = PropertiesCache.getInstance().getDatabaseDirectory();
private String databaseName = PropertiesCache.getInstance().getDatabaseName();
private String userName = PropertiesCache.getInstance().getUserName();
private String userPwd = PropertiesCache.getInstance().getUserPwd();
private String copyDbCmd = PropertiesCache.getInstance().getCopyDbCmd();
private String copyprocCmd = PropertiesCache.getInstance().getCopyprocCmd();
private String copyedFilePath = PropertiesCache.getInstance().getCopyedFilePath();
private String restoreCmd = PropertiesCache.getInstance().getRestoreCmd();
private String msg = "";
private String msg1 = "";
private BackUpServiceI service;
@Resource(name="backupService")
public void setBackupService(BackUpServiceI service){
this.service=service;
}
@SuppressWarnings("unchecked")
public String list() throws Exception
{
HashMap map = new HashMap();
List<BackUpVO> list = (List)service.findList(map);
request.setAttribute("list", list);
log.debug("####"+PropertiesCache.getInstance().getTemplateDirectory());
return "success";
}
@SuppressWarnings("unchecked")
public String backup() throws Exception
{
Runtime rt = Runtime.getRuntime();
String str = databaseDirectory+"/"+copyDbCmd+" --no-defaults "+"-u"+userName+" "+"-p"+userPwd+" "+copyprocCmd+" "+"--set-charset=utf-8"+" "+databaseName;
Process child = rt.exec(str);
InputStream in = child.getInputStream();
InputStreamReader xx = new InputStreamReader(in, "utf8");
String inStr;
StringBuffer sb = new StringBuffer("");
String outStr;
BufferedReader br = new BufferedReader(xx);
while ((inStr = br.readLine()) != null) {
sb.append(inStr + "\r\n");
}
outStr = sb.toString();
if(outStr != null && !outStr.trim().equals("")){
msg="ok";
}
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String today=format.format(new Date());
FileOutputStream fout = new FileOutputStream(
copyedFilePath+"/"+today+"ibtts.sql" );
OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8");
writer.write(outStr);
writer.flush();
in.close();
xx.close();
br.close();
writer.close();
fout.close();
HashMap map = new HashMap();
map.put("db_nm", "ibtts");
map.put("bk_nm", today+"ibtts.sql");
map.put("bk_user",((UserVO)session.get(Constants.S_LOGIN_USER)).getUser_id());
service.create(map);
return list();
}
@SuppressWarnings("unchecked")
public String load() throws Exception
{
copytable();
Map<String, String> map = ParameterUtil.getStringMap(request,session);
BackUpVO vo=(BackUpVO) service.findById(map);
String fPath=copyedFilePath+"/"+vo.getBk_nm();
System.out.println(fPath);
Runtime rt = Runtime.getRuntime();
Process child = rt.exec(databaseDirectory+"/"+restoreCmd+" "+"-u"+userName+" "+"-p"+userPwd+" "+databaseName);
java.io.OutputStream out =child.getOutputStream();
String inStr;
StringBuffer sb = new StringBuffer("");
String outStr;
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(fPath), "utf8"));
while ((inStr = br.readLine()) != null) {
sb.append(inStr + "\r\n");
}
outStr = sb.toString();
OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
writer.write(outStr);
writer.flush();
out.close();
br.close();
writer.close();
map.put("restore_user", ((UserVO)session.get(Constants.S_LOGIN_USER)).getUser_id());
restoretable(map);
request.getSession().setAttribute("dbmap", map);
msg1 = "tiaozhuan";
return "success";
}
public void copytable() throws Exception
{
Runtime rt = Runtime.getRuntime();
Process child = rt.exec(databaseDirectory+"/"+copyDbCmd+" --no-defaults "+"-u"+userName+" "+"-p"+userPwd+" "+"--set-charset=utf8"+" "+databaseName+" "+"t_a_dbbak");
InputStream in = child.getInputStream();
InputStreamReader xx = new InputStreamReader(in, "utf8");
String inStr;
StringBuffer sb = new StringBuffer("");
String outStr;
BufferedReader br = new BufferedReader(xx);
while ((inStr = br.readLine()) != null) {
sb.append(inStr + "\r\n");
}
outStr = sb.toString();
FileOutputStream fout = new FileOutputStream(
copyedFilePath+"/"+"ibtts_talbe.sql" );
OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8");
writer.write(outStr);
writer.flush();
in.close();
xx.close();
br.close();
writer.close();
fout.close();
}
public void restoretable(Map map) throws Exception
{
String fPath=copyedFilePath+"/"+"ibtts_talbe.sql";
Runtime rt = Runtime.getRuntime();
Process child = rt.exec(databaseDirectory+"/"+restoreCmd+" "+"-u"+userName+" "+"-p"+userPwd+" "+databaseName);
System.out.println(databaseDirectory+"/"+restoreCmd+" "+"-u"+userName+" "+"-p"+userPwd+" "+databaseName);
java.io.OutputStream out =child.getOutputStream();
String inStr;
StringBuffer sb = new StringBuffer("");
String outStr;
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(fPath), "utf8"));
while ((inStr = br.readLine()) != null) {
sb.append(inStr + "\r\n");
}
outStr = sb.toString();
OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
writer.write(outStr);
writer.flush();
out.close();
br.close();
writer.close();
}
@SuppressWarnings("unchecked")
public String upbak() throws Exception{
HashMap map = new HashMap();
map = (HashMap)request.getSession().getAttribute("dbmap");
int sign=0;
BackUpVO vo=null;
while(vo==null){
vo=(BackUpVO) service.findById(map);
if(vo!=null||sign==100){
break;
}
sign++;
}
service.modify(map);
return list();
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getMsg1() {
return msg1;
}
public void setMsg1(String msg1) {
this.msg1 = msg1;
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結