java 备份 mysql 日志_如何用Java实现MySQL数据库的备份和恢复

package xxx.utils;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.io.UnsupportedEncodingException;

/**

* MySQL数据库的备份与恢复 缺陷:可能会被杀毒软件拦截

*

* @author xxx

* @version xxx

*/

public class DatabaseBackup {

/** MySQL安装目录的Bin目录的绝对路径 */

private String mysqlBinPath;

/** 访问MySQL数据库的用户名 */

private String username;

/** 访问MySQL数据库的密码 */

private String password;

public String getMysqlBinPath() {

return mysqlBinPath;

}

public void setMysqlBinPath(String mysqlBinPath) {

this.mysqlBinPath = mysqlBinPath;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public DatabaseBackup(String mysqlBinPath, String username, String password) {

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

mysqlBinPath = mysqlBinPath + File.separator;

}

this.mysqlBinPath = mysqlBinPath;

this.username = username;

this.password = password;

}

/**

* 备份数据库

*

* @param output

*            输出流

* @param dbname

*            要备份的数据库名

*/

public void backup(OutputStream output, String dbname) {

String command = "cmd /c " + mysqlBinPath + "mysqldump -u" + username

+ " -p" + password + " --set-charset=utf8 " + dbname;

PrintWriter p = null;

BufferedReader reader = null;

try {

p = new PrintWriter(new OutputStreamWriter(output, "utf8"));

Process process = Runtime.getRuntime().exec(command);

InputStreamReader inputStreamReader = new InputStreamReader(process

.getInputStream(), "utf8");

reader = new BufferedReader(inputStreamReader);

String line = null;

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

p.println(line);

}

p.flush();

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (reader != null) {

reader.close();

}

if (p != null) {

p.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 备份数据库,如果指定路径的文件不存在会自动生成

*

* @param dest

*            备份文件的路径

* @param dbname

*            要备份的数据库

*/

public void backup(String dest, String dbname) {

try {

OutputStream out = new FileOutputStream(dest);

backup(out, dbname);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

/**

* 恢复数据库

*

* @param input

*            输入流

* @param dbname

*            数据库名

*/

public void restore(InputStream input, String dbname) {

String command = "cmd /c " + mysqlBinPath + "mysql -u" + username

+ " -p" + password + " " + dbname;

try {

Process process = Runtime.getRuntime().exec(command);

OutputStream out = process.getOutputStream();

String line = null;

String outStr = null;

StringBuffer sb = new StringBuffer("");

BufferedReader br = new BufferedReader(new InputStreamReader(input,

"utf8"));

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

sb.append(line + "/r/n");

}

outStr = sb.toString();

OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");

writer.write(outStr);

writer.flush();

out.close();

br.close();

writer.close();

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 恢复数据库

*

* @param dest

*            备份文件的路径

* @param dbname

*            数据库名

*/

public void restore(String dest, String dbname) {

try {

InputStream input = new FileInputStream(dest);

restore(input, dbname);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

Configuration config = HibernateSessionFactory.getConfiguration();

String binPath = config.getProperty("mysql.binpath");

String userName = config.getProperty("connection.username");

String pwd = config.getProperty("connection.password");

DatabaseBackup bak = new DatabaseBackup(binPath, userName, pwd);

bak.backup("c:/ttt.sql", "ttt");

bak.restore("c:/ttt.sql", "ttt");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值