/**
* Description:备份数据库
* @param username
* @param password
* @param cmd
* @param dir
* @param db_name
* @return
*/
public boolean backup(String username, String password, String cmd, String dir,String db_name) {
boolean bool = false;
File f=new File(dir);
if(!f.isDirectory()){
f.mkdirs();
}
// dir += db_name + new SimpleDateFormat("yyyyMMdd_HH").format(new Date()) + ".sql";
cmd = "\""+cmd + "\" -u " + username + " --password=" + password + " "+db_name+" > " + dir;
System.out.println(cmd);
try {
Process p = Runtime.getRuntime().exec("cmd /C " + cmd);
p.waitFor();
bool = true;
} catch (Exception e) {
e.printStackTrace();
}
return bool;
}
/**
* Description:恢复数据
* @param username
* @param password
* @param cmd
* @param dir
* @param db_name
* @return
*/
public boolean restore(String username, String password, String cmd, String dir,String db_name) {
boolean bool = false;
cmd = "\""+cmd + "\" -u " + username + " --password=" + password + " "+db_name+" < " + dir;
System.out.println(cmd);
try {
Process p = Runtime.getRuntime().exec("cmd /C " + cmd);
p.waitFor();
bool = true;
} catch (Exception e) {
e.printStackTrace();
}
return bool;
}
public static void main(String args[]){
SysQuartzJobBean sqb = new SysQuartzJobBean();
boolean bl = sqb.restore("jcsj", "123456",
"D:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysqldump",
"d:\\jbpm20141023_13.sql", "jbpm");
System.out.println(bl ? "备份成功" : "备份失败");
bl = sqb.restore("jcsj", "123456",
"D:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysql",
"d:\\jbpm20141023_13.sql", "jbpm");
System.out.println(bl ? "恢复成功" : "恢复失败");
}