按数据库备份
/**
* 备份数据库db
* @param root
* @param pwd
* @param dbName
* @param backPath
* @param backName
*/
public static void dbBackUp(String root,String pwd,String dbName,String backPath,String backName) throws Exception {
String pathSql = backPath+backName;
File fileSql = new File(pathSql);
//[1]创建备份sql文件
if (!fileSql.exists()){
fileSql.createNewFile();
}
//mysqldump -hlocalhost -uroot -p123456 db > /home/back.sql
StringBuffer sb = new StringBuffer();
sb.append("mysqldump");
sb.append(" -h127.0.0.1");
sb.append(" -u"+root);
sb.append(" -p"+pwd);
sb.append(" "+dbName+" >");
sb.append(pathSql);
Process process = null;
System.out.println("cmd命令为:"+sb.toString());
Runtime runtime = Runtime.getRuntime();
System.out.println("开始备份:"+dbName);
//[2]判断操作系统 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("暂不支持该操作系统,进行数据库备份或还原!");
}
System.out.println("备份成功!");
//[3]输出返回的错误信息
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();
}
可选择某一个数据库中的多张表备份
/**
* 备份数据库db
* @param root
* @param pwd
* @param dbName
* @param tableName
* @param backPath
* @param backName
* mysqldump test -u root -p -B --tables moni_yp > D:\db_script.sql 按某个数据库的,多张表,备份
*/
public static void dbBackUp(String root,String pwd,String dbName,String tableName,String backPath,String backName) throws Exception {
String pathSql = backPath+backName;
File fileSql = new File(pathSql);
//[1]创建备份sql文件
if (!fileSql.exists()){
fileSql.createNewFile();
}
//mysqldump -hlocalhost -uroot -p123456 db > /home/back.sql
StringBuffer sb = new StringBuffer();
sb.append("mysqldump");
sb.append(" "+dbName);
sb.append(" -h127.0.0.1");
sb.append(" -u"+root);
sb.append(" -p"+pwd);
sb.append(" --tables");
sb.append(" "+tableName+" >");
sb.append(pathSql);
Process process = null;
System.out.println("cmd命令为:"+sb.toString());
Runtime runtime = Runtime.getRuntime();
System.out.println("开始备份:"+dbName);
//[2]判断操作系统 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("暂不支持该操作系统,进行数据库备份或还原!");
}
System.out.println("备份成功!");
//[3]输出返回的错误信息
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();
}
按数据库恢复
/**
* 恢复数据库
* @param root
* @param pwd
* @param dbName
* @param filePath
* mysql -hlocalhost -uroot -p123456 db < /home/back.sql
*/
public static void dbRestore(String root,String pwd,String dbName,String filePath){
StringBuilder sb = new StringBuilder();
sb.append("mysql");
sb.append(" -h127.0.0.1");
sb.append(" -u"+root);
sb.append(" -p"+pwd);
sb.append(" "+dbName+" <");
sb.append(filePath);
System.out.println("cmd命令为:"+sb.toString());
Runtime runtime = Runtime.getRuntime();
System.out.println("开始还原数据");
try {
// Process process = runtime.exec("cmd /c"+sb.toString());
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("暂不支持该操作系统,进行数据库备份或还原!");
}
InputStream is = process.getInputStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(is,"utf8"));
String line = null;
while ((line=bf.readLine())!=null){
System.out.println(line);
}
is.close();
bf.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("还原成功!");
}
测试
@Test
void test01() throws Exception {
NPtest01.dbBackUp("root","123","test","D:\\","test.sql");
}
@Test
void test02() throws Exception {
NPtest01.dbRestore("root","123","test","D:\\test.sql");
}
@Test
void test03() throws Exception {
NPtest01.dbBackUp("root","xk20010515","test","moni_yp","D:\\","test.sql");
}