快捷搜索:

使用java备份MySQL数据表实例

核心是mysqldump和Runtime

操作着实并不是很艰苦,创建一个进行备份操作的类,接管到备份调用后,标记该表正在备份,然后创建一个子线程进行备份操作。所需的设置设置设备摆设摆设信息是从设置设置设备摆设摆设文件读取的,也要留意在Windows和linux下路径问题。

设置设置设备摆设摆设文件如下:

# 数据库地址

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//

履行功能的代码类如下:

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;}

@Overridepublic 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中,可以直接调用备份操作措施:

DbBackUpMethod.getDbBackUpMethod().backup(tableName); // 调用备份

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

if(null != DbBackUpMethod.backUpTableList.get(tableName))

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

function deleteTableByTableName(){

var pk = table.getSelectedKeys();if(""==pk){

alert("请选择一笔记录!");return false;

}if(pk.length > 1){

alert("请选择一笔记录!");return false;

}var rows = table.get(pk);

var tableName=rows.tableName;if(confirm("你确认要删除该表吗?")) {

if(confirm("删除该表前,你必要备份操作吗?\n\n选择备份后,系统将后台进行相关操作!\n在此时代,您不能删除该表!\n备份操作可能将持续数小韶光阴!请知晓!")) {document.form1.action="backUpTable.action?tableName=" + tableName;

document.form1.submit();}else{

if(confirm("你确认提交吗?该表将删除!")) {document.form1.action="del.action?tableName=" + tableName;

document.form1.submit();}

}}

}

您可能还会对下面的文章感兴趣: