java中用process备份还原mysql数据库

/**
* 备份数据库.
*/
public int dataBack(String sqldir,String dbname,String dbpwd,String filename,String filedir){
int flag2=0;
try{
String batdir=System.getProperty("user.dir")+"\\DataBack.bat";
log.info("bat file path=========>"+batdir);
Process process=Runtime.getRuntime().exec(batdir+" "+sqldir+" "+dbname+" "+dbpwd+" "+filename+" "+filedir);
String line=null;
BufferedReader reader=new BufferedReader(new InputStreamReader(process.getInputStream()));
while((line=reader.readLine())!=null){
log.info(line);
}
flag2=1;
}catch(Exception ex){

}
return flag2;
}


/**
* 还原数据库.
*/
public int dataRestore(String sqldir,String dbname,String dbpwd,String filename,String filedir){
int flag3=0;
try{
String batdir=System.getProperty("user.dir")+"\\DataRestore.bat";
Process process =Runtime.getRuntime().exec(batdir+" "+sqldir+" "+dbname+" "+dbpwd+" "+filename+" "+filedir);
String line = null;
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while((line = reader.readLine()) != null)
{
System.out.println(line);
flag3=1;
}

}catch(Exception ex){
ex.printStackTrace();
}
return flag3;
}


.bat 文件如下:

DataBack.bat如下:

if "%2"=="root" goto 1
if "%3"=="root" goto 2
if "%4"=="root" goto 3
if "%5"=="root" goto 4
if "%6"=="root" goto 5
:1
"%1\bin\mysqldump" -u%2 -p%3 --database OnlineExamSystem > %5\%4
goto 6
:2
"%1 %2\bin\mysqldump" -u%3 -p%4 --database OnlineExamSystem > %6\%5
goto 6
:3
"%1 %2 %3\bin\mysqldump" -u%4 -p%5 --database OnlineExamSystem > %7\%6
goto 6
:4
"%1 %2 %3 %4\bin\mysqldump" -u%5 -p%6 --database OnlineExamSystem > %8\%7
goto 6
:5
"%1 %2 %3 %4 %5\bin\mysqldump" -u%6 -p%7 --database OnlineExamSystem > %9\%8
:6

DataRestore.bat如下;

if "%2"=="root" goto 1
if "%3"=="root" goto 2
if "%4"=="root" goto 3
if "%5"=="root" goto 4
if "%6"=="root" goto 5
:1
"%1\bin\mysql" -u%2 -p%3 < %5\%4
goto 6
:2
"%1 %2\bin\mysql" -u%3 -p%4 < %6\%5
goto 6
:3
"%1 %2 %3\bin\mysql" -u%4 -p%5 < %7\%6
goto 6
:4
"%1 %2 %3 %4\bin\mysql" -u%5 -p%6< %8\%7
goto 6
:5
"%1 %2 %3 %4 %5\bin\mysql" -u%6 -p%7< %9\%8
:6
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值