/**
* 首先把mysql的bin加入到系统的环境变量中,适用环境是window xp/nt/2000/2003
* 测试的MySQL版本是mysql-5.1.26-rc-win32.exe
*/
package bakup;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
/**
* @author conkeyn
* @时间 2008-12-5:上午11:53:53
*/
public class BakupMysql {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws Exception {
backup("localhost", "3306", "root", "123456", "proj_stock",
"D:\\test_path", "nameprefix");
}
/**
*
* @param hostName
* 数据库主机地址
* @param port
* 数据库主机端口
* @param user
* 数据库用户
* @param password
* 数据库密码
* @param database
* 要备份的数据库
* @param directory
* 存放SQL的目录
* @param filePrefix
* 文件名前缀,生成文件后会加上日期时间
* @throws Exception
*/
public static void backup(String hostName, String port, String user,
String password, String database, String directory,
String filePrefix) throws Exception {
if (StringUtils.isBlank(hostName)) {
System.out.println("You need to specify a host name.");
return;
}
if (StringUtils.isBlank(port)) {
port = "3306";
System.out
.println("You can specify a valid new port for the MySQL host. Default is 3306.");
}
if (StringUtils.isBlank(user)) {
System.out.println("You need to specify a user name.");
return;
}
if (StringUtils.isBlank(password)) {
System.out.println("You need to specify a password.");
return;
}
if (StringUtils.isBlank(database)) {
System.out.println("You need to specify a database name.");
return;
}
if (StringUtils.isBlank(directory)) {
System.out.println("You need to specify a validable directory.");
return;
}
if (StringUtils.isBlank(filePrefix)) {
System.out
.println("You need to specify a validable file name prefix.");
return;
}
String bakupCmd = "cmd.exe /c mysqldump --host="
+ hostName
+ " --port="
+ port
+ " --user="
+ user
+ " --password="
+ password
+ " --add-drop-database --add-drop-table --complete_insert --databases "
+ database + " ";
System.out.println("Executed bakup command: " + bakupCmd);
Runtime rt = Runtime.getRuntime();
Process child = rt.exec(bakupCmd);
InputStream in = child.getInputStream();// 控制台的输出信息作为输入流
InputStreamReader inputStream = new InputStreamReader(in, "GBK");// 设置输出流编码为GBK
String inStr;
StringBuffer sb = new StringBuffer("");
String outStr;
// 组合控制台输出信息字符串
BufferedReader bufferedReader = new BufferedReader(inputStream);
while ((inStr = bufferedReader.readLine()) != null) {
sb.append(inStr + "\r\n");
}
outStr = sb.toString();
System.out.println(outStr);
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd-HH-mm-ss");
String dateStr = dateFormat.format(new Date());
// 要用来做导入用的sql目标文件
directory = getDirectory(directory);
FileOutputStream fout = new FileOutputStream(directory + filePrefix
+ "-" + dateStr + ".sql");
OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8");
writer.write(outStr);
// 注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免
writer.flush();
// 别忘记关闭输入输出流
in.close();
inputStream.close();
bufferedReader.close();
writer.close();
fout.close();
// 退出控制台
rt.exec("cmd.exe /c exit");
}
public static String getDirectory(String directory) throws Exception {
directory = directory.replace("\\", "/");
if (!directory.endsWith("/")) {
directory += "/";
}
File file = new File(directory);
if (!file.exists()) {
file.mkdirs();
}
return directory;
}
}
备份MySQL数据库
最新推荐文章于 2024-05-15 15:34:26 发布