java使用mysqldump备份mysql数据库

今天公司分配了新任务,备份mysql数据库或者备份某两张表,做完oracle的备份,做mysql的备份,我都无语了,不过网友是无私的,感谢网友的分享,查过资料后,终于搞定,在此备份下资料,以免将来用到。代码是从oracle数据库那边改过来的,中间可能会有注释、属性名称涉及oracle,这个无所谓的
其实代码很简单,只是需要前台传递一个是否备份全库的标识1:全库;2:备份重要数据表

类名:IEEM060501Action.java


import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.Date;
import com.jysd.ms.exception.BaseException;
import com.jysd.ms.util.ReadSystemConfig;
import com.jysd.ms.util.TimeCountUtil;
import com.opensymphony.xwork2.ActionSupport;

public class IEEM060501Action extends ActionSupport {

private static final long serialVersionUID = 1L;
private Runtime runtime = Runtime.getRuntime();
private Process process = null ;
//返回前台数据
private String message;
//备份文件地址
private String backupUrl ;
//备份文件名

private String dmpName;
//备份方式
private int backupMode;
//备份类型
private int backupType;
/**
* @方法名称: execute
* @描述: 数据备份管理初始化方法
* 创建人:
* @return String
* @param @return
*/
public String execute()throws BaseException{
backupUrl = ReadSystemConfig.getValue("backupPath");
return SUCCESS;
}
/**
* @方法名称: backup
* @描述: 备份数据库

* 创建人:
* @return String
* @param @return
* @param @throws BaseException
*/
public String backup()throws BaseException{
//从配置文件中读取数据库信息,并备份数据库
boolean backupFlag = backupOracleDB(getExpString());
if(backupFlag){
message="备份成功!";
}else{
message="备份失败!";
}
return "backJson";
}
/**
* @方法名称: backupOracleDB
* @描述: 备份、还原 mysql数据库的方法
* 创建人:
* @return boolean
* @param @param cmdStr 备份命令 ( 即导出 )
* @param @return
*/
public boolean backupOracleDB(String cmdStr) {
boolean isSuccess = true;
try {
process = runtime .exec(cmdStr);
String line = null;
BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream()));
//读取ErrorStream很关键,这个解决了挂起的问题。
while ((line = br.readLine()) != null){
System.out.println(line);
}
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = br.readLine()) != null){
System.out.println(line);
}
//process.waitFor();
if(process.waitFor()!=0){
isSuccess = false;
}
}catch (Exception e) {
e.printStackTrace();
isSuccess = false;
}
return isSuccess ;
}
/**
* @方法名称: getLeftMenu
* @描述: 获取备份数据库的执行语句,备份文件名称,以当前时间命名
* @创建人:
* @return String
* @param @throws BaseException
*/
private String getExpString()throws BaseException{
StringBuffer sbu = new StringBuffer();
try {
sbu.append(ReadSystemConfig.getValue("mysqlPath"));
sbu.append("mysqldump ");
sbu.append("-h ");
sbu.append(ReadSystemConfig.getValue("ipaddress"));
sbu.append(" ");
sbu.append("-u");
sbu.append(ReadSystemConfig.getValue("username"));
sbu.append(" ");
sbu.append("-p");
sbu.append(ReadSystemConfig.getValue("password"));
sbu.append(" ");
/*
* 这只是一个快捷选项,等同于同时添加
* --add-drop-table,--add-locks,--create-options,--quick,--extended-insert,
* --lock-tables,--set-charset,--disable-keys选项。
* 本选项能让 mysqldump 很快的导出数据,并且导出的数据能很快导回。
* 该选项默认开启,但可以用 --skip-opt 禁用。
*/
sbu.append("--opt ");
//该选项在导出大表时很有用,它强制 mysqldump 从服务器查询取得记录直接输出而不是取得所有记录后将它们缓存到内存中
sbu.append("-q ");
//
//sbu.append("--lock-all-tables=true ");
//
//sbu.append("--complete-insert=false ");
//每条记录对应一个insert语句
sbu.append("--extended-insert=false ");
sbu.append("--result-file=");
String filePath = ReadSystemConfig.getValue("backupPath");
File file = new File(filePath);
if(!file.exists()){
file.mkdirs();
}
sbu.append(filePath);
//-start-数据库备份文件名称----------------------
//用户备份的名称为空时,以当前系统时间为备份名称;不为空时,在后面缀上系统时间
Date date = new Date();
if(dmpName==null){
dmpName = TimeCountUtil.dateToString(date,"yyyyMMddHHmmss");
}else{
dmpName += TimeCountUtil.dateToString(date,"yyyyMMddHHmmss");
}
dmpName = dmpName.replace(" ","");
dmpName = dmpName.replace(".sql","");
sbu.append(dmpName).append(".sql");
//-end--数据库备份文件名称----------------------
sbu.append(" ");
sbu.append("--default-character-set=utf8 ");

//备份方式编号等于2,仅备份监测数据(实时表、日结表数据)
if(backupMode==2){
//sbu.append("-B");
sbu.append(ReadSystemConfig.getValue("databaseName"));
//sbu.append(" ");
//sbu.append("--table");
sbu.append(" ").append(ReadSystemConfig.getValue("realtable")).append(" ").append(ReadSystemConfig.getValue("daytable"));
}else{
sbu.append(ReadSystemConfig.getValue("databaseName"));
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(sbu.toString());
return sbu.toString();
}
/**
* 读取数据库信息

*/
/*private static Properties properties = new Properties() ;
static {
InputStream in = ReadSystemConfig.class.getClassLoader().getResourceAsStream("backupOracle.properties");
try {
properties.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/

public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getBackupUrl() {
return backupUrl;
}
public void setBackupUrl(String backupUrl) {
this.backupUrl = backupUrl;
}
public String getDmpName() {
return dmpName;
}
public void setDmpName(String dmpName) {
this.dmpName = dmpName;
}
public int getBackupMode() {
return backupMode;
}
public void setBackupMode(int backupMode) {
this.backupMode = backupMode;
}
public int getBackupType() {
return backupType;
}
public void setBackupType(int backupType) {
this.backupType = backupType;
}

}

general.properties

backupPath=D\:/mysqlbackup/
mysqlPath=C\://Program Files//MySQL//MySQL Server 5.1//bin//
username=root
password=123456
ipaddress=192.168.1.126
databaseName=mydb
realtable=table1
daytable=table2

差点忘了这个读取properties的类了,也贴出来吧
ReadSystemConfig.java

package com.jysd.ms.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**实现功能:
* 类名: ReadSystemConfig.java
* 更新时间: $Date$
* 最后更新者: $Author$
* 修改备注:
*/
public class ReadSystemConfig {
private static Properties properties = new Properties() ;
static {
InputStream in = ReadSystemConfig.class.getClassLoader().getResourceAsStream("general.properties");
try {
properties.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static String getValue(String key){

return properties.getProperty(key);
}
}

你可能感兴趣的:(mysql)