mysql数据库的备份还原
/**
* 备份
* @param mapping
* @param form
* @param request
* @param response
* @return
*/
public ActionForward backUpSmu(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
//String command="C:\\nvr2000\\mysql\\backup.bat";
Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
InputStream in = null;
try {
in = new FileInputStream("C:\\nvr2000\\conf\\config.properties");
Properties p = new Properties();
p.load(in);
// 得到MYSQL的用户名密码后调用 mysql 的 cmd:
String s= df.format(date);
s = s.replace(" ", "-").replace(":", "-");
StringBuffer sb = new StringBuffer();
sb.append(" mysqldump ");
sb.append("--opt ");
sb.append("-h ");
sb.append("localhost");
sb.append(" ");
sb.append("--user=");
sb.append(p.get("jdbc.username"));
sb.append(" ");
sb.append("--password=");
sb.append(p.get("jdbc.password"));
sb.append(" ");
sb.append("--lock-all-tables=true ");
sb.append("--result-file=");
sb.append("C:\\nvr2000\\initsql\\");
sb.append("smubak-"+s+".sql");
sb.append(" ");
sb.append("--default-character-set=utf8 ");
sb.append("smu");
Runtime cmd = Runtime.getRuntime();
Process pro = cmd.exec(sb.toString());
pro.waitFor();
response.setContentType(
"application/msword;charset=UTF-8");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
/**
* 恢复
* @param mapping
* @param form
* @param request
* @param response
* @return
*/
public ActionForward recoverSmu(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
String fileName = request.getParameter("fileName");
InputStream in = null;
try {
in = new FileInputStream("C:\\nvr2000\\conf\\config.properties");
Properties p = new Properties();
p.load(in);
String stmt = "mysql -u " + p.get("jdbc.username") + " -p" + p.get("jdbc.password") +" smu < C:\\nvr2000\\initsql\\"+fileName;
System.out.println("开始完全恢复: "+stmt);
String[] cmd = { "cmd", "/c", stmt };
Runtime.getRuntime().exec(cmd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
public ActionForward getLocalBackFiles(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
File file = new File("C:\\nvr2000\\initsql\\");
String [] li = file.list();
JSONObject jo = new JSONObject();
List<Object> l = new ArrayList<Object>();
for(int i = 0;i<li.length;i++){
Map<String ,String> map = new HashMap<String,String>();
map.put("fileName", li[i]);
l.add(map);
}
jo.put("files", l);
System.out.println(jo);
try {
response.getWriter().print(jo);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}