欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MySQL 使用java备份某张表的操作

程序员文章站 2022-04-03 10:47:04
...

核心是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<String, String> backUpTableList = new ConcurrentHashMap<String, String>();
	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中,可以直接调用备份操作方法:

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

 

请您到ITEYE网站看 java小强 原创,谢谢!

http://cuisuqiang.iteye.com/ ! 

自建博客地址:http://www.javacui.com/ ,内容与ITEYE同步!