/**
* 备份mysql数据库
* @param username 账号
* @param pwd 密码
* @param url 地址
* @param path 路径
* @param tableName 数据库名 表名
* @throws Exception
*/
public static void dbBackUpMysql(String username,String pwd,String url,String path,String tableName) throws Exception {
//mysqldump -h 127.0.0.1 -uroot -proot mysql user >D:/info/server/var/backupdata/backups.sql
String dbName = "mysql";
dbName += " "+tableName;
String pathSql = path+tableName+".sql";
File fileSql = new File(pathSql);
File filePath = new File(path);
//创建备份sql文件
if (!filePath.exists()){
fileSql.mkdirs();
}
if (!fileSql.exists()){
fileSql.createNewFile();
}
//mysqldump -hlocalhost -uroot -p123456 db > /home/back.sql
StringBuffer sb = new StringBuffer();
sb.append("mysqldump");
sb.append(" -h"+url);
sb.append(" -u"+username);
sb.append(" -p"+pwd);
sb.append(" "+dbName+" >");
sb.append(pathSql);
System.out.println("cmd命令为:"+sb.toString());
System.out.println("开始备份:"+dbName);
Process process = null;
//判断操作系统 windwos与linux使用的语句不一样
if(System.getProperty("os.name").toLowerCase().indexOf("windows") > -1){
process = Runtime.getRuntime().exec("cmd /c"+sb.toString());
}else if(System.getProperty("os.name").toLowerCase().indexOf("linux") > -1){
process = Runtime.getRuntime().exec("/bin/sh -c"+sb.toString());
}else{
throw new Exception("暂不支持该操作系统,进行数据库备份或还原!");
}
//设置超时一分钟
process.waitFor(60000, TimeUnit.MILLISECONDS);
//输出返回的错误信息
StringBuffer mes = new StringBuffer();
String tmp = "";
BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while((tmp = error.readLine()) != null){
mes.append(tmp + "\n");
}
if(mes != null || !"".equals(mes) ){
System.out.println("备份成功!==>"+mes.toString());
}
error.close();
}