import java.io.FileInputStream;
import java.io.OutputStream;
public class MySQLBackupUtil {
private static void restore(String host, String port, String user,
String password, String db, String sqlFile) throws Exception {
int BUFFER = 10485760;
String cmd = "mysql --host=" + host;
if (port != null && port.length() > 0)
cmd += " --port=" + port;
cmd += " --user=" + user;
if (password != null && password.length() > 0)
cmd += " --password=" + password;
cmd += " " + db;
Process run = Runtime.getRuntime().exec(cmd);
FileInputStream fis = new FileInputStream(sqlFile);
OutputStream out = run.getOutputStream();
byte[] buf = new byte[BUFFER];
int len;
while ((len = fis.read(buf)) >= 0)
out.write(buf, 0, len);
fis.close();
out.close();
}
public static void main(String[] args) {
try {
String sqlFile = "D:\\APP\\workspace\\chunkcode\\dbtest.sql";
restore("localhost", "3306", "root", "", "dbtest", sqlFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Restore MySQL Database Using Java
Restoring MySQL database from a backup file (SQL File).
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment