编写导出sql文件的shell脚本,在/home/amtcloud/sql/目录下的exportsql.sh
#!/bin/sh
cd /usr/share/mysql/ (或者mysql的bin目录下,即mysqldump存在的地方)
mysqldump -h192.168.58.255 -P3306 -uroot -p123 "$1“ ”$2" > /home/amtcloud/sql/aaa.sql
javad代码如下:
Runtime time = Runtime.getRuntime();
try {
/*time.exec("cd D:\\Program Files\\MySQL\\MySQL Server 5.6\\bin");
time.exec("mysqldump -h58.215.56.89 -P3306 -uroot -p123456 673680482_qq_aaa > D:\\aaa.sql");*/
String command = “/home/amtcloud/sql/exportsql.sh”;
//time.exec(command);
String[] com = new String[]{"/bin/sh","-c","bash "+command+" databasename"+" tablename"};
Process process = time.exec(com);
BufferedReader errorReader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = null;
while ((line = errorReader.readLine()) != null) {
System.err.println(line);
}
errorReader.close();
BufferedReader infoReader = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
while ((line = infoReader.readLine()) != null) {
System.out.println(line);
}
infoReader.close();
} catch (IOException e) {
e.printStackTrace();
}