1. 个人技术分享首页

MySQL基于java实现备份表操作

核心是mysqldump和Runtime
操作其实并不是很困难,创建一个进行备份操作的类,接收到备份调用后,标记该表正在备份,然后创建一个子线程进行备份操作。所需的配置信息是从配置文件读取的,也要注意在Windows和linux下路径问题。

配置文件如下:

Java代码 收藏代码
# 数据库地址
dbAddress=localhost
# 要备份的数据库名称
databaseName=nms
# 数据库用户名
username = root
# 数据库密码
password = root
# mysqldump 路径 Linux
mysqlpath = /usr/bin/
# 备份文件存放位置 Linux
sqlFilePath =/MySQlBack/
# mysqldump 路径 Windows
#mysqlpath = C://Program Files//MySQL//MySQL Server 5.5//bin//
# 备份文件存放位置 Windows
#sqlFilePath =C://MySQl//

执行功能的代码类如下:

Java代码 收藏代码
package com.nms.common.db;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 用于数据库备份操作
*/
public class DbBackUpMethod {
private static Log logger = LogFactory.getLog(DbBackUpMethod.class);
private static Properties pros = getPprVue(“db.properties”);
public static Map backUpTableList = new ConcurrentHashMap();
private static DbBackUpMethod backObj = new DbBackUpMethod();
public static DbBackUpMethod getDbBackUpMethod(){
return backObj;
}
public void backup(String tableName) {
if(null != backUpTableList.get(tableName)) return ;
backUpTableList.put(tableName, tableName); // 标记已经用于备份
new Thread(new DbBackUpThread(tableName)).start();
}
/**
* 用于执行某表的备份
*/
class DbBackUpThread implements Runnable {
String tableName = null;
public DbBackUpThread(String tableName){
this.tableName = tableName;
}
@Override
public void run() {
try {
String username = pros.getProperty(“username”);
String password = pros.getProperty(“password”);
String mysqlpaths = pros.getProperty(“mysqlpath”);
String address = pros.getProperty(“dbAddress”);
String databaseName = pros.getProperty(“databaseName”);
String sqlpath = pros.getProperty(“sqlFilePath”);
File backupath = new File(sqlpath);
if (!backupath.exists()) {
backupath.mkdir();
}
StringBuffer sb = new StringBuffer();
sb.append(mysqlpaths);
sb.append(“mysqldump “);
sb.append(“–opt “);
sb.append(“-h “);
sb.append(address);
sb.append(” “);
sb.append(“–user=”);
sb.append(username);
sb.append(” “);
sb.append(“–password=”);
sb.append(password);
sb.append(” “);
sb.append(“–lock-all-tables=true “);
sb.append(“–result-file=”);
sb.append(sqlpath);
sb.append(tableName+”.sql”);
sb.append(” “);
sb.append(“–default-character-set=utf8 “);
sb.append(databaseName);
sb.append(” “);
sb.append(tableName);
Runtime cmd = Runtime.getRuntime();
Process p = cmd.exec(sb.toString());
p.waitFor(); // 该语句用于标记,如果备份没有完成,则该线程持续等待
} catch (Exception e) {
logger.error(“备份操作出现问题”, e);
}finally{
backUpTableList.remove(tableName); // 最终都将解除
}
}
}
public static Properties getPprVue(String properName) {
InputStream inputStream = DbBackUpMethod.class.getClassLoader().getResourceAsStream(properName);
Properties p = new Properties();
try {
p.load(inputStream);
inputStream.close();
} catch (IOException e) {
logger.error(“无法读取用于备份数据的配置文件”, e);
}
return p;
}
}

在Action中,可以直接调用备份操作方法:

Java代码 收藏代码
DbBackUpMethod.getDbBackUpMethod().backup(tableName); // 调用备份

同时,如果页面有删除该表的操作,在操作前应该判断该表是否在进行备份

Java代码 收藏代码
if(null != DbBackUpMethod.backUpTableList.get(tableName))

然后页面JSP调用时,可以给予响应的提示,我的判断是只能删除一张表:

文章来源于互联网:MySQL基于java实现备份表操作

原创文章,作者:admin,如若转载,请注明出处:https://www.aliyunsolution.com/3567.html