美文网首页
使用java代码在linux下备份mysql数据库

使用java代码在linux下备份mysql数据库

作者: 阿__飞 | 来源:发表于2018-04-03 08:50 被阅读0次

1.执行下列代码

linux中,下列代码可以备份数据库

mysqldump --user=root --password=123456 --opt my>/root/test.2018.sql;

(其中,my为数据库名,>后为sql文件保存位置)

2.java代码

//数据库的备份

public static boolean exportDatabaseTool(String hostIP, String userName, String password, String savePath,

String fileName, String databaseName) throws InterruptedException {

// 创建文件保存的路径

File saveFile = new File(savePath);

if (!saveFile.exists()) {// 如果目录不存在

saveFile.mkdirs();// 创建文件夹

}

if (!savePath.endsWith(File.separator)) {

savePath = savePath + File.separator;

}

PrintWriter printWriter = null;

BufferedReader bufferedReader = null;

try {

printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath + fileName), "utf8"));

StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append("mysqldump").append(" --opt").append(" -h").append(hostIP);

stringBuilder.append(" --user=").append(userName).append(" --password=").append(password)

.append(" --lock-all-tables=true");

stringBuilder.append(" --result-file=").append(savePath + fileName).append(" --default-character-set=utf8 ")

.append(databaseName);

Process process = Runtime.getRuntime().exec(stringBuilder.toString());

InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf8");

bufferedReader = new BufferedReader(inputStreamReader);

String line;

while ((line = bufferedReader.readLine()) != null) {

printWriter.println(line);

}

printWriter.flush();

if (process.waitFor() == 0) {// 0 表示线程正常终止。

return true;

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (bufferedReader != null) {

bufferedReader.close();

}

if (printWriter != null) {

printWriter.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

return false;

}


//数据库的恢复

public static void recover(String path) throws IOException {

Runtime runtime = Runtime.getRuntime();

// 恢复到数据库的账户信息

Process process = runtime.exec("mysql -h 192.168.25.129 -u root -p123456 --default-character-set=utf8 qinmei");

OutputStream outputStream = process.getOutputStream();

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));

String str = null;

StringBuffer sb = new StringBuffer();

while ((str = br.readLine()) != null) {

sb.append(str + "\r\n");

}

str = sb.toString();

System.out.println(str);

OutputStreamWriter writer = new OutputStreamWriter(outputStream, "utf-8");

writer.write(str);

writer.flush();

outputStream.close();

br.close();

writer.close();

}


//启动备份到另一个数据库,执行该方法进行备份到异地数据库

@RequestMapping("/mysql")

@ResponseBody

public void getEmpById() throws IOException {

// 备份

try {

if (exportDatabaseTool("127.0.0.1", "root", "root", "D:/backupDatabase", "qinmei.sql", "qinmei")) {

System.out.println("数据库成功备份!!!");

} else {

System.out.println("数据库备份失败!!!");

}

} catch (InterruptedException e) {

e.printStackTrace();

}

// 恢复

recover("D:/backupDatabase/qinmei.sql");

}

相关文章

网友评论

      本文标题:使用java代码在linux下备份mysql数据库

      本文链接:https://www.haomeiwen.com/subject/xwqyxftx.html