日期:2014-05-16  浏览次数:20652 次

mysql 本地备份,解决乱码问题
public void dump(String ip,String subarea) throws IOException {// 本地备份
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

String sAccount="-uroot"; //帐号
String sPassword="-proot";// 密码
String sDBName="oa_db";  //备份数据库名
String path=subarea+":/test";
String sBackupPath = path + "/oa_db" + (format.format(new Date()))
+ ".sql";// 备份路径
String sCommand="cmd /c mysqldump -h "+ip;//h 后边的参数可以是ip,也可以是域名;
sCommand += " " + sAccount;
sCommand += " " + sPassword;
sCommand += " " + sDBName;

File file = new File(path);
if (!file.exists()) {
file.mkdir();
}

Runtime run = Runtime.getRuntime();
Process process = run.exec(sCommand);
InputStreamReader ir = new InputStreamReader(process.getInputStream(),"utf8");
PrintWriter p=new PrintWriter(new OutputStreamWriter(new FileOutputStream(sBackupPath),"utf8"));
LineNumberReader input = new LineNumberReader(ir);
String line;
while ((line = input.readLine()) != null){
p.println(line);

}
input.close();
p.close();


file = null;
format = null;

}

/**
* 导入 数据库
*
* @throws IOException
*
*/
public void load(String path) throws IOException {// 还原

String common = "cmd /c mysql -uroot -proot oa_db < d:/mysqlbackup/"
+ path;

Runtime run = Runtime.getRuntime();
run.exec(common);

}